Jump to content
  • Advertisement
Sign in to follow this  
invictus

plotting crossing lines

This topic is 3923 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

Hi I have a small problem. I have 2 coordinates (x1,y1 and x2,y2). From these I was going to calculate the length between the points. This by itself is not problem. The problem is this: The distance between the points form a line of a given length. Along this line I want to calculate 10 (equally aligned) crossing lines that are 10 units long in each direction from the base line with a 90 degree angle. The lines are of no importance by themselves, but the x,y coordinates at both ends are. Can anyone help me out in the right direction here? Take a look in the following drawing: http://home.online.no/~bj-bakke/lines.png I am using this information in order to create a small utility to help he analyze images for use in biometric authentication (non-profit research project).

Share this post


Link to post
Share on other sites
Advertisement
To calculate the equally spaced points on the line, you can use linear interpolation. The formula is (x,y) = (x1,y1)(1 - t) + (x2,y2)(t), and you choose values for 't' in-between [0,1], inclusively. t = 0 is the first endpoint, and t = 1 is the second endpoint. So for 10 points, you could choose t = 0.05, 0.15, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75. 0.85, 0.95.

To determine the direction perpendicular to the line, you take the 2D cross-product (not surprisingly called 'perp') of the line's direction. This vector is (x2,y2)-(x1,y1), and the 'perp' operator is defined as perp(x,y) = (-y,x). You then normalize this vector and multiply it by the length you want in each direction (10 in this case). Then just add/subtract it onto each point you calculated in the first step (adding gives you the first line endpoint, subtracting gives you the second line endpoint).

Share this post


Link to post
Share on other sites
Here's how I'd do it. First, determine the distance between points, which you said yourself is trivial. Obtain the slope of the line between the points (y1 - y0) / (x1 - x0). Now, the slope of the new crossing lines is just the negative reciprocal of the slope you just calculated, since they are perpendicular.

Now we are going to need the point on the original line where the new one crosses (let's say we're doing the one closest to (x1,y1)) Just make the difference of the points a vector, scale it to 1/10 length, and add to the first point. You now know the point on the original line.

With m being the slope, the general equation for a line is y - y1 = m(x - x1) where the x1 and y1 is the point on the line just calculated. Repeat this for the rest of the crossing lines using an appropriate vector scale each time and you have all the lines.

To calculate the end points of the line segments that go out 10 units, we again use the point on the original line and the slope of the crossing line. Multiply the slope by the distance 10. The slope is the change in y / change in x. So just add this to the point on the line to go in one direction, and subtract it from the point on the line to go in the other. That should do it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!