Page 1 of 1

How to change the distance of a line..

Posted: Thu Nov 24, 2011 4:07 am
by Zaid
Okay so say I have a line (20,40) , (40 , 80)

The Distance between the two points is 45.

I need the distance to be 30.

How do I find the new end point with that length?

Re: How to change the distance of a line..

Posted: Thu Nov 24, 2011 4:41 am
by NotZachary82
(20, 40) , (~33, ~47)

I think.

Re: How to change the distance of a line..

Posted: Thu Nov 24, 2011 5:55 am
by Zaid
How did you do it?
That was an example line, I was mainly trying to learn how to do it.

Re: How to change the distance of a line..

Posted: Thu Nov 24, 2011 6:37 pm
by XZodia
Distance = End - Start
Direction = Distance / Distance Length
New End = Start + Direction * 30

Distance Length Found By:
Square Root (X * X + Y * Y)

Re: How to change the distance of a line..

Posted: Fri Nov 25, 2011 4:43 am
by Zaid
How do I get x2 and y2 from "Start + Direction * 30"?

Re: How to change the distance of a line..

Posted: Fri Nov 25, 2011 8:47 am
by troymac1ure
Zaid wrote:Okay so say I have a line (20,40) , (40 , 80)

The Distance between the two points is 45.

I need the distance to be 30.

How do I find the new end point with that length?
In the above example
dx = x2 - x1
= 40 - 20
= 20

dy = y2 - y1
= 80 - 40
= 40

distance = Sqrt(dx^2 + dy^2)
= 44.72

therfore, to get the new distance of 30, find the difference in length between the new point & old point:
newDistancePercent = new distance / orignal distance
= 30 / 44.72
= 0.67 (or 67% of original length)

ndx = dx * newDistancePercent
= 20 * 0.67
= 13.41

ndy = dy * newDistancePercent
= 40 * 0.67
= 26.83

So now add the starting points to ndx & ndy and you will have the end coordinates:
x end = x1 + ndx
= 20 + 13.41
= 33.41

y end = y1 + ndy
= 40 + 26.83
= 66.83


If you check the above with the given rounding, you get a distance of 29.995, so close enough. If you're doing it in a program, you won't have the precision loss.

Re: How to change the distance of a line..

Posted: Sat Nov 26, 2011 1:28 am
by XZodia
Zaid wrote:How do I get x2 and y2 from "Start + Direction * 30"?
Its the equation of a straight line. The Direction has a length of 1, ie a unit in the axis defined by the Direction. Thus, multiplying by 30 gives a line whose length is 30.

Re: How to change the distance of a line..

Posted: Sat Nov 26, 2011 1:44 am
by Zaid
Thank you both, So much!
It worked perfectly!