# calculating a point of intersection with a bounding box given two points

Rather than try to explain this in purely mathematical terms (which I will fail to do properly, I'm sure), I will simply explain what I am attempting to accomplish and the parameters I am working in as best as I can. I don't think this is terribly complicated, but I clearly lack the ability to figure this out on my own, so I really appreciate any assistance you can provide me.

I am working on a 2d game for iPad that uses the Cocos2d framework. In that framework, the 0,0 coordinate position is at bottom-left and the 1024,768 coordinate position (on iPad 1 & 2) is at top right. In my game, a player's ship can be at virtually any point on the screen, and can fire a projectile in any direction. The direction of firing is based on the user tapping on the screen.

In order to animate the movement of the projectile, I must provide an x,y destination coordinate to a Cocos2d function. Because the projectile is ultimately moving off-screen (unless it intersects with an enemy ship), this x,y coordinate must be a point off-screen - not the point that the user touched, but a point on a line that goes from the ship location through the touch location to the ultimate projectile destination.

The problem is therefore as follows: given a ship at x1,y1, and a touch at x2,y2, what are the coordinates of a point x3,y3 that is off-screen, where all points are on a straight line?

Because it might ultimately fit with the game's eventual development, and because I think it might make the solution simpler, I was thinking it might be good to define a circle whose center is at the center of the screen, but that is larger than the screen (so that no part of the circle, if drawn, would be visible). Then, the point x3,y3 would be the point where the line drawn through x1,y1 and x2,y2 intersects with the circle. To accomplish this solution, I would also need the math for defining such a circle, including considering that the origin in my coordinate system would be at screen width (1024) / 2 and screen height (768) / 2.

Lastly, I took math up to a pre-calculus level in college so somewhere inside my brain I do have the basics for how to solve this kind of problem on my own, but as a software developer I haven't needed those skills in a decade and I've gotten really rusty. However, I'm starting to play around a bit with game programming, and suddenly I need these skills again. What topics would you recommend I focus on? Just general geometry and trigonometry?

-
I've spent a couple of hours on this and I think I know what to do now. The lack of a response here is probably a good thing, since it's forced me to figure it out on my own. Admins, you can go ahead and delete this question if you like. Its presence makes me feel lazy. – Adrian Duyzer May 28 '12 at 2:08
Why don't you post your solution as an answer here and help out others. That way you won't feel as lazy! – Tom Auger Aug 24 '12 at 15:01
@TomAuger I actually ended up deciding to use a bounding box instead of a circle, and I blogged about it and made the source code (in Ruby and Objective C) publicly available. factore.ca/on-the-floor/… – Adrian Duyzer Sep 4 '12 at 15:01

Let $b$ be some big number (at least 1024). Then $$x3 = x1 + b*(x2 - x1)$$ $$y3 = y1 + b*(y2 - y1)$$
This assumes that $x2 \ne x1$ and $y2 \ne y1$, which seems reasonable.