简体   繁体   English

Java 处理:使用鼠标绕一个点旋转

[英]Java Processing: Rotate about a Point Using the Mouse

I am creating a java processing program in which I need to rotate a rectangle about a point when the mouse is held down.我正在创建一个 java 处理程序,其中我需要在按住鼠标时围绕一个点旋转一个矩形。

A few restrictions:一些限制:

  1. It can only rotate 180 degrees(in a semicircle)只能旋转180度(半圆)
  2. Once it gets to the end of that 180 degrees, it must reverse the direction of its rotation.一旦到达 180 度的终点,它必须反转其旋转方向。

Any help would be greatly appreciated.任何帮助将不胜感激。 My program is attached.附上我的程序。

void setup() {
    size(600, 600);
    smooth();
}

void draw() {
    background(0);
    noStroke();
    fill(169,169,169); 

    translate(width/2, height/2);

    boolean run=true;
    if (mousePressed==true && run==true){
        rotate(radians(frameCount/2));
    }
    if (mousePressed==false){
        run=false;
    }
    if(mousePressed==true  && run==false){
        run=true;
    }
    fill(255);
    rect(-50, -50, 50, 50);
}

The 3rd and 4th parameter of rect is not the bottom right point of the rectangle, it is its width and height. rect的第三个和第四个参数不是矩形的右下点,而是它的宽度和高度。
If a rotation ( rotate() ) is performed, then an object is rotated about its center.如果执行旋转( rotate() ),则对象将围绕其中心旋转。

You've to define a rectangle, with a center point of (0, 0).您必须定义一个矩形,其中心点为 (0, 0)。 eg:例如:

rect(-50, -50, 50, 50);

rect(-25, -25, 50, 50);

If you want to rotate an object around a pivot ( poivot_x , poivot_y ), then you've to:如果要围绕枢轴( poivot_xpoivot_y )旋转对象,则必须:

  • Translate the object in that way, that the pivot is at (0, 0): translate(-poivot_x, -poivot_y)以这种方式平移对象,枢轴位于 (0, 0) 处: translate(-poivot_x, -poivot_y)

  • Then rotate the object: rotate(...)然后旋转对象: rotate(...)

  • Finally translate the pivot back to its original place: translate(poivot_x, poivot_y)最后将枢轴平移回其原始位置: translate(poivot_x, poivot_y)

Since matrix operations like rotate() , scale() and translate() , define a matrix and multiply the current matrix by the new matrix, this operations have to be done in reverse order.由于像rotate()scale()translate()等矩阵运算定义了一个矩阵并将当前矩阵乘以新矩阵,因此必须以相反的顺序进行这些运算。

eg Rotate the rectangle (0, 0, 50, 50) around the center of the top edge (25, 0):例如,围绕顶部边缘 (25, 0) 的中心旋转矩形 (0, 0, 50, 50):

int poivot_x = 25; // center
int poivot_y = 0;  // top

translate(poivot_x, poivot_y);
rotate(radians(frameCount/2));
translate(-poivot_x, -poivot_y);

rect(0, 0, 50, 50);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM