Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Code by Grey Hugentobler, March 31st, 2011
- package GravityApplet;
- public class Vector {
- public static void main(String[] Args) {
- }
- public float xCoordinate;
- public float yCoordinate;
- // create the Vector
- public Vector(float x, float y) {
- this.xCoordinate = x;
- this.yCoordinate = y;
- }
- // get the x Coordinate of the Vector
- public float x() {
- return this.xCoordinate;
- }
- // get the y Coordinate of the Vector
- public float y() {
- return this.yCoordinate;
- }
- // add two Vectors
- public Vector add(Vector that) {
- return new Vector(this.xCoordinate + that.xCoordinate, this.yCoordinate
- + that.yCoordinate);
- }
- // get the negative of a vector
- public Vector negative() {
- return new Vector(-this.xCoordinate, -this.yCoordinate);
- }
- // subtract one vector from another
- public Vector subtract(Vector that) {
- return this.add(that.negative());
- }
- // get the dot product of two Vectors
- public float dot(Vector that) {
- return this.xCoordinate * that.xCoordinate + this.yCoordinate
- * that.yCoordinate;
- }
- // Multiply a Vector by a scalar
- public Vector multiply(float that) {
- return new Vector(that * this.xCoordinate, that * this.yCoordinate);
- }
- // return the distance between the endpoints of two Vectors
- public float distance(Vector that) {
- return Math.round(Math.sqrt((this.subtract(that)).dot(this
- .subtract(that))));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement