Distance from a point to a line
Asked Answered
M

4

1

I have created a class "Point" and i want to calculate the shortest distance between a given point and a line ( characterized by 2 other points ), all points are known. I tried to use this formula : |Ax+By+C| / sqrt(A^2+B^2) , but i messed up and got more confused by the minute (mostly because of math formulas :( )...

I did find some sites where people asked this question too, but it either was not for Python or it was in a 3D system not 2D ...

​​
Below is my class :

class Point:
        def __init__(self,initx,inity):
            self.x = initx
            self.y = inity
        def getX(self):
            return self.x
        def getY(self):
            return self.y
        def __str__(self):
            return "x=" + str(self.x) + ", y=" + str(self.y)
        def distance_from_point(self,the_other_point):
            dx = the_other_point.getX() - self.x
            dy = the_other_point.getY() - self.y
        def slope(self,other_point):
            if self.x - other_point.getX() == 0 :
                return 0
            else:
                panta = (self.y - other_point.getY())/ (self.x - other_point.getX())
                return panta

Can someone help me write a separate function or a method that does what i want ? I tried for 2 hours and I can't figure it out ...

Mercorr answered 5/12, 2016 at 9:0 Comment(5)
Do you mean an infinite line that intersects those two points, or a line that stretches between them but doesn't extend further?Blasius
I'm voting to close this question as off-topic because it is not a programming-related question. Try math.stackexchange.comKazachok
@EliKorvigo : Yo, I found the mathematic formulas too, but the problem was to translate it into python . Also anyone who wants to calculate the area of a triangle and the the area of any quadrilateral ( by breaking it into 2 triangles ) - what i am gonna do next - needs this .Mercorr
Simpler one-line solution answered here already: https://mcmap.net/q/363645/-distance-between-point-and-a-line-from-two-pointsEngstrom
Simpler on-line solution already answered for this question: #39840530Engstrom
P
6

You should be able to use this formula from the points directly. So, you'd have something like:

import math

class Point:
    def distance_to_line(self, p1, p2):
        x_diff = p2.x - p1.x
        y_diff = p2.y - p1.y
        num = abs(y_diff*self.x - x_diff*self.y + p2.x*p1.y - p2.y*p1.x)
        den = math.sqrt(y_diff**2 + x_diff**2)
        return num / den
Popham answered 5/12, 2016 at 9:7 Comment(7)
If this is a method , shouldn't there be a self in the parameters?Mercorr
Anyway,after i put that in there it works just fine. Thank you a lot !Mercorr
Er, and the last line should have den not dem... I swear I know what I'm doing.Popham
yeah, I corrected that too, but did not want to point that out too so i don't sound like a smart-ass after u helped me XDMercorr
Pretty sure this is wrong... If your line is <100,0,0>, <200,0,0> and your point is at <0,0,0>, the answer should be 100, not 0... If you only care about the direction and not the length of the line, why store it as two points to begin with, instead of as a point and a [unit] vector?Blasius
The way you write the function depends on what data you have to deal with, and it seems ROBlackSnail has a bunch of points. The way 'line' is interpreted also depends. In this case, it appears to be the infinite line defined by two points, in which case the answer should be 0.Popham
yeah, I think I did not expressed myself well ... It was distance from a line to a SEGMENT ( delimited by the other 2 given points ), because ,after that , i want to calculate the area of a triangle .Mercorr
Z
0

The distance formula between two points is Distance =sqrt((x2−x1)^2+(y2−y1)^2). And the formula to calculate slope is slope = (y2 - y1) / (x2 - x1).

so below is a simple method to calculate the distance

def distance_from_other_point(self, other_point):
    return math.sqrt( ( other_point.getX() - self.getX() )**2 + ( other_point.getY() - self.getY() )**2 )

def slope(self, otehr_point):
   return ( other_point.getY() - self.getY() )*1.0 / ( other_point.getX() - self.getX() )

In the second method, slope, I multiplied with 1.0 so that result will be in float. Note - I used the syntax of python 2.7.6 though hopefully, it will work in python 3.x as well.

Zalea answered 5/12, 2016 at 9:13 Comment(1)
I think i copied wrong the distance_from_other_point method XD It was : def distance_from_point(self,the_other_point): dx = the_other_point.getX() - self.x dy = the_other_point.getY() - self.y return sqrt(dx ** 2 + dy ** 2)Mercorr
W
0

You can install FastLine via pip and then use it in this way:

from FastLine import Line
# define a line by two points
l1 = Line(p1=(0,0), p2=(10,10))
# or define a line by slope and intercept
l2 = Line(m=0.5, b=-1)

# compute distance
d1 = l1.distance_to((20,50))
# returns 21.213203435596427
d2 = l2.distance_to((-15,17))
# returns 22.807893370497855
Waterish answered 22/1, 2022 at 17:32 Comment(0)
B
0

enter image description here

It is

Q-P0 = α (P1-P0) + β k×(P1-P0)

where k×(P1-P0) is the vector multiplication between the z versor and the position vector.

Writing the above equation as a scalar system, we have

ΔX = α dx - β dy
ΔY = α dy + β dx

solving for α, β

(dx²+dy²) α = + ΔX dx + ΔY dy
(dx²+dy²) β = - ΔX dy + ΔY dx

Because (dx²+dy²) = |P1-P0|²

                        ΔY dx - ΔX dy
β |P1-P0| = distance = ---------------
                            |P1-P0|

Of course, our result is equivalent to (P1-P0)×(Q-P0)/|P1-P0|.

Final remark, the distance of Q from (P1-P0) is oriented, and maybe you need the absolute value of the distance.

Brucie answered 2/12, 2022 at 13:5 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.