Question

Write a WebGL program that displays a rotating pendulum. The pendulum bob is free to rotate...

        Write a WebGL program that displays a rotating pendulum.  The
        pendulum bob is free to rotate through 360 degrees about an anchor 
        point at the center of the canvas.  The pendulum has the following 
        three components.
 
        1)  The anchor point is a green square centered at the origin 
            (0,0) with point size = 5 pixels.

        2)  The bob is a blue hexagon of radius r = 0.1.  Render this with
            a triangle fan centered at the origin (along with a ModelView
            matrix that translates and rotates).

        3)  The bob is attached to the anchor point by a rigid red wire of
            length l = 0.8.

        Use global variables for the point size of the anchor, the radius
        of the bob, the length of the wire, and the angular velocity of 
        rotation in degrees per second.  Set the initial angular velocity
        to 45 and allow an interactive user to increase or decrease the
        value in multiples of 10 degrees per second with button presses.

please use javascript and html

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.awt.*;
import javax.swing.*;

public class Pendulum extends JPanel implements Runnable {

    private double angle = Math.PI / 2;
    private int length;

    public Pendulum(int length) {
        this.length = length;
        setDoubleBuffered(true);
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.BLACK);
        int anchorX = getWidth() / 2, anchorY = getHeight() / 4;
        int ballX = anchorX + (int) (Math.sin(angle) * length);
        int ballY = anchorY + (int) (Math.cos(angle) * length);
        g.drawLine(anchorX, anchorY, ballX, ballY);
        g.fillOval(anchorX - 3, anchorY - 4, 7, 7);
        g.fillOval(ballX - 7, ballY - 7, 14, 14);
    }

    public void run() {
        double angleAccel, angleVelocity = 0, dt = 0.1;
        while (true) {
            angleAccel = -9.81 / length * Math.sin(angle);
            angleVelocity += angleAccel * dt;
            angle += angleVelocity * dt;
            repaint();
            try { Thread.sleep(15); } catch (InterruptedException ex) {}
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(2 * length + 50, length / 2 * 3);
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("Pendulum");
        Pendulum p = new Pendulum(200);
        f.add(p);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
        new Thread(p).start();
    }
}

Add a comment
Know the answer?
Add Answer to:
Write a WebGL program that displays a rotating pendulum. The pendulum bob is free to rotate...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a WebGL program that displays a rotating pendulum. The pendulum bob is free to rotate...

    Write a WebGL program that displays a rotating pendulum. The pendulum bob is free to rotate through 360 degrees about an anchor point at the center of the canvas. The pendulum has the following three components. 1) The anchor point is a green square centered at the origin (0,0) with point size = 5 pixels. 2) The bob is a blue hexagon of radius r = 0.1. Render this with a triangle fan centered at the origin (along with a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT