Results 1 to 7 of 7

Thread: Restrict Line Distance

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Restrict Line Distance

    What I'm wanting to do is add a line to a graphic path, but limit the distance from point a to point b. For example, the if the maximum distance allowed for a line is 10 then using this function to get the distance:
    Code:
    Public Function GetDistance(ByVal startPoint As Point, ByVal endPoint As Point) As Int32
        Return Convert.ToInt32(Math.Sqrt((Math.Abs(endPoint.X - startPoint.X) ^ 2) + (Math.Abs(endPoint.Y - startPoint.Y)
    End Function
    If point a was (0, 0) and point b was (0, 15) then the distance would be 15 and therefore too long. What I would like to do is change point b to (0, 10). I know that b would be (0, 10) because the two points have at least one of the same planes(in this case the vertical plane).

    However, how would I get the point if point a was (54, 126) and point b was (105, 220) and I wanted to keep the maximum distance as 15?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Restrict Line Distance

    My man Pythagoras can help you out. It's simple trigonometry. You can easily calculate the lengths of all three sides of the right triangle described by those two points so you can thus calculate the other two angles within that triangle. Those angles and a different hypotenuse length will allow you to calculate the corresponding lengths of the other two sides.

  3. #3
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Restrict Line Distance

    Let:
    Code:
      P1 = (X1, Y1)
      P2 = (X2, Y2)
      ΔX = X2 - X1
      ΔY = Y2 - Y1
      Distance (R) = Sqrt( ΔX^2 + ΔY^2)
    To find a point on the same line defined by P1 and P2, but at distance (r) from P1, you can use the proportionality of similar triangles to calculate the position of this point.

    Code:
      P(r) =  (X1 + ΔX (r/R),  Y1 + ΔY (r/R))

  4. #4

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Restrict Line Distance

    Sorry for the late reply, I took a step back from this project for a while.

    Let me ask you this, rather than heavily relying on math. Am I able to get all the points of a line and just iterate to either: A) The distance allowed or B) The end point of the line, whichever is less?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Restrict Line Distance

    Quote Originally Posted by dday9 View Post
    Sorry for the late reply, I took a step back from this project for a while.

    Let me ask you this, rather than heavily relying on math. Am I able to get all the points of a line and just iterate to either: A) The distance allowed or B) The end point of the line, whichever is less?

    I don't know where you think these intervening points and their corresponding distances from the starting point are going to magically materialize from. A line can be defined by two points, but it is infinite in length and there exists an infinite number of points between any two points on said line.

    Even if they did exist, why would you iterate over them (and at what resolution?) when you can directly compute the needed information? This is in no way a complicated calculation.

    Here is an implementation of what I think your are seeking. You give it start-point, a proposed end-point, and a limiting distance from the start-point and it will return either the proposed end-point or a point on the line defined by the start and end points as the specified maximum distance.

    Code:
    Public Shared Function MaxDistanceEndPoint(startPoint As PointF, proposedEndPoint As PointF, maxDistance As Double) As PointF
       Dim ret As PointF = proposedEndPoint
       Dim ΔX As Double = proposedEndPoint.X - startPoint.X
       Dim ΔY As Double = proposedEndPoint.Y - startPoint.Y
       Dim distance As Double = Math.Sqrt((ΔX * ΔX) + (ΔY * ΔY))
       If distance > maxDistance Then
          Dim distanceRatio As Double = maxDistance / distance
          ret = New PointF(CSng(startPoint.X + (ΔX * distanceRatio)), CSng(startPoint.Y + (ΔY * distanceRatio)))
       End If
       Return ret
    End Function

  6. #6
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,758

    Re: Restrict Line Distance

    you can, but it not as good a solution. Besides, the math is not difficult when approached properly.

    Because you have similar triangles, you can write
    x/ΔX = y/ΔX =c/Distance where (x y c) are the legs of the triangle you want and (ΔX ΔY Distance) are the corresponding legs in the triangle you have.


    Since you know ΔX, ΔX and Distance (from TnT's formula above) AND you know how long the distance should be (lets say 10), this all reduces to 2 equations.

    x/ΔX = 10/Distance --> x = 10 * ΔX / Distance

    and

    y/ΔY = 10/Distance --> y = 10 * ΔY / Distance

    x and y are now the lengths of the new triangle, so to find the new (x,y) coordinate, add the x and y values to the first point....

    if P1 was (34,65) the new P2 would be (34 +x, 65 + y)

    I hope my math is right and you can follow it. It's a lot better solution that figuring out all the points and iterating.
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Restrict Line Distance

    Quote Originally Posted by TnTinMN View Post

    I don't know where you think these intervening points and their corresponding distances from the starting point are going to magically materialize from. A line can be defined by two points, but it is infinite in length and there exists an infinite number of points between any two points on said line.
    ...
    While I agree that a little math is the correct approach, he did say he was using a graphic path, which I took to mean a Drawing2D.GraphicsPath object, which you can iterate through the points that make up the path. While you can iterate through the points, you still need the math to determine the distance traveled so it isn't going to save you anything and is going to be much more complicated than just using a function like TnTinMn posted to restrict the endpoint of the line segment before adding it to the path.
    If you added a too long line to a path, and then iterated the points, you would still need to then eliminate the extra points from the array (or collection) in the path, so seems really a bad way to approach the problem.

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