Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Vector2
- {
- private double x, y, length;
- /**
- * @return A new Vector2
- * @param x = HorizontalLength; y = Vertical Length
- */
- public Vector2(double x, double y)
- {
- this.x = x;
- this.y = y;
- this.length = length(this);
- }
- public double getX(){ return x;}
- public double getY(){ return y;}
- public double getLength(){ return length;}
- public void setX(double x){ this.x = x;}
- public void setY(double y){ this.y = y;}
- public void setLength(double length){ this.length = length;}
- /**
- * @return The sum of both Vector2
- */
- public Vector2 add(Vector2 vec1, Vector2 vec2)
- {
- Vector2 sum = new Vector2((vec1.x + vec2.x), (vec1.y + vec2.y));
- return sum;
- }
- /**
- * @return The subtraction of both Vector2
- */
- public Vector2 subtract(Vector2 vec1, Vector2 vec2)
- {
- Vector2 sub = new Vector2((vec1.x - vec2.x), (vec1.y - vec2.y));
- return sub;
- }
- /**
- * @return The distance of both Vector2
- */
- public double distance(Vector2 vec1, Vector2 vec2)
- {
- double distance = length(subtract(vec1, vec2));
- return distance;
- }
- /**
- * @return The product of a Vector2 and a factor as a Vector2
- */
- public Vector2 scalarm(Vector2 vec, double factor)
- {
- Vector2 restult = new Vector2(vec.x * factor, vec.y * factor);
- return restult;
- }
- /**
- * @return The scalar product of both Vector2
- */
- public double scalarp(Vector2 vec1, Vector2 vec2)
- {
- double scalar = (vec1.x * vec2.x) + (vec1.y * vec2.y);
- return scalar;
- }
- /**
- * @return The length of the Vector2
- */
- public double length(Vector2 vec)
- {
- double length = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
- return length;
- }
- /**
- * @return The length of the Vector2
- */
- public double squaredLength(Vector2 vec)
- {
- double squaredLength = Math.pow(x, 2) + Math.pow(y, 2);
- return squaredLength;
- }
- /**
- * @return A Vector that was divided by its length
- */
- public Vector2 normalize(Vector2 vec)
- {
- Vector2 norm = new Vector2(vec.x * (1/vec.length), vec.y * (1/vec.length));
- return norm;
- }
- /**
- * @return The angle between two Vector2 in radians
- */
- public double angle(Vector2 vec1, Vector2 vec2)
- {
- vec1 = normalize(vec1);
- vec2 = normalize(vec2);
- double angle = Math.acos(vec1.x * vec2.x + vec1.y * vec2.y);
- return angle;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement