import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class DoublePendulum here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class DrawPendulum extends Actor
{
private double angle1 = Math.PI / 2;
private double angle2 = Math.PI / 2;
private int length1;
private int length2;
double angleAccel1 = 0;
double angleAccel2 = 0;
double angleVelocity1 = 0;
double angleVelocity2 = 0;
double dt1 = 0.1;
double dt2 = 0.2;
public DrawPendulum(int length1, int length2) {
this.length1 = length1;
this.length2 = length2;
}
/**
* Act - do whatever the DoublePendulum wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
drawDoublePendulum();
run();
}
public void drawDoublePendulum() {
GreenfootImage gfim = new GreenfootImage(900, 600);
gfim.setColor(Color.WHITE);
gfim.fillRect(0, 0, gfim.getWidth(), gfim.getHeight());
gfim.setColor(Color.BLACK);
int anchorX1 = gfim.getWidth() / 2, anchorY1 = gfim.getHeight() / 4;
int anchorX2 = gfim.getWidth() / 2, anchorY2 = gfim.getHeight() / 4;
int ballX1 = anchorX1 + (int) (Math.sin(angle1) * length1);
int ballY1 = anchorY1 + (int) (Math.cos(angle1) * length1);
int ballX2 = ballX1 + (int) (Math.sin(angle2) * length2);
int ballY2 = ballY1 + (int) (Math.cos(angle2) * length2);
gfim.drawLine(anchorX1, anchorY1, ballX1, ballY1);
gfim.drawLine(ballX1, ballY1, ballX2, ballY2);
gfim.fillOval(anchorX1 - 3, anchorY1 - 4, 7, 7);
gfim.fillOval(anchorX2 - 3, anchorY2 - 4, 7, 7);
gfim.fillOval(ballX1 - 7, ballY1 - 7, 14, 14);
gfim.fillOval(ballX2 - 7, ballY2 - 7, 14, 14);
this.setImage(gfim);
}
public void run()
{
angleAccel1 = -9.81 / length1 * Math.sin(angle1);
angleVelocity1 += angleAccel1 * dt1;
angle1 += angleVelocity1 * dt1;
angleAccel2 = -9.81 / length2 * Math.sin(angle2);
angleVelocity2 += angleAccel2 * dt2;
angle2 += angleVelocity2 * dt2;
}
}

