微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在旋转之前预测矩形的位置处理

如何解决如何在旋转之前预测矩形的位置处理

我希望我简洁地提出这个问题。我想运行一个脚本,该脚本将在旋转实际开始之前预测进行旋转时矩形的最终位置。因此,如果为您提供了一个位于坐标(40,40)上的矩形,并且希望该角度改变20度,那么如何预测或估计该矩形最终位置的x y值?我想先进行此估算,然后将其存储在数组中,然后在发生实际旋转时进行比较。对于预测,我以为会是这样的……

  void setup(){

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

  PShape Shape = createShape(GROUP);

  PShape rectangle = createShape(RECT,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);
 
  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);

}

但是,此打印的预测与x y实际结束的位置不太接近。它实际上以263、292结尾,但是打印时将x值设置为〜-121,将y值设置为〜317。我真正需要做的是使此预测的x和y坐标与我运行rectangle.rotate(radians(20))时的坐标相同。我只希望能够在实际到达该矩形之前先看到该矩形将要到达的位置。我觉得这是一个数学问题。我显然是新来的,所以不胜感激。

解决方法

您需要使用相对(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));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。