How to compute distance of points to a line?

  • #1
53
6

Main Question or Discussion Point

I have computed a line from point A to point B by just subtracting the coordinates as below:

line = np.array (x2-x1, y2-y1)

I am not sure if I need to form the formula for this line first by computing the slope and intercept but I continued my code as below, slope being m and b being the intercept from the formula y=mx+b

slope = (y2-y1)/(x2-x1)
b = (y2-y1) - (x2-x1)*slope

Now I am not sure how I can calculate the distance of several x,y points to this line.

can anyone help? thanks.
 

Answers and Replies

  • #2
phinds
Science Advisor
Insights Author
Gold Member
2019 Award
15,907
5,589
Is this a homework problem?
 
  • #3
53
6
no this is a self project
 
  • #4
phinds
Science Advisor
Insights Author
Gold Member
2019 Award
15,907
5,589
OK. When you say "the distance of several x,y points to this line" I assume you mean the standard "shortest distance", not just any old distance. If that's the case then it should be clear that the distance from any point to any line L is going to be on a line that is perpendicular to L, yes?
 
  • #5
53
6
yes.. but just to be sure we are on the same page... say I have line L that I have computed as above. and I have x,y coordinates around this line or as you said that are perpendicular to this line and I now want to compute the distance of each x,y points to this line and take the point that has the maximum distance. I have attached the image for clarity. thanks
 

Attachments

  • #7
53
6
i tried this formula and implemented it in python, and it seems to work so this problem is solved.
 

Related Threads on How to compute distance of points to a line?

Replies
14
Views
2K
Replies
1
Views
522
Replies
8
Views
3K
Replies
5
Views
2K
Replies
18
Views
4K
Replies
4
Views
932
Replies
8
Views
30K
Replies
9
Views
15K
Replies
21
Views
1K
Replies
1
Views
384
Top