unaccurate rotation! What can I do?
in
Programming Questions
•
3 years ago
Hi!
I have a serious problem of rotation around a point :
It works but it is not enough accurate
I have this:
- public /*abstract*/ class Renderable {
- // variables
- protected float w,h, cx,cy;
- protected int rot;
- public Renderable() {
- cx=cy=0;
- w=200;
- h=100;
- }
- public void translate(float x, float y){
- this.cx += x;
- this.cy += y;
- }
- public void drawDecorationOn(PGraphics pg){
- pg.pushMatrix();
- pg.translate(cx, cy);
- pg.rotate((float) (rot*PApplet.PI/180.));
- pg.rect(-w/2, -h/2, w, h);
- pg.popMatrix();
- }
- public void rotateDeg(int deg){
- this.rot += deg;
- }
- public void rotateRad(double rad){
- rotateDeg((int)(rad*180./PApplet.PI));
- }
- public void rotateDeg(int deg, float pivotX, float pivotY){
- rotateRad((double)deg*PApplet.PI/180., pivotX, pivotY);
- }
- public void rotateRad(double rad, float pivotX, float pivotY){
- this.cx = (float) ( Math.cos(rad)*(this.cx-pivotX) - Math.sin(rad)*(this.cy-pivotY) + pivotX);
- this.cy = (float) ( Math.sin(rad)*(this.cx-pivotX) + Math.cos(rad)*(this.cy-pivotY) + pivotY);
- rotateRad(rad);
- }
- }
But if I rotate around a point several times, it does a "spiral" instead of a "circle"...
(the distance between the point become smaller or bigger, depending on the angle).
As example, this code (30 degrees):
- public class Main extends PApplet {
- Renderable r;
- public void setup() {
- size(570, 650);
- background(0);
- r = new Renderable();
- r.translate(200, 300);
- r.rotateDeg(30);
- }
- public void draw() {
- stroke(255);
- noFill();
- r.drawDecorationOn(g);
- r.rotateDeg(30, 300f, 300f);
- }
- }
does
and with 91 degrees :
What's the problem?
How can I obtain a correct accuracy?
1