Results 1 to 3 of 3

Thread: Calculating coords of a point which is on a straight line

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    Exclamation Calculating coords of a point which is on a straight line

    Hi,

    I have a straight line whose start and end points are known. (Pt1 and Pt2)...

    Is it possible to calculate the coordinates of a point that is on on this straight line at a distance of 1000 from pt1 or pt2...

    thanks

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Calculating coords of a point which is on a straight line

    Yup, it sure is.

    First you need an equation for your line. Personally I think parametric (instead of slope-intercept) is conceptually nicer, so I'll use it.

    Let each point's coordinates be P.X and P.Y (as you would expect).

    Then we get a dummy variable d (it can be interpreted as distance if you like) such that when d=0, we're at the first line segment, and at d=D, we're at the second line segment. Since we're using parametric lines, we have two functions that describe the x and y coordinate of the line as we change d, with the specific values given below:

    x(d=0) = P1.X
    y(d=0) = P1.Y
    x(d=D) = P2.X
    y(d=D) = P2.Y

    Since we're describing a line, we know both x(d) and y(d) are linear, so

    x(d) = x1*d + x0
    y(d) = y1*d + y0

    for some x1, y1, x0, y0. Plug the previous values in to solve for these variables and we have

    x0 = P1.X
    x1 = (P2.X - P1.X) / D
    y0 = P1.Y
    y1 = (P2.Y - P1.Y) / D

    Yay! We now have a nifty parametric function of our line--how do we travel along it some number of units?

    First, we have to set our scale factor, D. We have to let this be the actual numerical distance between the two points P1 and P2, otherwise our distances will be interpreted in different units (i.e. inches instead of meters, if P1 and P2 are in meters, or some other such arbitrary conversion). So, we let D = dist(P1, P2) = Sqrt((P1.X - P2.X)^2 + (P1.Y - P2.Y)^2).

    Now, we may very easily find the point of interest due to the equations above. To find a point some distance D0 from the first point P1 travelling towards P2, just plug in D0 to the parametric equations:

    P0.X = x(D0) = x1*D0 + x0
    P0.Y = y(D0) = y1*D0 + y0

    Since I've solved for everything else above, you're done.

    If you want to find a point some distance from the other end of your line segment (that is, a distance from P2), just flip P1 and P2. This works since I set the distance d=0 at whatever the first point is, and the distance d=D=the true distance between the points at whatever the second point is.

    If you want to find a point some distance away from the other end of your line segment, use a negative D0.


    Note: I would give some code, but I think this is enough assistance on the math in itself. Even if I went too far talking about functions and stuff, you should be able to follow the argument well enough to pick out the useful bits and code it :) If you have specific questions, though, please don't hesitate to ask.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    Re: Calculating coords of a point which is on a straight line

    Thanks a ton jemidiah; let me integrate your solution with my work and revert back to you....

    thanks again....

    Quote Originally Posted by jemidiah
    Yup, it sure is.

    First you need an equation for your line. Personally I think parametric (instead of slope-intercept) is conceptually nicer, so I'll use it.

    Let each point's coordinates be P.X and P.Y (as you would expect).

    Then we get a dummy variable d (it can be interpreted as distance if you like) such that when d=0, we're at the first line segment, and at d=D, we're at the second line segment. Since we're using parametric lines, we have two functions that describe the x and y coordinate of the line as we change d, with the specific values given below:

    x(d=0) = P1.X
    y(d=0) = P1.Y
    x(d=D) = P2.X
    y(d=D) = P2.Y

    Since we're describing a line, we know both x(d) and y(d) are linear, so

    x(d) = x1*d + x0
    y(d) = y1*d + y0

    for some x1, y1, x0, y0. Plug the previous values in to solve for these variables and we have

    x0 = P1.X
    x1 = (P2.X - P1.X) / D
    y0 = P1.Y
    y1 = (P2.Y - P1.Y) / D

    Yay! We now have a nifty parametric function of our line--how do we travel along it some number of units?

    First, we have to set our scale factor, D. We have to let this be the actual numerical distance between the two points P1 and P2, otherwise our distances will be interpreted in different units (i.e. inches instead of meters, if P1 and P2 are in meters, or some other such arbitrary conversion). So, we let D = dist(P1, P2) = Sqrt((P1.X - P2.X)^2 + (P1.Y - P2.Y)^2).

    Now, we may very easily find the point of interest due to the equations above. To find a point some distance D0 from the first point P1 travelling towards P2, just plug in D0 to the parametric equations:

    P0.X = x(D0) = x1*D0 + x0
    P0.Y = y(D0) = y1*D0 + y0

    Since I've solved for everything else above, you're done.

    If you want to find a point some distance from the other end of your line segment (that is, a distance from P2), just flip P1 and P2. This works since I set the distance d=0 at whatever the first point is, and the distance d=D=the true distance between the points at whatever the second point is.

    If you want to find a point some distance away from the other end of your line segment, use a negative D0.


    Note: I would give some code, but I think this is enough assistance on the math in itself. Even if I went too far talking about functions and stuff, you should be able to follow the argument well enough to pick out the useful bits and code it If you have specific questions, though, please don't hesitate to ask.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width