Homework X, Bugs on a Plane
Short Description:
Write a program that will allow the user to explore the Four Bug Problem.
Goals
When you finish this homework, you should have:- Demonstrated your ability to transform the plane.
- Demonstrated your ability to generate points on a regular polygon.
- Demonstrated your ability to use timers.
- Demonstrated your ability to use vectors for movement.
Formal Description
In the classical four bug problem, four bugs start at the corners of a square and move toward the next bug conuter clockwise. We would like to build an application that allows users to explore this problem, or at least look at the pretty picture, when the number of bugs and corners is somewhat more variable.You should build an application with a resonably well developed user interface that allows the user to
- Set the number of sides on the polygon where the bugs start between 3 and 50
- Set the number of bugs betwen the 2 and the number of sides sepecified, but the number of bugs must be a factor of the number of sides. A 12 sided polygon can have 2, 3, 4, 6 or 12 bugs. Prime number sided polygons must have exactaly that number of bugs.
- Set the size of step that each bug takes between 1 and 10
- Take a single step in the computation
- Run the computation until the bugs are within 150% of a step of each other. This should probably use a timeout.
- Stop/pause a running computation
- Reset the computation with new inputs.
You should use a setTransform to place center of the computation in the center of the canvas. You may use any scale that you wish, I still worked in pixel coordinates with my polygon having "radius"
min(canvas.width/2, canvas.height/2)*.9
Thinking of the coordinates (bug locations) as vectors makes computing the bug's movement very simple. A bug is at x,y looking at another bug at p,q. The vector (p-x, q-y) simulates this. Forming this as a unit vector (p-x,q-y)/d, where d = $\sqrt((p-x)^+(q-y)^2)$ gives a unit vector along the path the bug must travel, so the bug will move to (x+(p-x)/d, y+(p-y)/d). If you multiply the unit vector by the bug step size, you will get the coordinate to which the bug wishes to move. (Ask and we will go over this in class)
Your final result should resemble this picture where the polygon had 10 sides, there were 5 bugs each taking a 10 unit step eachupdate.