fatz
fatz

Reputation: 698

How to find the X coordinate of a point on a line given another point of the line and the 2 points on a perpendicular line?

I'm writing a python app to select rectangular areas, when the user has set 2 points (the length of the rectangle), the third and 4th points are constrained in forming a rectangle, where the width of the rectangle is given by the Y difference of the mouse cursor and the last point.

Here's a quick picture to explain, I'm looking for the X coordinate of the point C.

enter image description here

I know :

I'm not sure how to tackle this... using vectors ? I'm using numpy in my project.

Upvotes: 1

Views: 2308

Answers (1)

P. Camilleri
P. Camilleri

Reputation: 13218

This is more of a math issue than a numpy issue.

The slope of (AB) is (y_a - y_b)/ (a - b). So the slope of any perpendicular to (AB) is p=(b-a)/(y_a-y_b) (opposite of the inverse of the original slope).

From here it is easy to determine the equation of the perpendicular to (AB) passing through B : y-y_b=p*(x-x_b). And substitute y_c to y to find x_c

There is an issue (division by zero) if (AB) is horizontal (0 slope). In that case, x_c is just x_b (all of the points on (BC) have same x coordinate)

Upvotes: 3

Related Questions