public class MovingPolygon extends DPolygon { /* A MovingPolygon, is just a DPolygon with a velocity (speed, direction). The MovingPolygon includes just one function, move, to move itself one step according to its velocity. This class also defines the topology--a 2D torus manifold. Polygons moving off one end of the screen simply reappear on the other side. */ // maximum coordinates before wrapping to 0.0 public static double X_LIMIT = 400.0, Y_LIMIT = 400.0; public double speed, direction; // defines a velocity public MovingPolygon() // allocate space for a moving polygon { super(); speed = 0.0; direction = 0.0; } public MovingPolygon(MovingPolygon poly) // make a copy of a moving polygon { super(poly); speed = poly.speed; direction = poly.direction; } public MovingPolygon(double[] x, double[] y, double r, DPoint c) { // create a moving polygon given coordinates, a radius, and a center point super(x,y,r,c); } public void move() // move the polygon one step within the 2D torus manifold { translate(speed * Math.cos(direction), speed * Math.sin(direction)); // check bounds on the visible area and fix if out of range if (center.x < 0.0) translate(X_LIMIT, 0.0); else if (center.x > X_LIMIT) translate(-X_LIMIT, 0.0); if (center.y < 0.0) translate(0.0, Y_LIMIT); else if (center.y > Y_LIMIT) translate(0.0, -Y_LIMIT); } }