calculating points on an intersecting line

Started by
4 comments, last by haegarr 14 years, 5 months ago
I have been unable to find a solution to this problem which means I have probably over-engineered it. Basically, I'm trying to draw an arrow tip at the end of a line. Here is how I thought I would go about it: 1. Given 2 points, I know: a. The Slope b. The Distance c. The equation for a perpendicular line at the endpoint (invert the slope and solve for c by plugging in one of our two known points) This is where I have no clue how to calculate what is needed: 2. Find a point some distance further than the ending point and draw lines back to the perpendicular line and equal distance apart OR 2. Find a point on the perpendicular line and then find at what point it intersects the original line given some arbitrary angle. Thanks for any help math gurus
Check out my current project on Kickstarter: Genegrafter
Advertisement
Maybe this?

//these define the triangle shape of the arrow headconst float headLength = #;const float halfHeadWidth = #;//draw an arrow head at ptBVector lineSeg = Normalize(ptB - ptA);Vector linePerp = Perpindicular(lineSeg);Vector ptC = ptB - lineSeg * headLength;Vector arrowCorners[2] = { ptC + linePerp * halfHeadWidth                         , ptC - linePerp * halfHeadWidth };//draw complete arrowLine(ptA, ptC);Line(ptB, arrowCorners[0]);Line(ptB, arrowCorners[1]);Line(arrowCorners[0], arrowCorners[1]);
If I understand correctly you have a line like this:


And want to get points for an arrow like this:


You should be able to calculate it like this:
n = (y1 - y0, x0 - x1)
l = |p1 - p0|

p2 = p1 + ((p1 - p0) * d) / l
p3 = p1 + (n * d) / l
p4 = p1 - (n * d) / l

Where d is some distance you want to move the points on the arrow from p1. If you want the arrow-tip at 45 degrees, make d twice as large for p2 as for p3 and p4.
That all makes sense but where I am getting tripped up is the finding n part. Let's say that I want each side of the arrow to be 5, how would I find the two points on the perpendicular line that were 5 units away from the endpoint?
Check out my current project on Kickstarter: Genegrafter
The equations I posted should do that, with d = 5. Calculating n like (y1 - y0, x0 - x1) gives it the same length as the line, and by dividing by the length l = |p1 - p0|, the offset is exactly d.
To clarify the mysterious n:

In 2D, there are 2 possibilities to calculate the so-called "perp vector" of a vector [dx,dy], both which are orthogonal to the original vector. These are
(1) [-dy,dx] w/ the proof: [dx,dy] . [-dy,dx] = -dx*dy + dy*dx == 0
(2) [dy,-dx] w/ the proof: [dx,dy] . [dy,-dx] = dx*dy - dy*dx == 0
The first one goes counter-clockwise, while the latter one goes clockwise w.r.t. the original vector. Both are suitable for the problem described in the OP.

This topic is closed to new replies.

Advertisement