Guest User

Untitled

a guest
Oct 4th, 2024
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.51 KB | Source Code | 0 0
  1. package core;
  2.  
  3. public class Vector2D {
  4.  
  5.     private double x;
  6.     private double y;
  7.    
  8.     public Vector2D(double x, double y) {
  9.         this.x = x;
  10.         this.y = y;
  11.     }
  12.    
  13.     public double length() {
  14.         return Math.sqrt(x*x + y*y);
  15.     }
  16.    
  17.     public void normalize() {
  18.         double length = length();
  19.         x = (x == 0) ? 0 : x/length;
  20.         y = (y == 0) ? 0 : y/length;
  21.     }
  22.  
  23.     public void multiply(double speed) {
  24.         x *= speed;
  25.         y *= speed;
  26.     }
  27.    
  28.     public double getX() {
  29.         return x;
  30.     }
  31.    
  32.     public double getY() {
  33.         return y;
  34.     }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment