8 Replies Latest reply on Aug 17, 2007 10:28 AM by kglad

# can someone help me explain this code

hello, when i study this code, i can't understand some code , can someone help me to explain this codes ?
thanks very much!!!
• ###### 1. can someone help me explain this code
Its creating a 'method' for a Vector object (in the older style of class definition), giving it a rotate method with an argument in degrees.

It changes the Vector's existing x and y properties ( in the 'with(this)' part of the code) based on the angle that was passed.

I don't know if it gives the correct results because I didn't check the math/trig but that's the interpretation of what I see. What exactly do you want to have explained in more detail?

• ###### 2. Re: can someone help me explain this code
the math is correct.
• ###### 3. Re: can someone help me explain this code
i know this code mean,but i really want to know the four lines,like this:
var ca = Math.cos(angle * Math.PI / 180); //what's this mean?
var sa = Math.sin(angle * Math.PI / 180); //and this.
with(this) {
var rx = x* ca - y*sa; //and this;
var ry = x* sa + y*ca; // and this;
en ,thanks very much!!!
• ###### 4. Re: can someone help me explain this code
angle is in degrees and multiplying by pi and dividing by 180 converts those degrees to radians.

the last two lines give the values rx,ry of a point x,y after rotation by angle degrees.
• ###### 5. Re: can someone help me explain this code
thanks your reply, my question is that why use this method to acculate the x and y ,!!
that's what i want to konw!
thanks !!!
• ###### 6. Re: can someone help me explain this code
you would use the rotate method if you wanted to rotate a point.
• ###### 7. Re: can someone help me explain this code
ok，thanks very much
i think i will study the math again!!!
too bad that ,i can only choose one correct answer,
at last, i really thanks everyone who help me!
• ###### 8. Re: can someone help me explain this code
you're welcome.