Find end point of line

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
cdrwolfe
Posts: 100
Joined: Thu Nov 15, 2007 5:38 pm
Location: Cranfield University

Find end point of line

Post by cdrwolfe »

Hi all,

What i would think would be a simepl thing has me a little perplexed, and certainly not for the first time :).

I have been trying to find a way to get the endpoint of a line (2D space) using the lines length and its angle and start point.

So google tells me :),

end.X = Length * Cos(angle) + Start.X
end.Y = Length * Sin(angle) + Start.Y

However this doesn't seem to work for what I'am using.

Image

I need to basically draw lines from each corner, on the left i want the lines to have an outward angle (180 to 360 deg), while the right the opposite.

In the above scenario i set it that all angles given are 45degrees and in order to mirror the left one i add 180.

So i convert my 45 degrees to radians, because Math.Cos uses only radians and then perform the calculation above.

I then draw the line with the given start and endpoints, however as you see the line is drawn at a 135 deg angle relative to the start point for the right side and 315 deg angle for the left. In both cases it is an extra 90 deg out.

I can't quite figure out why, other then reading somehwere that find the point is based on the hypotenuse of a triangle, no doubt one restricted to a 90 deg angle, other then that I'am clueless :).

Any one have any ideas?

Regards Wolfe
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Could you just draw an example (or examples) of what you'd like to see?
olivehehe_03
Posts: 157
Joined: Tue Mar 20, 2007 8:30 am

Post by olivehehe_03 »

I ran into a similar problem a little while back in one of my random experiments :lol:

Not sure if it's the best way to do it but the core::vector2d class template has a function called rotateBy which, to quote the documentation, "rotates the point anticlockwise around a center by an amount of degrees" (go here).

Borrowing from my original solution, the way I would do it would be to create a vector2d and set the position to startPoint.x, startPoint.Y + length so that your new points is "length" distance above your starting point. Then using that function on your new point, give it your starting point as the center and work out what then angle needs to be (doing it this way, assuming 0° as straight up).

Image

According to my awesome diagram here (it was getting a little wierd without a picture :D), red dot is your starting point, blue dot is our new point, red line with arrow is your angle so therefore blue angle is 360° - red angle. That function should return your new point which should end up nicely on the end of your line.

Hope that helps (and works)
Tell me what you cherish most. Give me the pleasure of taking it away.
Post Reply