Skip to content
Snippets Groups Projects
Commit 07fb8243 authored by William Billingsley's avatar William Billingsley
Browse files

Added labels for flock data

parent b97a5761
No related branches found
No related tags found
No related merge requests found
......@@ -100,6 +100,7 @@ Partially implemented, you have:
* The **Action Replay** button must take the simulation back to the start of
the memory buffer (typically one second) and let the simulation continue
again from there.
Hint: think about which end you want to insert the frame.
* The **wind** buttons should set the wind strength and direction. As the
boids' velocity is normalised on the next tick, in practice this works as a
......@@ -118,6 +119,20 @@ Partially implemented, you have:
* Clicking a point on the canvas should add a new boid into the simulation,
heading in a random direction with a velocity of 1
* There are also three labels at the foot of the simulation, which are intended to show some
data about the flock. (These are drawn from `Simulation.flockDir`, `Simulation.flockSpeed`, and `Simulation.flockSep`
which you'll need to implement.)
- Flock direction: The average direction of the flock. (Sum the velocity vectors and take its theta). You should be
able to see if this is working by seeing how it changes with the wind buttons
- Flock speed: The average speed of the flock. (Take the average of the magnitude of the velocity vectors). This should
stay more or less constant, because of how boids work
- Flock separation: The variance of the position of the flock. This one's a little trickier. Find the centroid of the flock
(average the position vectors). Then for each boid, calculate the square of its distance from the centroid. Return the
mean of that. To test this, hit the "explosion of boids" button. It should drop to nearly zero and then grow.
## Note on the provided code
I've provided this code by writing a solution and then cutting back from it.
......@@ -138,8 +153,8 @@ Functionality:
* Startle works: 1
* Regenesis works: 1
* Action replay works: 1
* Mean direction, separation, and velocity works: 3 (1 each)
Quality:
* Boid uses functional rather than imperative code: 3
* Overall quality judgment (readable, tidy, concise): 5
* Overall quality judgment (functional, readable, tidy, concise): 5
......@@ -7,6 +7,9 @@ import javax.swing._
import scala.collection.mutable
import scala.util.Random
/**
* You shouldn't edit this file
*/
object BoidsApp {
/** The main window */
......@@ -32,7 +35,11 @@ object BoidsApp {
val regenesis = new JButton("Regenesis")
def main(args:Array[String]):Unit = {
val directionLabel = new JLabel("Flock direction: xx radians")
val separationLabel = new JLabel("Flock speed: xx")
val velocityLabel = new JLabel("Flock separation: xx")
@main def run = {
val container = new JPanel()
container.setLayout(new BorderLayout())
......@@ -79,6 +86,15 @@ object BoidsApp {
container.add(controlsContainer, BorderLayout.EAST)
val dataContainer = new JPanel()
dataContainer.setLayout(new GridLayout(1, 3))
dataContainer.add(directionLabel)
dataContainer.add(velocityLabel)
dataContainer.add(separationLabel)
container.add(dataContainer, BorderLayout.SOUTH)
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
window.add(container)
window.setSize(container.getPreferredSize)
......@@ -106,6 +122,11 @@ object BoidsApp {
val timer = new Timer(16, (e) => {
boidsPanel.setBoids(Simulation.update())
SwingUtilities.invokeLater(() =>
directionLabel.setText(s"Flock direction: ${Simulation.flockDir} radians")
directionLabel.setText(s"Flock speed: ${Simulation.flockSpeed} ")
directionLabel.setText(s"Flock separation: ${Simulation.flockSep}")
)
})
timer.start()
......
......@@ -87,6 +87,29 @@ object Simulation {
}
/** The current average direction of the flock. Add up all the boids' velocity vectors, and take the theta. */
def flockDir:Double =
println("Warning, you haven't implemented flockDir!")
0d
/** The current average speed of the flock. Take the mean of all the boids' velocity magnitudes. */
def flockSpeed:Double =
println("Warning, you haven't implemented flockSpeed!")
0d
/**
* The variance of the flock's positions, ignoring the fact we wrap around the screen.
* To get this one:
* * Calculate the centroid of the flock (Add all the position vectors, and divide by the number of boids)
* * Calculate the square of the distance of each boid from this centroid, and sum them.
* i.e., sum Math.pow((b.position - centroid).magnitude, 2)
* * Divide this by the number of boids.
*
* We'll probably eyeball the code for this one, given we're going to find it harder to eyeball whether the number
* on the screen looks right!
*/
def flockSep:Double =
println("Warning, you haven't implemented flockSep!")
0d
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment