如何在矩形旋转之前预测其将移动到的位置(Processing)

huangapple go评论63阅读模式
英文:

How to predict where a rectangle will go before being rotated (Processing)

问题

我希望我表达得足够简洁。我想运行一个脚本,在旋转实际开始之前,预测矩形旋转后的最终位置。因此,假设给定一个位于坐标(40,40)的矩形,并且你想要将角度改变 20 度,你将如何预测或估计矩形的最终 x y 值?我想首先进行这种估计,然后将其存储在一个数组中,然后在实际旋转发生时进行比较。对于预测,我认为可能是这样的...

void setup(){
  size(825, 825);
  background(255);
  smooth();

  PShape Shape = createShape(GROUP);

  PShape rectangle = createShape(RECT, 40, 40, 120, 230); // 40 和 40 分别是 x 和 y 坐标
  
  // 额外的点,只是用来显示矩形的 x 和 y 坐标 //
  
  strokeWeight(5);
  stroke(0, 255, 0);
  PShape point = createShape(POINT, 40, 40);
 
  Shape.addChild(rectangle);
  Shape.addChild(point);

  int rectangleX = 40;
  int rectangleY = 40;

  int translationModifierX = 200;
  int translationModifierY = 200;

  // 这里是理论估计的新的 x 和 y 坐标,用于平移,然后再进行旋转。这个估计比较简单,当然可以预测出来。//
  
  int newX = rectangleX + translationModifierX; 
  int newY = rectangleY + translationModifierY;

  // 这里是尝试估计在旋转后的新的 x 和 y 坐标。//
  
  float rotatedX = newX*cos(20) - newY*sin(20);
  float rotatedY = newX*sin(20) + newY*cos(20);

  println("最终 X 坐标预测:", rotatedX);
  println("最终 Y 坐标预测:", rotatedY);

  pushMatrix();
  Shape.translate(newX, newY);
  Shape.rotate(radians(20));
  popMatrix();

  shape(Shape);
}

然而,这个打印出来的预测值与实际的 x y 值并不接近。实际的最终位置大约是 263,292,但打印出的 x 值大约为 ~-121,y 值为 ~317。我真正需要做的是,使得这个预测的 x 和 y 坐标与运行 rectangle.rotate(radians(20)) 时的坐标相同。我只想在实际发生旋转之前看到矩形会去哪里。我觉得这是一个数学问题。显然我是新手,所以我会非常感谢任何帮助。

英文:

I hope I'm asking this concisely enough. I'm wanting to run a script that will predict where a rectangle will end up when doing a rotation, before the rotation actually starts. So, if you're given a rectangle which is located on coordinates (40, 40) and you want the angle to change by 20 degrees, how would you predict or estimate the x y values of where that rectangle would end up? I'm wanting to do this estimation first, then store it in an array, and then compare it when the real rotation happens. For the prediction, I'd have thought it would be something like this...

  void setup(){

  size(825, 825);
  background(255);
  smooth();

  PShape Shape = createShape(GROUP);

  PShape rectangle = createShape(RECT, 40, 40, 120, 230); // with 40 and 40 being the x and y
  
  // extra point just to show where the x and y of the rectangle are //
  
  strokeWeight(5);
  stroke(0, 255, 0);
  PShape point = createShape(POINT, 40, 40);
 
  Shape.addChild(rectangle);
  Shape.addChild(point);

  int rectangleX = 40;
  int rectangleY = 40;

  int translationModifierX = 200;
  int translationModifierY = 200;

  // so this here would be the theoretical estimate on what the new x and y coordinates would be for the translation, before moving onto the rotation. This one's easy to predict, of course. //

  int newX = rectangleX + translationModifierX; 
  int newY = rectangleY + translationModifierY;

  // And here is where I'd be trying to estimate what the new x and y coordinates would be after rotated. //

  float rotatedX = newX*cos(20) - newY*sin(20);
  float rotatedY = newX*sin(20) + newY*cos(20);

  println("Final X Coordinate Prediction:", rotatedX);
  println("Final Y Coordinate Prediction:", rotatedY);

  pushMatrix();
  Shape.translate(newX, newY);
  Shape.rotate(radians(20));
  popMatrix();

  shape(Shape);

}

This printed prediction, though, is not that close to where the x y actually ends up. It actually ends up around 263, 292, but the print puts the x value as ~-121, and its y value at ~317. What I'm really needing to do is get this prediction's x and y coordinates to be the same as it would be when I run rectangle.rotate(radians(20)). I just want to be able to see where this rectangle would go before it actually goes there. I feel like it's a math problem. I'm obviously new, so I'd appreciate any assistance.

答案1

得分: 1

使用相对坐标(rectangleX/rectangleY),而不是绝对坐标(newX/newY)。

float rotatedX = newX + rectangleX * cos(radians(20)) - rectangleY * sin(radians(20));
float rotatedY = newY + rectangleX * sin(radians(20)) + rectangleY * cos(radians(20));
英文:

You need to use the relative (rectangleX/rectangleY), not the absolute (newX/newY) coordinates.

float rotatedX = newX + rectangleX*cos(radians(20)) - rectangleY*sin(radians(20));
float rotatedY = newY + rectangleX*sin(radians(20)) + rectangleY*cos(radians(20));

huangapple
  • 本文由 发表于 2020年10月11日 05:47:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64298607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定