我想在Java处理中以圆形运动移动一个正方形

发布于 2025-02-13 17:53:19 字数 503 浏览 3 评论 0原文

这是一个学校项目,因此我不能使用很多诸如翻译或旋转之类的功能。我必须使用基本三角学来做到这一点。因此,我制作了一个正方形,我需要它以360度的圆形运动移动,其点恒定且不动。

               float rotX,rotY;
               size(500,500);

               fill(#B71143);
               int rectX=width/4;
               int rectY=height/10;
               int rectSize=30;
               angle=angle+0.1;
               //rotX=rectX*cos(angle)-rectY*sin(angle);
               //rotY=rectX*cos(angle)+rectY*sin(angle);

               square(rotX,rotY,rectSize);

This is a school project so i cannot use a lot of functions like translate or rotate. I have to use basic trigonometry to do this. So I have made a square and I need it to move in a circular motion 360 degrees with one of it's point constant and not moving.

               float rotX,rotY;
               size(500,500);

               fill(#B71143);
               int rectX=width/4;
               int rectY=height/10;
               int rectSize=30;
               angle=angle+0.1;
               //rotX=rectX*cos(angle)-rectY*sin(angle);
               //rotY=rectX*cos(angle)+rectY*sin(angle);

               square(rotX,rotY,rectSize);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

漫雪独思 2025-02-20 17:53:19

至少就三角学部分而言,您是如此亲密。

在处理方面,您只缺少setup() and draw()它将绘制一个帧(一旦您取消注册rotx的作业,roty,roty < /code>和初始化Angle to 0)

这是您的代码,上面的注释:

float rotX, rotY;
float angle = 0;

void setup() {
  size(500, 500);

  fill(#B71143);
}

void draw() {

  int rectX=width/4;
  int rectY=height/10;

  int rectSize=30;
  angle=angle+0.1;
  
  rotX = rectX*cos(angle)-rectY*sin(angle);
  rotY = rectX*cos(angle)+rectY*sin(angle);
  
  
  square(rotX, rotY, rectSize);
}

此外,如果要从中心绘制,则可以在渲染之前将宽度/高度的一半添加到方形坐标(相等的要翻译成中心),如果您想绘制一个圆而不是椭圆形,请使用两个RADII的大小(命名rectx,retecty,retecty):

float rotX, rotY;
float angle = 0;

void setup() {
  size(500, 500);

  fill(#B71143);

  rectMode(CENTER);
}

void draw() {

  int rectX=width/4;
  int rectY=height/4;

  int rectSize=30;
  angle=angle+0.1;
  
  rotX = rectX*cos(angle)-rectY*sin(angle);
  rotY = rectX*cos(angle)+rectY*sin(angle);
  
  // offset to center
  rotX += width / 2;
  rotY += height / 2;
  
  square(rotX, rotY, rectSize);
}

就我个人而言,我会有点简化代码(尽管您的作业要求可能会根据您的课程而有所不同)。

// initialise angle value
float angle = 0;

void setup() {
  size(500, 500);

  fill(#B71143);
}

void draw() {
  // circular motion radius
  int radius = width / 4;
  // define square size
  int rectSize=30;
  // increment the angle (rotating around center)
  angle=angle+0.1;
  // convert polar (angle, radius) to cartesian(x,y) coords
  float x = cos(angle) * radius;
  float y = sin(angle) * radius;
  
  // offset to center
  x += width / 2;
  y += height / 2;
  // render the square at the rotated position
  square(x, y, rectSize);
}

(如果您是沿着笛卡尔坐标的另一个极地转换公式的说明,则可以在这里查看我的较旧答案其中包括互动演示)

You are so close, at least in terms of the trigonometry part.

In terms of Processing you're only missing setup() and draw() which will draw a single frame (once you uncomment the assignments of rotX, rotY and initialise angle to 0)

Here's your code with above notes applied:

float rotX, rotY;
float angle = 0;

void setup() {
  size(500, 500);

  fill(#B71143);
}

void draw() {

  int rectX=width/4;
  int rectY=height/10;

  int rectSize=30;
  angle=angle+0.1;
  
  rotX = rectX*cos(angle)-rectY*sin(angle);
  rotY = rectX*cos(angle)+rectY*sin(angle);
  
  
  square(rotX, rotY, rectSize);
}

Additionally, if you want to draw from the centre, you can add half the width/height to the square coordinates before rendering (equivalent to translating to centre), and if you want to draw a circle instead of an oval, use the same size for the two radii (named rectX, rectY):

float rotX, rotY;
float angle = 0;

void setup() {
  size(500, 500);

  fill(#B71143);

  rectMode(CENTER);
}

void draw() {

  int rectX=width/4;
  int rectY=height/4;

  int rectSize=30;
  angle=angle+0.1;
  
  rotX = rectX*cos(angle)-rectY*sin(angle);
  rotY = rectX*cos(angle)+rectY*sin(angle);
  
  // offset to center
  rotX += width / 2;
  rotY += height / 2;
  
  square(rotX, rotY, rectSize);
}

Personally I'd simplify the code a bit (though your assignment requirements might differ based on your curriculum).

// initialise angle value
float angle = 0;

void setup() {
  size(500, 500);

  fill(#B71143);
}

void draw() {
  // circular motion radius
  int radius = width / 4;
  // define square size
  int rectSize=30;
  // increment the angle (rotating around center)
  angle=angle+0.1;
  // convert polar (angle, radius) to cartesian(x,y) coords
  float x = cos(angle) * radius;
  float y = sin(angle) * radius;
  
  // offset to center
  x += width / 2;
  y += height / 2;
  // render the square at the rotated position
  square(x, y, rectSize);
}

(In case you're after another polar to cartesian coordinates conversion formula explanation you can check out my older answer here which includes an interactive demo)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文