0

in a current assignment we need to create a method, to rotate a given point, around a center point given, in the angle given, in CLOCK WISED offset. after searching through posts here, i thought i figured it out, but according to my teacher, something is wrong with this and i can't figure it out.

i was hoping maybe someone could help out pointing what the problem is.

this is my method

    /**
 * rotate this Point around a given enter point, with the given angle, in clock wised offset.
 * @param center - the center Point for rotation
 * @param angle - the size angle in degrees - to be rotate clock wised
 */
public void rotate(Point center, double angle){

    // convert the angle to radiant (to use with Math class methods). The negative sign is because, in trigonometry, default rotation is COUNTER-clock wised.
    double rad_angle = -Math.toRadians(angle); 

    // in order to rotate properly, we need to know the difference between the coordinates, to get a base for the rotation.
    double diff_x = this.x - center.X(); 
    double diff_y = this.y - center.Y(); 

    // calculate the rotation difference
    this.x = diff_x * Math.cos(rad_angle) - diff_y* Math.sin(rad_angle);  
    this.y = diff_x * Math.sin(rad_angle) + diff_y * Math.cos(rad_angle);

    // translate the differences back to this point
    this.x = this.x + center.X(); 
    this.y = this.y + center.Y();

    return;
}//rotate

thanks in advance.

Adiel
  • 1,203
  • 3
  • 18
  • 31
  • Have you tried passing it various test input values? What did you get, and what did you expect to get instead? – shoover Jan 13 '14 at 20:25
  • i think i've found what seems to be the problem - as example , if i rotate < -5.0 , 2.5 > in 90 degrees, instead of getting < 2.5 , 5.0 > i'm getting : <2.4999999999999996,5.0> i think its critical because in the assignment, this rotation must be precise. if i use Math.round it will round it to 2.0, which is not good for me. – Adiel Jan 13 '14 at 20:41
  • Doubles don't have exact values of 2.5. They are always approximations. http://stackoverflow.com/questions/3988821/comparison-of-float-and-double-variables – Liron Jan 13 '14 at 20:56
  • @Liron, i don't understand. why does doubles don't have exact value of 2.5? if i initialize " double a = 100.0 / 40.0; " for example, 'a' will be 2.5. or did you mean something else and i'm missing the point? – Adiel Jan 13 '14 at 21:38
  • @user3025555 Try it out. The way doubles (and all floating point numbers) work is that they are approximations of the real value. Try print out 10.0/4.0, 100.0/40.0, 1000.0/400.0 and see what you get. – Liron Jan 14 '14 at 07:47
  • The way to compare doubles for equality is to do `abs(double1 - double2) < 0.00001` or some other small value like that. – Liron Jan 14 '14 at 07:49
  • But either way, that's not your big problem. Have you tried it out with some known inputs and outputs to see what you get? Try with some simple things, like {-5,0} rotated around {0,0} by 90 degrees, should be {0,-5). Then try {-10,0} rotated around {-5,0} by 90 degrees should be {-5,-5} – Liron Jan 14 '14 at 09:07

1 Answers1

0
    angle += 0.2f * delta;
    position.x = target.x + (float) Math.sin(angle) * distanceFromCenter;
    position.y = target.y + (float) Math.cos(angle) * distanceFromCenter;

There's really nothing more to it.

Nine Magics
  • 637
  • 2
  • 8
  • 17