Below is the syntax highlighted version of Body.java
from §3.4 Case Study: N-Body Simulation.
/****************************************************************************** * Compilation: javac Body.java * Execution: java Body * Dependencies: Vector.java StdDraw.java * * Implementation of a 2D Body with a position, velocity and mass. * * ******************************************************************************/ public class Body { private Vector r; // position private Vector v; // velocity private final double mass; // mass public Body(Vector r, Vector v, double mass) { this.r = r; this.v = v; this.mass = mass; } public void move(Vector f, double dt) { Vector a = f.scale(1/mass); v = v.plus(a.scale(dt)); r = r.plus(v.scale(dt)); } public Vector forceFrom(Body b) { Body a = this; double G = 6.67e-11; Vector delta = b.r.minus(a.r); double dist = delta.magnitude(); double magnitude = (G * a.mass * b.mass) / (dist * dist); return delta.direction().scale(magnitude); } public void draw() { StdDraw.setPenRadius(0.025); StdDraw.point(r.cartesian(0), r.cartesian(1)); } // this method is only needed if you want to change the size of the bodies public void draw(double penRadius) { StdDraw.setPenRadius(penRadius); StdDraw.point(r.cartesian(0), r.cartesian(1)); } }