Advertisement
Grey_Hugentobler

Vector Object 1.3

Apr 4th, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 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. private float xCoordinate;
  12. private 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. //get the length of the Vector
  36. public float length() {
  37. return Math.round(Math.sqrt(this.xCoordinate*this.xCoordinate + this.yCoordinate*this.yCoordinate));
  38. }
  39.  
  40. //get the direction of the Vector
  41. public float angle() {
  42. return Math.round(Math.atan2(this.yCoordinate, this.xCoordinate));
  43. }
  44.  
  45. // add two Vectors
  46. public Vector add(Vector that) {
  47. return new Vector(this.xCoordinate + that.xCoordinate, this.yCoordinate + that.yCoordinate);
  48. }
  49.  
  50. // get the negative of a vector
  51. public Vector negative() {
  52. return new Vector(-this.xCoordinate, -this.yCoordinate);
  53. }
  54.  
  55. // subtract one vector from another
  56. public Vector subtract(Vector that) {
  57. return this.add(that.negative());
  58. }
  59.  
  60. // get the dot product of two Vectors
  61. public float dot(Vector that) {
  62. return this.xCoordinate * that.xCoordinate + this.yCoordinate * that.yCoordinate;
  63. }
  64.  
  65. // Multiply a Vector by a scalar
  66. public Vector multiply(float that) {
  67. return new Vector(that * this.xCoordinate, that * this.yCoordinate);
  68. }
  69.  
  70. // return the distance between the endpoints of two Vectors
  71. public float distance(Vector that) {
  72. return Math.round(Math.sqrt((this.subtract(that)).dot(this.subtract(that))));
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement