user3015645
user3015645

Reputation: 31

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();
   }
  }
}

Upvotes: 0

Views: 1800

Answers (2)

wuchang
wuchang

Reputation: 3069

while(angle < step) is actual a dead loop.And another thing ,you never use the variable input which is initiated by Scanner input = new Scanner(System.in);

Upvotes: 0

Kayaman
Kayaman

Reputation: 73528

Well, your while loop is wrong. 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 (i.e. 2*Math.PI).

Upvotes: 1

Related Questions