# calculating points on an intersecting line

## Recommended Posts

darkpegasus    269
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

##### Share on other sites
bzroom    647
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]);

##### Share on other sites
Erik Rufelt    5901
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.

##### Share on other sites
darkpegasus    269
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?

##### Share on other sites
Erik Rufelt    5901
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.

##### Share on other sites
haegarr    7372
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.