this.rotate = function (antiClockwise)
{
if (antiClockwise)
{
x = -1*this.y;
y = this.x;
this.x = x;
this.y = y;
}
else
{
x = this.y;
y = -1*this.x;
this.x = x;
this.y = y;
}
}
Refactorings
No refactoring yet !
paul.wilkins.myopenid.com
March 5, 2010, March 05, 2010 22:06, permalink
this.rotate = function (antiClockwise) {
var dir = (antiClockwise) ? -1 : 1;
temp_x = this.x;
this.x = dir * this.y;
this.y = -dir * temp_x;
}
Nathan
March 8, 2010, March 08, 2010 08:51, permalink
Great. It passes my unit tests and it looks nice and simple, so I'm happy.
Thomas Salvador
March 9, 2010, March 09, 2010 20:57, permalink
hi,
i suggest to shorten the swap.
regards,
thomas.
this.rotate = function (antiClockwise) {
var dir = (antiClockwise) ? -1 : 1;
[this.x, this.y] = [dir * this.y, -dir * this.x];
}
or even
this.rotate = function (antiClockwise) {
[this.x, this.y] = (antiClockwise)
? [-this.y, this.x]
: [ this.y,-this.x];
}
Peter Tchernev
June 24, 2010, June 24, 2010 12:09, permalink
I am not sure if this is a direction that you want to go, since it is actually more code. But I would do the transformations in matrix-form to allow you to add other effects more easily. So it really depends on wether you are expanding in that direction or not. Also, the shortform if's will just make the code more difficult to understand for the next guy.
this.transform = function(m00,m01,m10,m11){
var x=m00*this.x+m01*this.y;
var y=m10*this.x+m11*this.y;
this.x=x;
this.y=y;
}
this.rotate90CCW = function(){
transform(0,-1,1,0);
}
this.rotate90CW = function(){
transform(0,1,-1,0);
}
// Add mirror, scale, rotate 180 etc
// For compatibility with your current API
this.rotate = function(antiClockwise){
if(antiClockwise)
rotate90CCW();
else
rotate90CW();
}
A nice and fun one here. I want to be able to rotate some co-ordinates either clockwise or anticlockwise around the 0,0 point. The temporary methods are a bit of an irritation, and it seems unnecessary to do the whole thing twice.