Rev 835 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 835 | chris | 1 | package com.gebauz.bauzoid.math; |
| 2 | |||
| 3 | public class Vector2 |
||
| 4 | { |
||
| 5 | |||
| 6 | public float x; |
||
| 7 | public float y; |
||
| 8 | |||
| 9 | public Vector2() |
||
| 10 | { |
||
| 11 | } |
||
| 12 | |||
| 13 | public Vector2(float _x, float _y) |
||
| 14 | { |
||
| 1001 | chris | 15 | set(_x, _y); |
| 16 | } |
||
| 17 | |||
| 18 | public void set(float _x, float _y) |
||
| 19 | { |
||
| 835 | chris | 20 | x = _x; |
| 21 | y = _y; |
||
| 22 | } |
||
| 23 | |||
| 24 | public void normalize() |
||
| 25 | { |
||
| 26 | float len = length(); |
||
| 27 | if (len != 0.0f) |
||
| 28 | { |
||
| 29 | x /= len; |
||
| 30 | y /= len; |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | public float length() |
||
| 35 | { |
||
| 36 | float squared = squaredLength(); |
||
| 37 | return (float)Math.sqrt(squared); |
||
| 38 | } |
||
| 39 | |||
| 40 | public void setLength(float newLength) |
||
| 41 | { |
||
| 42 | normalize(); |
||
| 43 | x *= newLength; |
||
| 44 | y *= newLength; |
||
| 45 | } |
||
| 46 | |||
| 47 | public boolean isZero() |
||
| 48 | { |
||
| 49 | return ((x == 0.0f) && (y == 0.0f)); |
||
| 50 | } |
||
| 51 | |||
| 52 | public float squaredLength() |
||
| 53 | { |
||
| 54 | return (x*x + y*y); |
||
| 55 | } |
||
| 56 | |||
| 57 | public void addVector(Vector2 v) |
||
| 58 | { |
||
| 59 | x += v.x; |
||
| 60 | y += v.y; |
||
| 61 | } |
||
| 62 | |||
| 63 | public void subtractVector(Vector2 v) |
||
| 64 | { |
||
| 65 | x -= v.x; |
||
| 66 | y -= v.y; |
||
| 67 | } |
||
| 68 | |||
| 69 | public float getAngle() |
||
| 70 | { |
||
| 71 | float angle = (float)Math.atan2(x, y); |
||
| 72 | return MathUtil.stayInDegrees0to360(MathUtil.radToDeg(angle)); |
||
| 73 | } |
||
| 74 | |||
| 75 | public float dotProduct(Vector2 v) |
||
| 76 | { |
||
| 77 | return dotProduct(this, v); |
||
| 78 | } |
||
| 79 | |||
| 80 | static public float dotProduct(Vector2 a, Vector2 b) |
||
| 81 | { |
||
| 82 | return (a.x*b.x + a.y*b.y); |
||
| 83 | } |
||
| 84 | |||
| 85 | static public float distance(Vector2 a, Vector2 b) |
||
| 86 | { |
||
| 87 | Vector2 c = new Vector2(b.x - a.x, b.y - a.y); |
||
| 88 | return c.length(); |
||
| 89 | } |
||
| 90 | |||
| 91 | static public Vector2 fromRotation(float angle, float length) |
||
| 92 | { |
||
| 93 | Vector2 result = fromRotation(angle); |
||
| 94 | result.setLength(length); |
||
| 95 | return result; |
||
| 96 | } |
||
| 97 | |||
| 98 | static public Vector2 fromRotation(float angle) |
||
| 99 | { |
||
| 100 | return new Vector2((float)Math.cos(MathUtil.degToRad(angle)), (float)Math.sin(MathUtil.degToRad(angle))); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** Return a copy of this Vector2. */ |
||
| 104 | public Vector2 copy() |
||
| 105 | { |
||
| 106 | return new Vector2(x, y); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 |