• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.replica.replicaisland;
18 
19 /**
20  * Simple 2D vector class.  Handles basic vector math for 2D vectors.
21  */
22 public final class Vector2 extends AllocationGuard {
23     public float x;
24     public float y;
25 
26     public static final Vector2 ZERO = new Vector2(0, 0);
27 
Vector2()28     public Vector2() {
29         super();
30     }
31 
Vector2(float xValue, float yValue)32     public Vector2(float xValue, float yValue) {
33         set(xValue, yValue);
34     }
35 
Vector2(Vector2 other)36     public Vector2(Vector2 other) {
37         set(other);
38     }
39 
add(Vector2 other)40     public final void add(Vector2 other) {
41         x += other.x;
42         y += other.y;
43     }
44 
add(float otherX, float otherY)45     public final void add(float otherX, float otherY) {
46         x += otherX;
47         y += otherY;
48     }
49 
subtract(Vector2 other)50     public final void subtract(Vector2 other) {
51         x -= other.x;
52         y -= other.y;
53     }
54 
multiply(float magnitude)55     public final void multiply(float magnitude) {
56         x *= magnitude;
57         y *= magnitude;
58     }
59 
multiply(Vector2 other)60     public final void multiply(Vector2 other) {
61         x *= other.x;
62         y *= other.y;
63     }
64 
divide(float magnitude)65     public final void divide(float magnitude) {
66         if (magnitude != 0.0f) {
67             x /= magnitude;
68             y /= magnitude;
69         }
70     }
71 
set(Vector2 other)72     public final void set(Vector2 other) {
73         x = other.x;
74         y = other.y;
75     }
76 
set(float xValue, float yValue)77     public final void set(float xValue, float yValue) {
78         x = xValue;
79         y = yValue;
80     }
81 
dot(Vector2 other)82     public final float dot(Vector2 other) {
83         return (x * other.x) + (y * other.y);
84     }
85 
length()86     public final float length() {
87         return (float) Math.sqrt(length2());
88     }
89 
length2()90     public final float length2() {
91         return (x * x) + (y * y);
92     }
93 
distance2(Vector2 other)94     public final float distance2(Vector2 other) {
95         float dx = x - other.x;
96         float dy = y - other.y;
97         return (dx * dx) + (dy * dy);
98     }
99 
normalize()100     public final float normalize() {
101         final float magnitude = length();
102 
103         // TODO: I'm choosing safety over speed here.
104         if (magnitude != 0.0f) {
105             x /= magnitude;
106             y /= magnitude;
107         }
108 
109         return magnitude;
110     }
111 
zero()112     public final void zero() {
113         set(0.0f, 0.0f);
114     }
115 
flipHorizontal(float aboutWidth)116     public final void flipHorizontal(float aboutWidth) {
117         x = (aboutWidth - x);
118     }
119 
flipVertical(float aboutHeight)120     public final void flipVertical(float aboutHeight) {
121         y = (aboutHeight - y);
122     }
123 }
124