[Flashcoders] Math: Triangle rotations around an origin

2006-11-29 Thread Andreas R
This is based on Danny Codicek's Mathematics  Physics for programmers, 
and i'm feeling very stupid right now.


From what i can tell, the book describes the function as such:
rotatedX = 
Math.sqrt(originalx*originalx+originaly*originaly)*Math.sin(angle-Math.atan2(originaly,originalx))

and the same for rotatedY

My implementation is this:

import flash.geom.Point;
function Triangle(p1:Point,p2:Point,p3:Point){
   this.p1 = p1;
   this.p2 = p2;
   this.p3 = p3;
   this.update = function(){
   this.clip = _root.container.createEmptyMovieClip(triangle,1);
   with(this.clip){
   beginFill(0xFF);
   moveTo(p1.x,p1.y);
   lineTo(p2.x,p2.y);
   lineTo(p3.x,p3.y);
   lineTo(p1.x,p1.y);
   endFill();
   }
   }
   this.update();
}

function rotateTriangle(triangle:Object,angle:Number){
   var rad:Number = angle*(Math.PI/180);
   for(var i = 3;i--;){
   var p = triangle[p+(i+1)];
   var py:Number = Math.sqrt(p.x*p.x+p.y*p.y);
   var np:Point = new Point();
   np.x = dist*Math.cos(rad-Math.atan2(p.y,p.x));
   np.y = dist*Math.sin(rad-Math.atan2(p.y,p.x));
   triangle[p+(i+1)].x = np.x;
   triangle[p+(i+1)].y = np.y;
   }
   triangle.update();
}

var t1:Object = new Triangle(new Point(10,10),new Point(40,40),new 
Point(10,40));

rotateTriangle(t1,45);

This isn't working very well: I'm not sure wether angle should be 
angles or radians, since either gives off-kilter results and radians 
gives the least freakishness, but what happens is on every other update 
the triangle gets mirrored rather than rotated.


Could someone with stronger cerebral capacity than me have a look at my 
function, figure it out and explain where i went wrong? The more i look 
at it the less i understand why it flips every other update.


- A
___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


Re: [Flashcoders] Math: Triangle rotations around an origin

2006-11-29 Thread Mark Winterhalder

Hi Andreas,

you need the initial angle, which is Math.atan2( mouseY, mouseX ).
Then the angle while rotating, which is the same minus the initial
angle. That's your current angle -- like all angles, in radians.

to rotate points by this angle, just do this:
var newX = Math.cos( angle ) * originalX - Math.sin( angle ) * originalY;
var newY = Math.sin( angle ) * originalX + Math.cos( angle ) * originalY;

HTH,
Mark


On 11/29/06, Andreas R [EMAIL PROTECTED] wrote:

This is based on Danny Codicek's Mathematics  Physics for programmers,
and i'm feeling very stupid right now.

 From what i can tell, the book describes the function as such:
rotatedX =
Math.sqrt(originalx*originalx+originaly*originaly)*Math.sin(angle-Math.atan2(originaly,originalx))
and the same for rotatedY

My implementation is this:

import flash.geom.Point;
function Triangle(p1:Point,p2:Point,p3:Point){
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.update = function(){
this.clip = _root.container.createEmptyMovieClip(triangle,1);
with(this.clip){
beginFill(0xFF);
moveTo(p1.x,p1.y);
lineTo(p2.x,p2.y);
lineTo(p3.x,p3.y);
lineTo(p1.x,p1.y);
endFill();
}
}
this.update();
}

function rotateTriangle(triangle:Object,angle:Number){
var rad:Number = angle*(Math.PI/180);
for(var i = 3;i--;){
var p = triangle[p+(i+1)];
var py:Number = Math.sqrt(p.x*p.x+p.y*p.y);
var np:Point = new Point();
np.x = dist*Math.cos(rad-Math.atan2(p.y,p.x));
np.y = dist*Math.sin(rad-Math.atan2(p.y,p.x));
triangle[p+(i+1)].x = np.x;
triangle[p+(i+1)].y = np.y;
}
triangle.update();
}

var t1:Object = new Triangle(new Point(10,10),new Point(40,40),new
Point(10,40));
rotateTriangle(t1,45);

This isn't working very well: I'm not sure wether angle should be
angles or radians, since either gives off-kilter results and radians
gives the least freakishness, but what happens is on every other update
the triangle gets mirrored rather than rotated.

Could someone with stronger cerebral capacity than me have a look at my
function, figure it out and explain where i went wrong? The more i look
at it the less i understand why it flips every other update.

- A
___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com