Advertisement
tnkv

Untitled

Dec 8th, 2022
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. import java.util.Locale;
  2.  
  3. public class Vector2D {
  4.     public double vX, vY;
  5.  
  6.     public Vector2D() {
  7.         this.vX = 1.0;
  8.         this.vY = 1.0;
  9.     }
  10.  
  11.     public Vector2D(double x, double y) {
  12.         this.vX = x;
  13.         this.vY = y;
  14.     }
  15.  
  16.     public Vector2D(Vector2D vector) {
  17.         this.vX = vector.vX;
  18.         this.vY = vector.vY;
  19.     }
  20.  
  21.     public void print() {
  22.         System.out.println("(" + String.format(Locale.US, "%.2f", vX) + ", " + String.format(Locale.US, "%.2f", vY) + ")");
  23.     }
  24.     public void add(Vector2D vector) {
  25.         this.vX += vector.vX;
  26.         this.vY += vector.vY;
  27.     }
  28.     public void sub(Vector2D vector) {
  29.         this.vX -= vector.vX;
  30.         this.vY -= vector.vY;
  31.     }
  32.     public double length(){
  33.         return Math.pow(Math.pow(this.vX, 2) + Math.pow(this.vY, 2), 0.5);
  34.     }
  35.     public void scale(double scale) {
  36.         this.vX *= scale;
  37.         this.vY *= scale;
  38.     }
  39.     public void normalized() {
  40.         this.vX = this.vX / this.length();
  41.         this.vY = this.vY / this.length();
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement