# mathHow to calculate end points of perpendicular line segments

I know the end points of a line segment and the distance/size of the perpendicular end caps I'd like to create but I need to calcuate the end points of the perpendicular line. I've been banging my head against the wall using either 45-45-90 triangles and dot products but I just can't seem to make it come together.

I know the points in blue and the distance to the points in red, I need to find the points in red.

Before marking as duplicate, I tried the answer posted in this question but it resulted in end caps which were always vertical.

If B1 is the blue point between the 2 red points, and B2 is the other blue point then the way to do this is:

• Find B1 - B2
• Normalise this vector
• Then scale this vector up by half the distance between the red points
• Rotate by 90 degrees
• Add this vector to B1 (this is R1)
• Subtract this vector from B1 (This is R2)

All of the above is fairly straightforward - the trickiest bit would be figuring out how to write it out in text!

This might be helpful though - matrix to rotate by 90 degrees:

[ 0  -1 ]
[ 1   0 ]


The easy way around this one is not to think in terms of slope m, but rather the change in x and y, which I call dx, dy (from the calculus notation).
The reason is for one thing, that dealing with a slope for a vertical line is infinite, and in any case, you don't need to use trig functions, this code will be faster and simpler.

dx = x2 - x1;
dy = y2 - y1;


I am assuming here that point 2 is the intersection of the desired line.

Ok, so the perpendicular line has a slope with the negative reciprocal of the first.
There are two ways to do that:

dx2 = -dy
dy2 = dx


or

dx2 = dy
dy2 = -dx


this corresponds to the two directions, one turning right, and the other left.

However, dx and dy are scaled to the length of the original line segment. Your perpendicular has a different length.

Here's the length between two points:

double length(double x1, double y1, double x2, double y2) {
return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}


Do what you want, to go to one side or the other, is:

double scale = length(whatever length you want to go)/sqrt(dx*dx+dy*dy);
double dx2 = -dy * scale;
double dy2 = dx * scale


and then the same again for the other side.
I just realized my example is somewhat c++, since I used sqrt, but the differences are trivial. Note that you can write the code more efficiently, combining the square roots.

You know the slope of the blue line, let's call it m. And a line perpendicular to the blue line will have slope -1/m.

to find the x-coordinate you need some trig, sine \theta = d / delta_x, where \theta is the angle of the blue line for the x-axis and d is the distance to one of the red points from the blue point. Then add/subtract delta_x to the x-coordinate of the blue point you want the line to be perpendicular to. Now you can use the point-slope formula to figure out the y coordinate.

I'd prefer the vector or matrix solutions suggested by Kragan and in your previous question, but you might also try a more basic approach: Assume a linear equation of the form y = mx + b. Use the two-point form to get the slope (m) of the line's equation. The perpendicular has slope -1/m. Use this new slope and the endpoint in the point–slope form to find any two points on the perpendicular. Naturally, you have to avoid m = 0.

Related Articles
• I have a line segment (great circle part) on earth. The line segment is defined by the coordinates of its ends. Obviously, two points define two line segments, so assume I am interested in the shorter one. I am given a third point, and I am looking f
• I am trying to figure out how to find the end points of a line. Lets say that I have a line that goes from (0,0) to (2,2) in a cartesian coordinate system and opencv sees the line using Probabilistic Hough Line Transform, how do I find those 2 points
• I need a basic function to find the shortest distance between a point and a line segment. Feel free to write the solution in any language you want; I can translate it into what I'm using (Javascript). EDIT: My line segment is defined by two endpoints
• I have a complicated problem and it involves an understanding of Maths I'm not confident with. Some slight context may help. I'm building a 3D train simulator for children and it will run in the browser using WebGL. I'm trying to create a network of
• I have a start point in 3D coordinates, e.g. (0,0,0). I have the direction I am pointing, represented by three angles - one for each angle of rotation (rotation in X, rotation in Y, rotation in Z) (for the sake of the example let's assume I'm one of
• G'day! When I know the slope and y-intercept of a line, I need to calculate an x-value that is 1 unit out from the line. For example, if pointA = (4,5), and I set a line going from it with 0 slope (and therefore 5 as the y-intercept), then the x valu
• Essentially, i want to find the equation of the ring that circles a point in space, this ring is perpendicular to a normal away from this point. I have a line, in the form of 2 points, L1, L2; I have the normal from L1->L2, N; I have the plane that i
• I'm searching for an algorithm to calculate the average distance between a point and a line segment in 3D. So given two points A(x1, y1, z1) and B(x2, y2, z2) that represent line segment AB, and a third point C(x3, y3, z3), what is the average distan
• I am trying to find a way to extend a line segment by a specific distance. For example if I have a line segment starting at 10,10 extending to 20,13 and I want to extend the length by by 3 how do I compute the new endpoint. I can get the length by sq
You Might Also Like
• FMS stands for Flash Media Server. I don't see a reason why these tags shouldn't be merged.
• It seems to me the fastest way to do a row/col subset of a data.table is to use the join and nomatch option. ...
• What I would like to do is find a more concise way of creating variables of empty lists that will be similar ...
• I am creating folders using php. When I use php's glob function on a directory it does not return anything. ...
• This question already has an answer here: The mysql extension is deprecated and will be removed in the futur ...
• I have some code that compiles in Visual C++ 2013, but am told it doesn't build in VC++2010 (as it uses a su ...
• I have an application, where I need to send both "static" arguments (shouldn't change) and "d ...
• A bright double room of good size available i ...
• CPS Property require 2 x two bedroom properti ...
• My name is Becky and I am 20 years old. Don't let this put you off! This will be my 3rd year of tutoring GCS ...