#### Archived

This topic is now archived and is closed to further replies.

# Finding line slope, endpoint

## Recommended Posts

Ishamael    122
I''m writing an Asteroids clone (yes, I know that''s lame, but...) and one of the weapons the player can get is a laser. In order to display a laser being shot I want to draw a line from the ship to the edge of the screen. If I know the (X,Y) location of the ship and the angle the ship is pointing, how would I calculate the endpoint of a line somewhere off the screen so I could draw a line for the laser? Or is there some other better way to do this? Thanks, Ishamael

##### Share on other sites
wise_Guy    122
Okay if you know the angle the player is facing, and want to draw a line 100 pixels long, then use trigonometry:

  start_x = player.x;start_y = player.y;end_x = start_x + (radius * sin[angle]);end_y = start_y + (radius * cos[angle]);
;

You will also have to invert the y endpoint because on the computer screen y=0 is at the top of the screen while on the cartesian plane y=0 is at the bottom (of the 1st quadrant).

Hope that helps!

##### Share on other sites
LilBudyWizer    491
Don''t draw off the edge of the screen. It is a bad habit. Use the slope of the line and the distance to the edge. Based upon the direction of the ray you can only hit two sides. Pick one and use it. If the point you hit that side is past the other side then repeat for the other side otherwise use it. If you have an angle instead of a direction vector, then the tangent is the slope. The only thing you have to watch out for is vertical lines, but then that is easy. It is either the top edge or bottom edge with the same x.