Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you find a point at a given perpendicular distance from a line?

I have a line that I draw in a window and I let the user drag it around. So, my line is defined by two points: (x1,y1) and (x2,y2). But now I would like to draw "caps" at the end of my line, that is, short perpendicular lines at each of my end points. The caps should be N pixels in length.

Thus, to draw my "cap" line at end point (x1,y1), I need to find two points that form a perpendicular line and where each of its points are N/2 pixels away from the point (x1,y1).

So how do you calculate a point (x3,y3) given it needs to be at a perpendicular distance N/2 away from the end point (x1,y1) of a known line, i.e. the line defined by (x1,y1) and (x2,y2)?

like image 829
AZDean Avatar asked Sep 25 '08 15:09

AZDean


People also ask

How do you find a point on a line that is perpendicular to the line?

To find a line that's perpendicular to a line and goes through a particular point, use the point's coordinates for (x1, y1) in point slope form: y - y1 = m (x - x1). Then, calculate the "negative reciprocal" of the old line's slope and plug it in for m.

How do you find a point perpendicular to a point?

To find the perpendicular of a given line which also passes through a particular point (x, y), solve the equation y = (-1/m)x + b, substituting in the known values of m, x, and y to solve for b.

How do you find the perpendicular distance from a point to a line vector?

The perpendicular distance, 𝐷 , between a point 𝑃 ( 𝑥 , 𝑦 , 𝑧 )     and a line with direction vector ⃑ 𝑑 can be found by using the formula 𝐷 = ‖ ‖  𝐴 𝑃 × ⃑ 𝑑 ‖ ‖ ‖ ‖ ⃑ 𝑑 ‖ ‖ ,  where 𝐴 is any point on the line.

How do you find a point on a given line?

To find points on the line y = mx + b, choose x and solve the equation for y, or. choose y and solve for x.


2 Answers

You need to compute a unit vector that's perpendicular to the line segment. Avoid computing the slope because that can lead to divide by zero errors.

dx = x1-x2 dy = y1-y2 dist = sqrt(dx*dx + dy*dy) dx /= dist dy /= dist x3 = x1 + (N/2)*dy y3 = y1 - (N/2)*dx x4 = x1 - (N/2)*dy y4 = y1 + (N/2)*dx 
like image 135
David Nehme Avatar answered Oct 07 '22 17:10

David Nehme


You just evaluate the orthogonal versor and multiply by N/2

vx = x2-x1 vy = y2-y1 len = sqrt( vx*vx + vy*vy ) ux = -vy/len uy = vx/len  x3 = x1 + N/2 * ux Y3 = y1 + N/2 * uy  x4 = x1 - N/2 * ux Y4 = y1 - N/2 * uy 
like image 22
Giacomo Degli Esposti Avatar answered Oct 07 '22 18:10

Giacomo Degli Esposti