Date: 03/27/2002 at 13:05:53 From: Maes Ludwig Subject: Coordinates or angle I want to be able to add two angles without using sines or cosines, because they aren't fast enough for programming games. I mean : o (x1/y1) o o (x2/y2) o + o o o o- - - - - - - - - (x,o) o- - - - - - - - - (x,o) /(x3/y3) / = / / / /------------------ (x,o) I just want to know how to rotate / or add the two angles knowing x1, y1, x2, y2 and also knowing that we mean the angle between (x/y) and the y-axis. Ludwig
Date: 03/27/2002 at 22:51:25 From: Doctor Peterson Subject: Re: Coordinates or angle Hi, Ludwig. For any kind of graphic programming, it is extremely useful to learn to use vectors and matrices. That is how I would approach this. I'm not sure whether you really meant what you said about "the angle between (x/y) and the y-axis"; in your pictures you appear to be using the angle between a point (x1,y1) and the x-axis. I will go by the pictures. The angle between the x-axis and the vector (a,b) can be seen as a rotation from (1,0) to (a,b). This can be written as a matrix multiplication (x') (a -b) (x) (y') = (b a) (y) which is just a short way to write x' = ax - by y' = bx + ay Notice that this takes (1,0) to (a,b), and (0,1) to (-b, a). This is not strictly a rotation; it also multiplies a vector by the length of (a,b). But you only care about the direction, so that doesn't matter. So to add two angles, you just have to do two rotations, represented by (a,b) and (c,d), in a row: (x') (c -d) (a -b) (x) (ac-bd -ad-bc) (x) (y') = (d c) (b a) (y) = (ad+bc ac-bd) (y) x' = (ac-bd)x - (ad+bc)y y' = (ad+bc)x + (ac-bd)y This will rotate the vector (1,0) to (ac-bd, ad+bc), which is therefore the direction you are looking for. In terms of your (x1,y1) and (x2,y2), this is x3 = x1 x2 - y1 y2 y3 = x1 y2 + y1 x2 As an example, if (x1,y1) = (1,1) and (x2,y2) = (1,1), forming two 45 degree angles, then x3 = 1*1 - 1*1 = 0 y3 = 1*1 + 1*1 = 2 giving a 90 degree angle represented by (0,2). - Doctor Peterson, The Math Forum http://mathforum.org/dr.math/
Search the Dr. Math Library:
Ask Dr. MathTM
© 1994- The Math Forum at NCTM. All rights reserved.