Advertisement
xatzisktv

Untitled

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