Getting position certain way down a line segment
Author |
Message |
DtY
|
Posted: Mon Jul 27, 2009 9:29 pm Post subject: Getting position certain way down a line segment |
|
|
I'm writing this in python, but it's in general programming because it's in no way python specific.
I have two points that make up a line segment, and I need to get the x,y position of a certain way through the line (the position is 0-1 inclusive).
So far, this is how I tried doing it, it doesn't work right, the object moves along some weird line, but lines up at the halfway point (which will make sense as I explain it)
Of course, to get the midpoint of a line it's:
(start.x + end.x) / 2 (and same for y)
Which of course is:
(start.x + end.x) * .5
So it felt natural that the position one quarter in would be:
(start.x + end.x) * .25
Apparently not though, because that's putting it along some other line. (It should be moving left to right (and slightly down) across part of the screen, but it's starting in the top left and going right and down, lining up at .5 (Which makes sense if you think about it (start.x+end.x)*0=0, *.1 would be right and down of that, so on), so that doesn't work.
Thanks for any help. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
cavetroll
|
Posted: Mon Jul 27, 2009 11:09 pm Post subject: Re: Getting position certain way down a line segment |
|
|
Well, if you apply the logic from the first step (to find the midpoint), you find that for the quarter way mark, you need to find half way between the start and the midpoint.
Therefore:
(start.x + mid.x) / 2 = quarter.x
if
mid.x = (start.x + end.x) / 2
it follows that
(start.x + (start.x + end.x) / 2) / 2 = quarter.x
0.75 * start.x + 0.25 * end.x = quarter.x
So to find an arbitrary fraction point along the line, you just use:
(1-f)*start.x + f*end.x
where f is the fraction. |
|
|
|
|
|
DtY
|
Posted: Tue Jul 28, 2009 9:30 am Post subject: RE:Getting position certain way down a line segment |
|
|
Thanks |
|
|
|
|
|
|
|