Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Geometry - how to find points on line perpendicular/parallel to given line?

This thread is locked; no one can reply to it. rss feed Print
Geometry - how to find points on line perpendicular/parallel to given line?
spacepionier
Member #11,917
May 2010

I'm having trouble sorting a simple concept for some reason.
Basically I have a line element AB (as indicated on the attached picture) and I want to write a code which will calculate points L,M,N (Coordinates) so I could draw local axes of the element. Axis Y always will be perpendicular to element AB and axis X will be always parallel to element AB.

known:
point A(x1,y1)
point B(x2,y2)
point c(xm,ym) midpoint of the line AB
distance d (distance from the line AB to point M)
we can set as well the lengts of the axes so we know the distance from point M to N and from M to L

I was trying first to calculate point M from the distance equation unfortunately with no luck. Could you please help me and post a code in any programming language ( I will try to convert it to C# or VB.net then) or give me some hints or links where I could read about the soultion and refresh my highschool math :)

Thank you in Advance!

Jonatan Hedborg
Member #4,886
July 2004
avatar

I'm sure there easier ways to do this, but I would probably use sin,cos and atan2;
(pseudo code, obviously untested. Might need to change the orders in atan2... Also requires some vector class with overloaded operators ;))

ab_angle = atan2(a.y-b.y, a.x-b.x);
m.x = cos(ab_angle-M_PI/2);
m.y = sin(ab_angle-M_PI/2);
l = m * lm_length;
n.x = cos(ab_angle);
n.y = sin(ab_angle);
n *= nm_length;
m *= d;
m += c;
l += m;
n += m;

EDIT: Obviously there are better solutions below

Evert
Member #794
November 2000
avatar

Probably the easiest way to do this is to follow the Gram-Schmidt process.

EDIT: oh, looks like this is 2D? In that case it's much easier to find a vector that is perpendicular to the direction vector you already have. Those two vectors then span the space and if you express your coordinates with respect to those vectors you should be good.

EDIT2: Basically, what SiegeLord said below.

verthex
Member #11,340
September 2009
avatar

#SelectExpand
1 2float Ax, Ay, Bx, By,cXm,cYm,Lx, Ly, Mx, My, Nx, Ny, d; 3float greenx, greeny;// I think these two have to be given theres no other way to find L or N 4 5cXm = (AX - Bx)/2; 6cym = given; 7 8Mx = cXm; 9My = cym + d; 10 11Lx = Mx; 12Ly = My + greeny; 13 14Nx = cXm - x; 15Ny = d;

SiegeLord
Member #7,827
October 2006
avatar

First, you need to calculate the unit vector perpendicular to the segment AB. You do that by simply rotating the AB vector by 90 degrees (look up 2D rotation matrices on Wikipedia). In short, the vector that points in the direction cL (and cM) is (y2 - y1, x1 - x2). To get the point M, then, you just normalize that vector, multiply it by d and add it to the point c.

See how that works out for you... if you can't follow that I suggest brushing up on your basic linear algebra ;).

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

gnolam
Member #2,030
March 2002
avatar

Pfft. Trig functions are completely unnecessary. :)

<math>\vec u = \vec B - \vec A</math>
<math>\vec v = \{ -u_y, u_x \}</math>
<math>\vec M = \vec c + d \cdot \hat v</math>
<math>\vec L = \vec M + \hat v</math>
<math>\vec N = \vec M + \hat u</math>

[EDIT]
Thoroughly beaten. But I took my time trying to beat some sense into a.cc's LaTeX. ;)

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

SiegeLord
Member #7,827
October 2006
avatar

Way to go, everyone, for making the OP work for his answer :P.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Jonatan Hedborg
Member #4,886
July 2004
avatar

Woo, I actually learned something ;) Maybe I should dust of my old algebra book...

Evert
Member #794
November 2000
avatar

SiegeLord said:

Way to go, everyone, for making the OP work for his answer :P.

Well, he did ask for code or hints. Guess which one of those is easier to give. :P

spacepionier
Member #11,917
May 2010

Thank you very much for quick reply..

Yes, it would be good to get a code sample but looking on all replies I will have to find my old Linear Algebra book :)

gnolam: Would you mind to give some descripion to attached equations?

SiegeLord: will I be able to find coordinates of those M,L,N points using method you suggested?

I found a similar post on the forum
http://www.allegro.cc/forums/thread/589720 which I think will be helpful.

Could you please guys point the links from where I could try to sort out this issue ? some theory.

Thanks very much again for all replies!

gnolam
Member #2,030
March 2002
avatar

u: the vector from A to B.
v: counter-clockwise perpendicular vector to u. A 2D vector (x0, y0) rotated 90° counter-clockwise becomes (-y0, x0). You can derive this through rotation matrices, or just by drawing it on a piece of graph paper. :)

Hats denote unit vectors.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

spacepionier
Member #11,917
May 2010

Finally got it :) I have just modified slightly equations for L and N vectors from gnolam equations to get a specified length for x and y axes.. :D

Thank you all for the input!

-----------------------------------------------------------------------------------

EDIT: Just one quick question..I have all equations in place and my local axes are always parpendicular/parallel to the element, but depending on how I draw element - from left to right or from right to left I am getting axes above or below element. would it be possible to ammend those equations so that my local axes would be always above element and y axis would be always pointing upwards and x axis would be pointing in the same direction as element ie if element is drawn from right to left my X axis would go from 0,0 (local) to the left. I have attached a screen shot from my excel calculation with graph showing local axes below element because of the direction of element (from right to left)

I am sorry for edit but culd not reply..

Thank you in advance.

----------------------------------------------------------------------------------
EDIT 2: Never mind..this was a stupid question..I sorted out the issue.

Thomas Fjellstrom
Member #476
June 2000
avatar

Good to hear you got it fixed. And edits are ok here so long as you have a good excuse.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Go to: