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
17package com.replica.replicaisland;
18
19/**
20 * Simple 2D vector class.  Handles basic vector math for 2D vectors.
21 */
22public 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
28    public Vector2() {
29        super();
30    }
31
32    public Vector2(float xValue, float yValue) {
33        set(xValue, yValue);
34    }
35
36    public Vector2(Vector2 other) {
37        set(other);
38    }
39
40    public final void add(Vector2 other) {
41        x += other.x;
42        y += other.y;
43    }
44
45    public final void add(float otherX, float otherY) {
46        x += otherX;
47        y += otherY;
48    }
49
50    public final void subtract(Vector2 other) {
51        x -= other.x;
52        y -= other.y;
53    }
54
55    public final void multiply(float magnitude) {
56        x *= magnitude;
57        y *= magnitude;
58    }
59
60    public final void multiply(Vector2 other) {
61        x *= other.x;
62        y *= other.y;
63    }
64
65    public final void divide(float magnitude) {
66        if (magnitude != 0.0f) {
67            x /= magnitude;
68            y /= magnitude;
69        }
70    }
71
72    public final void set(Vector2 other) {
73        x = other.x;
74        y = other.y;
75    }
76
77    public final void set(float xValue, float yValue) {
78        x = xValue;
79        y = yValue;
80    }
81
82    public final float dot(Vector2 other) {
83        return (x * other.x) + (y * other.y);
84    }
85
86    public final float length() {
87        return (float) Math.sqrt(length2());
88    }
89
90    public final float length2() {
91        return (x * x) + (y * y);
92    }
93
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
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
112    public final void zero() {
113        set(0.0f, 0.0f);
114    }
115
116    public final void flipHorizontal(float aboutWidth) {
117        x = (aboutWidth - x);
118    }
119
120    public final void flipVertical(float aboutHeight) {
121        y = (aboutHeight - y);
122    }
123}
124