tags:

views:

491

answers:

4

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.

http://rauros.net/files/caps.png

+3  A: 

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.

nlucaroni
But you need to solve a system of equations involving distance to turn the point-slope form into a line segment. I was hoping for a solved system solution
basszero
yeah, i missed a step, you need to find the x-coordinate of the point-slope formula first, where the trig comes in.
nlucaroni
+4  A: 

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 ]
Kragen
The plain english vector explanation is exactly what I needed to put the pieces together in my head. Thanks! Shame you didn't get many upvotes
basszero
A: 

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.

trashgod
+3  A: 

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.

Dov