Sign in to follow this  

2D - how to get a parallel line with an orthogonal distance?

This topic is 4071 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 at all! thx for answering my previous questions. No I got a simple problem. I want to draw a parallel line with an orthogonal distance in 2D space. here is a picture for an example: http://dubtissm.cabspace.com/img/code/line.gif So I know I could calculate the angle of the given line from the horizon, add 90° to the angle, and get the point above P1 via x=sin(angle+90)*distance+P1.x y=cos(angle+90)*distance+P1.y But for calculating the angle of the given line, I need the acos or asin function. These are missing on my platform. Hopefully someone could help me ;)

Share this post


Link to post
Share on other sites
An easy solution would be to use vectors.

1. Calculate the vector between the two endpoints of the line: v1 = p1-p0
2. Calculate an orthogonal vector by exchanging the x and y value and negate one component
3. Normalize the orthogonal vector
4. Multiply it with your distance and add it to both enpoints to get the endpoints of your new line

Example:

P1(2,2), P2(10,6), dist = 2

V1 = P2 - P1 = (8,4)

Orthogonal vector V2 = (-4,8) or V2 = (4,-8) depending on your offset direction

Normalize V2 = (-4,8)/len(V2) = (-4,8)/sqrt(-4²+8²) = (-4,8)/8.94 = (-0.45,0.89)

P1' = P1 + V2 * dist = (2,2) + (-0.9,1.78) = (1.1,3.78)
P2' likewise

[Edited by - Quak on October 18, 2006 12:04:05 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Quak
An easy solution would be to use vectors.

1. Calculate the vector between the two endpoints of the line: v1 = p1-p0
2. Calculate an orthogonal vector by exchanging the x and y value and negate one component
3. Normalize the orthogonal vector
4. Multiply it with your distance and add it to both enpoints to get the endpoints of your new line

Example:

P1(2,2), P2(10,6), dist = 2

V1 = P2 - P1 = (8,4)

Orthogonal vector V2 = (-4,8) or V2 = (4,-8) depending on your offset direction

Normalize V2 = (-4,8)/len(V2) = (-4,8)/sqrt(-4²+8²) = (-4,8)/8.94 = (-0.45,0.89)

P1' = P1 + V2 * dist = (2,2) + (-0.9,1.78) = (1.1,3.78)
P2' likewise


Thx Quak! yes this will work.
great solution! Seems that there's no faster one.

Share this post


Link to post
Share on other sites

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