http://GameProgrammer.Com

Programming

GP Mailing List
     Thread Index
     Date Index

ATXGPSIG List
     Thread Index
     Date Index

Google
>

Home

Wise2Food



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

GP: Flipping a point across a line: The answer



Ok peeps,

I have now found out how to flip a point across a line formed by two other
points, curtousy of My Dad - fantastic guy.
Here is the pseudo-code...

// declare variables to use, integers are Ok.
declare A, B, C, D, E, Y, M, ZZ, P

// get points (x and y coords) of line end points
A = GetPoint()
B = GetPoint()

// get point to reflect
C = GetPoint()

// so, from here we want to get `D`, which will also have x and y values...
// this bit calculates where a line from C to A,B at a right angle will
// intersect...
Y = (B.y - A.y) / (B.x - A.x)
E.x = C.x + (C.y - A.y) * Y
ZZ = 1 + Y * Y
M = (E.x - A.x) / ZZ
// this calculates D, which is where the right-angle line intersects A,B
D.y = A.y + M * Y
D.x = A.x + M

// this bit flips C across the point D, which is on line A,B, to get E
P.x = D.x + (D.x - C.x)
P.y = D.y - (C.y - D.y)

// so, if you wanted to draw all of that one screen...
DrawLine( A.x, A.y , B.x, B.y )		// line to flip across
DrawPoint( C.x, C.y )			// point to flip
DrawPoint( P.x, P.y )			// flipped point


I hope this helps other people as much as it will help me.  If u like I can
put out a little Java1.0.2 applet which demo's this.

While wer'e on the subject of demo code, would anyone like to take a look
at my (group's) web site where code will be appearing soon...though it's a
little early right now;

	http://www.eudoxus.freeuk.com/

C ya.


Regards,

Matt. W.
-- 
***  eudoxus@freeuk.com  ***  http://members.xoom.com/eudoxusM/
                 mwebster@apsoft.co.uk

=================================================================
To SUBSCRIBE or UNSUBSCRIBE please visit
http://gameprogrammer.com/mailinglist.html