Skip to content

Instantly share code, notes, and snippets.

@dbellettini
Last active December 23, 2015 09:39
Show Gist options
  • Save dbellettini/6615825 to your computer and use it in GitHub Desktop.
Save dbellettini/6615825 to your computer and use it in GitHub Desktop.
Vector class
package com.davidebellettini.trafficspread.physics;
import com.davidebellettini.trafficspread.data.AverageSpeed;
public class Vector {
private final double modulus;
private final double angle;
public Vector(double modulus, double angle) {
while (angle < 0.0) {
angle += 360.0;
}
while (angle >= 360.0f) {
angle -= 360.0;
}
this.modulus = modulus;
this.angle = angle;
}
public double getModulus() {
return modulus;
}
public double getAngle() {
return angle;
}
public Vector sum(Vector vector) {
double x1 = modulus * Math.cos(degreeToRadians(angle));
double y1 = modulus * Math.sin(degreeToRadians(angle));
double x2 = modulus * Math.cos(degreeToRadians(vector.angle));
double y2 = modulus * Math.sin(degreeToRadians(vector.angle));
double x3 = x1 + x2;
double y3 = y1 + y2;
double modulus3 = Math.sqrt(Math.pow(x3, 2) + Math.pow(y3, 2));
float angle3 = (float) (180 * (Math.atan2(y3, x3) + Math.PI) / Math.PI);
return new Vector(modulus3, angle3);
}
private double degreeToRadians(double angle) {
return Math.PI * angle / 180;
}
public static Vector fromAverageSpeed(AverageSpeed speed) {
return new Vector(speed.getSpeed(), speed.getBearing());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment