# Getting P3 from P1, P2. Dist P3-P1 > Dist P1-P2 & P3 on line P1-P2

## Recommended Posts

homer_3    173
I'm trying to get point 3 from points 1 and 2 such that point 3 is on the same line that points 1 and 2 form, the distance between P1 and P3 is greater than P1 and P2, and the vector direction from P1 to P3 is the same as P1 to P2. This is what I have, but it doesn't work at all. Can anyone point out where I went wrong?

Origin is top left. +X is right. +Y is down.
[code]

lv_destPos = OrderGetTargetPoint(EventUnitOrder());
lv_destX = PointGetX(lv_destPos);
lv_destY = PointGetY(lv_destPos);

lv_curX = PointGetX(lv_curPos);
lv_curY = PointGetY(lv_curPos);

//get slope
lv_m = (lv_curY-lv_destY)/(lv_curX-lv_destX);

//get y intercept
lv_b = lv_y - (lv_m * lv_x);

//clicked to right
if(lv_destX > lv_curX)
{
lv_destX = lv_destX + 50.0;
lv_destY = (lv_m * lv_destX) + lv_b;
}else
{
//clicked to left
if(lv_destX < lv_curX)
{
lv_destX = lv_destX - 50.0;
lv_destY = (lv_m * lv_destX) + lv_b;
}else
{
//clicked below
if(lv_destY > lv_curY)
{
lv_destY = lv_destY+50;
lv_destX = (lv_destY - lv_b)/lv_m;
}else
{
//clicked above
lv_destY = lv_destY-50;
lv_destX = (lv_destY - lv_b)/lv_m;
}
}
}

lv_destPos = Point(lv_destX, lv_destY);[/code]

##### Share on other sites
Emergent    982
Any point on the line containing points p1 and p2 can be written as,

p = (1 - t) p1 + t p2

for some real number t. When t=0, you get p1. When t=1, you get p2. When 0 < t < 1, you get a point in-between (e.g., when t=1/2, you get the midpoint). When t is negative, or when it is greater than 1, you get a point beyond p1 or p2 on the line, respectively.

##### Share on other sites
alvaro    21246
What Emergent said. Maybe you'll find it easier to think of that formula as
P3 = P1 + t*(P2-P1)

Plugging in t=2 will give you a P3 that satisfies everything you asked for.

##### Share on other sites
homer_3    173
[quote][color=#1C2837][size=2]P3 = P1 + t*(P2-P1)[/quote][/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]I remember doing this before and I thought it was P1 * t*(P2-P1). But that didn't work for me so I tried just getting the line formula and going from there. I'll try this out though, thanks. Just to be clear[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]P3.X = P1.X + t*(P2.X-P1.X)[/size][/color]
[color=#1C2837][size=2]P3.Y = P1.Y + t*(P2.Y-P1.Y)[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]correct?[/size][/color]

##### Share on other sites
jyk    2094
[quote name='homer_3' timestamp='1302304232' post='4796133']
[[color="#1C2837"][size="2"]P3.X = P1.X + t*(P2.X-P1.X)[/size][/color]
[color="#1C2837"][size="2"]P3.Y = P1.Y + t*(P2.Y-P1.Y)[/size][/color]

[color="#1C2837"][size="2"]correct?[/size][/color]
[/quote]
As far as the parametric line form goes, yes, that looks correct.