Advertisement
Grey_Hugentobler

Vector object (float)

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