D5145c421cd25af6fa577c15219add90

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 !

Aacfa176a8d73ca75b90b6375151765a

paul.wilkins.myopenid.com

March 5, 2010, March 05, 2010 22:04, permalink

No rating. Login to rate!

duplicate post

Aacfa176a8d73ca75b90b6375151765a

paul.wilkins.myopenid.com

March 5, 2010, March 05, 2010 22:06, permalink

3 ratings. Login to rate!
1
2
3
4
5
6
this.rotate = function (antiClockwise) {
  var dir = (antiClockwise) ? -1 : 1;
  temp_x = this.x;
  this.x = dir * this.y;
  this.y = -dir * temp_x;
}
D5145c421cd25af6fa577c15219add90

Nathan

March 8, 2010, March 08, 2010 08:51, permalink

No rating. Login to rate!

Great. It passes my unit tests and it looks nice and simple, so I'm happy.

34db50b7ce2e115afadf5a765b950739

Thomas Salvador

March 9, 2010, March 09, 2010 20:57, permalink

No rating. Login to rate!

hi,

i suggest to shorten the swap.

regards,
thomas.

1
2
3
4
5
6
7
8
9
10
11
12
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];
}
B9fe8fcd15cc5a5d243d3d2412ea856c

Peter Tchernev

June 24, 2010, June 24, 2010 12:09, permalink

No rating. Login to rate!

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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();
}

Your refactoring





Format Copy from initial code

or Cancel