PDA

View Full Version : math problem (rotating points)


najamd
10-11-2006, 06:19 PM
Hey Guys,
Attached image, is a problem I’m facing and I’ve not been able to figure it out as its been a while since I touched this stuff.

I'm copying Dot A and making Dot B at 20 degrees from the 0 point. What is the shortest way to find the solution to where dot B should be? Shortest I mean least amount of flash CPU cycles as I’m using this in a heavy application.

Thanks allot in advance,


ND

senocular
10-11-2006, 06:41 PM
Its pretty simple:
ca = Math.cos(angle); // angle in radians
sa = Math.sin(angle);
newx = origx*ca - origy*sa;
newy = origx*sa + origy*ca;

najamd
10-11-2006, 07:10 PM
Thanks senocular!, Getting some odd result from that... attached is fla...

Its pretty simple:
ca = Math.cos(angle); // angle in radians
sa = Math.cos(angle);
newx = origx*ca - origy*sa;
newy = origx*sa + origy*ca;

senocular
10-11-2006, 07:33 PM
woops, I wrote cos for both, the second should be sin. I updated the original post

najamd
10-11-2006, 07:40 PM
Still not coming out right..

najamd
10-11-2006, 07:53 PM
Below is the code and it is run in a movie clip that is aligned to the bottom left of the root movie clip.

//
//c = Math.sqrt((dot._x*dot._x)+(dot._y*dot._y));
trace("CurX:"+dot._x);
trace("CurY:"+dot._y);
//
ca = Math.cos(20); // angle in radians
sa = Math.sin(20);
newx = dot._x*ca - dot._y*sa;
newy = dot._x*sa + dot._y*ca;
//
duplicateMovieClip(dot, "dot2", this.getNextHighestDepth());
dot2._x = newx;
dot2._y = newy;
//
trace("newX:"+dot2._x);
trace("newY:"+dot2._y);

senocular
10-11-2006, 09:32 PM
you're using 20 for the angle... thats not radians. 20 degrees is 20*Math.PI/180 radians

najamd
10-11-2006, 09:40 PM
Yeah, i just noticed that.. :p
Thanks senocular!, your help is very much appreciated,

ND

najamd
10-11-2006, 09:47 PM
One thing, I posted on a math forum this problem and a person there told me, I can go with a way geometry way or the matrix way...

Below is what he had posted for the matrix way... Can we do this kind of math in flash and if so would this be faster or slower for flash?..


Or you can use 2x2 matrix to rotate points about the orgin.

(cos(-20) -sin(-20))
(sin(-20) cos(-20))

senocular
10-11-2006, 10:29 PM
najamd, its the same exact code, just disguised in a different structure. Look at the position of the use of cos and sin and the location of the -.