Advertisement
Grey_Hugentobler

Vector Object 1.2

Apr 4th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. //Code by Grey Hugentobler, March 31st, 2011 through April 3rd, 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. //toStringMethod
  21. public String toString(){
  22. return "(" + this.xCoordinate + ", " + this.yCoordinate + ")";
  23. }
  24.  
  25. // get the x Coordinate of the Vector
  26. public float x() {
  27. return this.xCoordinate;
  28. }
  29.  
  30. // get the y Coordinate of the Vector
  31. public float y() {
  32. return this.yCoordinate;
  33. }
  34.  
  35. // add two Vectors
  36. public Vector add(Vector that) {
  37. return new Vector(this.xCoordinate + that.xCoordinate, this.yCoordinate + that.yCoordinate);
  38. }
  39.  
  40. // get the negative of a vector
  41. public Vector negative() {
  42. return new Vector(-this.xCoordinate, -this.yCoordinate);
  43. }
  44.  
  45. // subtract one vector from another
  46. public Vector subtract(Vector that) {
  47. return this.add(that.negative());
  48. }
  49.  
  50. // get the dot product of two Vectors
  51. public float dot(Vector that) {
  52. return this.xCoordinate * that.xCoordinate + this.yCoordinate * that.yCoordinate;
  53. }
  54.  
  55. // Multiply a Vector by a scalar
  56. public Vector multiply(float that) {
  57. return new Vector(that * this.xCoordinate, that * this.yCoordinate);
  58. }
  59.  
  60. // return the distance between the endpoints of two Vectors
  61. public float distance(Vector that) {
  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