How to change the distance of a line..

Discuss your Halo 2 mod project here.
Post Reply
User avatar
Zaid
Posts: 250
Joined: Sun Jan 09, 2011 2:07 am

How to change the distance of a line..

Post 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?
User avatar
NotZachary82
Posts: 1846
Joined: Thu Dec 20, 2007 8:39 pm

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

Post by NotZachary82 »

(20, 40) , (~33, ~47)

I think.
User avatar
Zaid
Posts: 250
Joined: Sun Jan 09, 2011 2:07 am

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

Post by Zaid »

How did you do it?
That was an example line, I was mainly trying to learn how to do it.
User avatar
XZodia
Staff
Posts: 2208
Joined: Sun Dec 09, 2007 2:09 pm
Location: UK
Contact:

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

Post by XZodia »

Distance = End - Start
Direction = Distance / Distance Length
New End = Start + Direction * 30

Distance Length Found By:
Square Root (X * X + Y * Y)
Image
JacksonCougar wrote:I find you usually have great ideas.
JacksonCougar wrote:Ah fuck. Why must you always be right? Why.
User avatar
Zaid
Posts: 250
Joined: Sun Jan 09, 2011 2:07 am

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

Post by Zaid »

How do I get x2 and y2 from "Start + Direction * 30"?
User avatar
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..

Post 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.
User avatar
XZodia
Staff
Posts: 2208
Joined: Sun Dec 09, 2007 2:09 pm
Location: UK
Contact:

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

Post 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.
Image
JacksonCougar wrote:I find you usually have great ideas.
JacksonCougar wrote:Ah fuck. Why must you always be right? Why.
User avatar
Zaid
Posts: 250
Joined: Sun Jan 09, 2011 2:07 am

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

Post by Zaid »

Thank you both, So much!
It worked perfectly!
Post Reply