[英]Using sin and cos to make a circle move in a circle in java
I am creating a program that has a circle object that moves in a circular motion around a canvas. 我正在创建一个程序,该程序具有在画布上作圆周运动的圆形对象。 I have the circle and render classes created and am just working on the moving now.
我已经创建了圈子和渲染类,并且现在正在移动。 This is my code so far and it is unfortunately not working.
到目前为止,这是我的代码,不幸的是它无法正常工作。 I am not sure where to go from here or if I am on the right track.
我不确定从这里去哪里或者我走的路是否正确。
import java.util.*;
public class Rotation
{
public static void main(String[] args)
{
int x;
int y;
int radius = 30;
double step = .01;
Scanner input = new Scanner(System.in);
Render render = new Render();
Circle c = new Circle(150,150,radius);
render.addCircle(c);
render.draw();
double angle = 0.0;
while (angle < step)
{
x = (int)(c.getX()+Math.cos(angle*2*(Math.PI))*radius);
y = (int)(c.getY()+Math.sin(angle*2*(Math.PI))*radius);
c.setX(x);
c.setY(y);
render.redraw();
}
}
}
Well, your while loop is wrong. 好吧,你的while循环是错误的。 If you stubstitute the values
如果将值存根
while(angle < step)
becomes 变
while(0 < 0.01)
But that doesn't matter, since you're not actually incrementing angle anywhere. 但这没关系,因为您实际上没有在任何地方增加角度。 Perhaps you should increment the angle by the step and do it until the circle is full (ie 2*Math.PI).
也许您应该逐步增加角度,直到圆满为止(即2 * Math.PI)。
while(angle < step)
另一方面,您永远不要使用由Scanner input = new Scanner(System.in);
初始化的变量输入Scanner input = new Scanner(System.in);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.