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?
How to change the distance of a line..
- NotZachary82
- Posts: 1846
- Joined: Thu Dec 20, 2007 8:39 pm
Re: How to change the distance of a line..
(20, 40) , (~33, ~47)
I think.
I think.
- Zaid
- Posts: 250
- Joined: Sun Jan 09, 2011 2:07 am
Re: How to change the distance of a line..
How did you do it?
That was an example line, I was mainly trying to learn how to do it.
That was an example line, I was mainly trying to learn how to do it.
- XZodia
- Staff
- Posts: 2208
- Joined: Sun Dec 09, 2007 2:09 pm
- Location: UK
- Contact:
Re: How to change the distance of a line..
Distance = End - Start
Direction = Distance / Distance Length
New End = Start + Direction * 30
Distance Length Found By:
Square Root (X * X + Y * Y)
Direction = Distance / Distance Length
New End = Start + Direction * 30
Distance Length Found By:
Square Root (X * X + Y * Y)
- Zaid
- Posts: 250
- Joined: Sun Jan 09, 2011 2:07 am
Re: How to change the distance of a line..
How do I get x2 and y2 from "Start + Direction * 30"?
- troymac1ure
- Keeper of Entity
- Posts: 1282
- Joined: Sat Aug 09, 2008 4:16 am
- Location: British Columbia, Canada, eh
- Contact:
Re: How to change the distance of a line..
In the above exampleZaid 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?
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.
- XZodia
- Staff
- Posts: 2208
- Joined: Sun Dec 09, 2007 2:09 pm
- Location: UK
- Contact:
Re: How to change the distance of a line..
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.Zaid wrote:How do I get x2 and y2 from "Start + Direction * 30"?
- Zaid
- Posts: 250
- Joined: Sun Jan 09, 2011 2:07 am
Re: How to change the distance of a line..
Thank you both, So much!
It worked perfectly!
It worked perfectly!