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:
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?
You already have X+Rcos(θ)=150 to calculate when the point hits one of the sides. It's positive when R is traveling towards that side. Solve this for R, then you want the minimum positive value of the four formulas.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement