[英]Java Processing: Rotate about a Point Using the Mouse
我正在创建一个 java 处理程序,其中我需要在按住鼠标时围绕一个点旋转一个矩形。
一些限制:
任何帮助将不胜感激。 附上我的程序。
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);
}
rect
的第三个和第四个参数不是矩形的右下点,而是它的宽度和高度。
如果执行旋转( rotate()
),则对象将围绕其中心旋转。
您必须定义一个矩形,其中心点为 (0, 0)。 例如:
rect(-50, -50, 50, 50);
rect(-25, -25, 50, 50);
如果要围绕枢轴( poivot_x
、 poivot_y
)旋转对象,则必须:
以这种方式平移对象,枢轴位于 (0, 0) 处: translate(-poivot_x, -poivot_y)
然后旋转对象: rotate(...)
最后将枢轴平移回其原始位置: translate(poivot_x, poivot_y)
由于像rotate()
、 scale()
和translate()
等矩阵运算定义了一个矩阵并将当前矩阵乘以新矩阵,因此必须以相反的顺序进行这些运算。
例如,围绕顶部边缘 (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.