Sign in to follow this  

calculating points on an intersecting line

This topic is 2956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
Share on other sites
Maybe this?


//these define the triangle shape of the arrow head
const float headLength = #;
const float halfHeadWidth = #;

//draw an arrow head at ptB
Vector lineSeg = Normalize(ptB - ptA);
Vector linePerp = Perpindicular(lineSeg);

Vector ptC = ptB - lineSeg * headLength;
Vector arrowCorners[2] = { ptC + linePerp * halfHeadWidth
, ptC - linePerp * halfHeadWidth };

//draw complete arrow
Line(ptA, ptC);
Line(ptB, arrowCorners[0]);
Line(ptB, arrowCorners[1]);
Line(arrowCorners[0], arrowCorners[1]);

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 2956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this