How to find verticies after rotation?

edited December 2016 in Questions about Code

Hi all!

I have a little square I want to rotate and position it inside a bigger square. I then want to connect verticies from the bigger square to the smaller one. I don't know how to get the position of the rotated vertices.

I found this article: http://math.stackexchange.com/questions/270194/how-to-find-the-vertices-angle-after-rotation

I will try this, but wondered if processing had any built in geometry methods to make it easier.

Thanks in advance.

Dave

Tagged:

Answers

  • Processing already has all the trigonometry methods built in. Check the trigonometry section in the reference.
    However, there are ways to do this more easily by using the transformation matrix.

  • Thanks for the reply - do you have any examples of using the transformation matrix?

  • edited December 2016

    I'm getting close, but not quite there. Here is my example. I want the small circle centered on the rotated upper left vertex of the small square.

    int w=80;
    int w2=20;
    int counter=1;
    float angle =0.0;
    float x0 = 0.0;
    float y0 = 0.0;
    float x1 = 0.0;
    float y1 = 0.0;
    float p = 0.0;
    float q = 0.0;
    float p1 = 0.0;
    float q1 = 0.0;
    
    void setup() {
      size(400, 400);
    }
    
    void draw() {
      background(255);
      // rect1
      counter++;
      noFill();
      p = w/2;
      q = w/2;
      p1 = p - w2/2;
      q1 = q - w2/2;
      frameRate(1);
    
      pushMatrix();
        rectMode(CENTER);
        translate(p, q);
        rect(x0, y0, w, w);
        translate(-p, -q);
        //line(x0, y0, w, w);
        //line(w, y0, x0, w);
        pushMatrix();
          translate(p, q);
          angle = 15;
          rotate(radians(angle));
          rect(0, 0, w2, w2);
        popMatrix();
        println(p1, q1);
        x1 = ((x0-p1) * cos(angle)) - ((y0-q1) * sin(angle)) + p1;
        y1 = ((x0-p1) * sin(angle)) + ((y0-q1) * cos(angle)) + q1;
        ellipse(x1, y1, 10, 10);
      popMatrix();
    
    }
    
  • Please format your code.
    Press the gear icon to edit your post.
    Select your code and press ctrl + o.
    Leave a line above and below.

  • I told you to check out screenX and screenY.
    But I don't understand, if that's all you need draw the circle before popMatrix after rotate and the circle should be cantered at the coordinates as though rotate never occurred.

  • edited December 2016

    I am just using the circle as an indicator in this test code of where i want to calculate. Ultimately, I will be animating the rotation of the small square and I want to connect its vertices to the larger square with lines as it rotates around. I know the point from the large square where the line will originate, but I need the point on the small square where the line will end.

    I tried to use ScreenX and ScreenY, but I guess I am not clear on the inputs. In my nested pushMatrix, if I called ScreenX(0,0,0), it still returned 0,0 in world space

    changing line 45 to line(0, 0, x1, y1) is what I am looking for (with x1,y1 at the correct vertex);

  • Answer ✓

    No really, use screenX() and screenY().

    void setup(){
      size(400,400);
      noFill();
      stroke(255);
      rectMode(CENTER);
    }
    
    void draw(){
      background(0);
      pushMatrix();
      translate(width-mouseY, height-mouseX);
      rotate(map(millis()%5000,0,5000,0,TWO_PI));
      rect(0,0,200,200);
      float x = screenX(100,100);
      float y = screenY(100,100);
      popMatrix();
      line(mouseX, mouseY, x, y);
    }
    
  • Thank you!

  • That's what I was talking about.
    Read the screenX and screenY reference.

Sign In or Register to comment.