Advertisement
Grey_Hugentobler

Vector Object 1.0

Apr 2nd, 2011
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. //Code by Grey Hugentobler, March 31st, 2011
  2.  
  3. package GravityApplet;
  4.  
  5. public class Vector {
  6.  
  7. public static void main(String[] Args) {
  8.  
  9. }
  10.  
  11. public float xCoordinate;
  12. public float yCoordinate;
  13.  
  14. // create the Vector
  15. public Vector(float x, float y) {
  16. this.xCoordinate = x;
  17. this.yCoordinate = y;
  18. }
  19.  
  20. // get the x Coordinate of the Vector
  21. public float x() {
  22. return this.xCoordinate;
  23. }
  24.  
  25. // get the y Coordinate of the Vector
  26. public float y() {
  27. return this.yCoordinate;
  28. }
  29.  
  30. // add two Vectors
  31. public Vector add(Vector that) {
  32. return new Vector(this.xCoordinate + that.xCoordinate, this.yCoordinate
  33. + that.yCoordinate);
  34. }
  35.  
  36. // get the negative of a vector
  37. public Vector negative() {
  38. return new Vector(-this.xCoordinate, -this.yCoordinate);
  39. }
  40.  
  41. // subtract one vector from another
  42. public Vector subtract(Vector that) {
  43. return this.add(that.negative());
  44. }
  45.  
  46. // get the dot product of two Vectors
  47. public float dot(Vector that) {
  48. return this.xCoordinate * that.xCoordinate + this.yCoordinate
  49. * that.yCoordinate;
  50. }
  51.  
  52. // Multiply a Vector by a scalar
  53. public Vector multiply(float that) {
  54. return new Vector(that * this.xCoordinate, that * this.yCoordinate);
  55. }
  56.  
  57. // return the distance between the endpoints of two Vectors
  58. public float distance(Vector that) {
  59. return Math.round(Math.sqrt((this.subtract(that)).dot(this
  60. .subtract(that))));
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement