Advertisement
xatzisktv

Untitled

Feb 20th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. package utilities;
  2. // mutable 2D vectors
  3. public final class Vector2D {
  4. public double x, y;
  5.  
  6. // constructor for zero vector
  7. public Vector2D() {
  8. this.x = 0;
  9. this.y = 0;
  10. }
  11.  
  12. // constructor for vector with given coordinates
  13. public Vector2D(double x, double y) {
  14. this.x = x;
  15. this.y = y;
  16. }
  17.  
  18. // constructor that copies the argument vector
  19. public Vector2D(Vector2D v) {
  20. this.x = v.x;
  21. this.y = v.y;
  22. }
  23.  
  24. // set coordinates
  25. public Vector2D set(double x, double y) {
  26. this.x = x;
  27. this.y = y;
  28. return this;
  29. }
  30.  
  31. // set coordinates based on argument vector
  32. public Vector2D set(Vector2D v) {
  33. this.x = v.x;
  34. this.y = v.y;
  35. return this;
  36. }
  37.  
  38. // compare for equality (note Object type argument)
  39. public boolean equals(Object o) {
  40. if (o instanceof Vector2D){
  41. Vector2D v = (Vector2D) o;
  42. return this.x == v.x && this.y == v.y;
  43. }
  44. return false;
  45. }
  46.  
  47. // String for displaying vector as text
  48. public String toString() {
  49. return "This vector has the X cord: " + this.x + "And Y cord: " + this.y;
  50. }
  51.  
  52. // magnitude (= "length") of this vector
  53. public double mag() {
  54. return Math.hypot(this.x, this.y);
  55. }
  56.  
  57. // angle between vector and horizontal axis in radians
  58. // can be calculated using Math.atan2
  59. public double angle() {
  60. return Math.atan2(this.y, this.x);
  61. }
  62.  
  63. // angle between this vector and another vector
  64. // take difference of angles, add 2*Math.PI if result is negative
  65. public double angle(Vector2D other) {
  66. return subtract(other).angle();
  67. }
  68.  
  69. // add argument vector
  70. public Vector2D add(Vector2D v) {
  71. this.x += v.x;
  72. this.y += v.y;
  73. return this;
  74. }
  75.  
  76. // add values to coordinates
  77. public Vector2D add(double x, double y) {
  78. this.x += x;
  79. this.y += y;
  80. return this;
  81. }
  82.  
  83. // weighted add - surprisingly useful
  84. public Vector2D addScaled(Vector2D v, double fac) {
  85. this.x += (v.x*fac);
  86. this.y += (v.y*fac);
  87. return this;
  88. }
  89.  
  90. // subtract argument vector
  91. public Vector2D subtract(Vector2D v) {
  92. this.x -= v.x;
  93. this.y -= v.y;
  94. return this;
  95. }
  96.  
  97. // subtract values from coordinates
  98. public Vector2D subtract(double x, double y) {
  99. this.x -= x;
  100. this.y -= y;
  101. return this;
  102. }
  103.  
  104. // multiply with factor
  105. public Vector2D mult(double fac) {
  106. this.x = this.x*fac;
  107. this.y = this.y*fac;
  108. return this;
  109. }
  110.  
  111. // rotate by angle given in radians
  112. public Vector2D rotate(double angle) {
  113. double a = this.x;
  114. double b = this.y;
  115. this.x = a*Math.cos(angle) - b*Math.sin(angle);
  116. this.y = a*Math.sin(angle) + b*Math.cos(angle);
  117. return this;
  118. }
  119.  
  120. // "dot product" ("scalar product") with argument vector
  121. public double dot(Vector2D v) {
  122. return this.x * v.x + this.y * v.y;
  123. }
  124.  
  125. // distance to argument vector
  126. public double dist(Vector2D v) {
  127. return new Vector2D(v.x - this.x, v.y - this.y).mag();
  128. }
  129.  
  130. // normalise vector so that magnitude becomes 1
  131. public Vector2D normalise() {
  132. return this.mult(1/this.mag());
  133. }
  134.  
  135. // wrap-around operation, assumes w> 0 and h>0
  136. public Vector2D wrap(double w, double h) {
  137. this.x = (x+w)%w;
  138. this.y = (y+h)%h;
  139. return this;
  140. }
  141.  
  142. // construct vector with given polar coordinates
  143. public static Vector2D polar(double angle, double mag) {
  144. return new Vector2D(angle * Math.cos(mag), angle * Math.sin(mag));
  145. }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement