Given a point A at (X,Y) which is inside the rectangle defined by the points (9,9), (9,78), (150,9), and (150, 78), and also given an angle θ which represents the direction in which point A will begin moving (θ=0 representing the positive x direction), I'm trying to find the distance r point A will travel before hitting one of the rectangle's sides. Here's the code I threw together initially to solve the problem:
Code:
As r doesn't need to be accurate past whole numbers, this code works. However, it's quite slow (and I guess you could say pretty "BASIC"...) and I'm having trouble thinking of a faster method. Any suggestions?
Code:
0→R
Repeat X+Rcos(θ)≥150 or X+Rcos(θ)≤9 or Y+Rsin(θ)≥78 or Y+Rsin(θ)≤9
R+10→R:End
Repeat X+Rcos(θ)≤150 and X+Rcos(θ)≥9 and Y+Rsin(θ)≤78 and Y+Rsin(θ)≥9
R-1→R:End
As r doesn't need to be accurate past whole numbers, this code works. However, it's quite slow (and I guess you could say pretty "BASIC"...) and I'm having trouble thinking of a faster method. Any suggestions?