Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. public class Vector2D {
  2. double x, y;
  3.  
  4. public Vector2D() {
  5. x = 0;
  6. y = 0;
  7. }
  8.  
  9. public Vector2D(double x, double y) {
  10. this.x = x;
  11. this.y = y;
  12. }
  13.  
  14. public Vector2D add(Vector2D v) {
  15. return new Vector2D(v.x + x, v.y + y);
  16. }
  17.  
  18. public void add2(Vector2D v) {
  19. x += v.x;
  20. y += v.y;
  21. }
  22.  
  23. public Vector2D sub(Vector2D v) {
  24. return new Vector2D(x - v.x, y - v.y);
  25. }
  26.  
  27. public void sub2(Vector2D v) {
  28. x -= v.x;
  29. y -= v.y;
  30. }
  31.  
  32. public Vector2D mult(double a) {
  33. return new Vector2D(x * a, y * a);
  34. }
  35.  
  36. public void mult2(double a) {
  37. x *= a;
  38. y *= a;
  39. }
  40.  
  41. public String toString() {
  42. return "(" + x + ";" + y + ")";
  43. }
  44.  
  45. public double length() {
  46. return Math.sqrt(x * x + y * y);
  47. }
  48.  
  49. public double scalarProduct(Vector2D v) {
  50. return x * v.x + y * v.y;
  51. }
  52.  
  53. public double cos(Vector2D v) {
  54. return scalarProduct(v) / (length() * v.length());
  55. }
  56.  
  57. public boolean equals(Vector2D v) {
  58. return v.x == x && v.y == y;
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement