1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| package net.phatcode.rel;
public class Vector2D {
public static final float EPSILON = 0.0000000001f;
public float x; public float y; public Vector2D() { x = 0; y = 0; } public Vector2D( float a, float b ) { x = a; y = b; } public Vector2D( Vector2D v ) { x = v.x; y = v.y; } public float magnitude() { return (float)Math.sqrt( (x * x) + (y * y) ); } public float magnitudeSquared() { return ( (x * x) + (y * y) ); } public float dot( Vector2D v ) { return ( (x * v.x) + (y * v.y) ); } public Vector2D normal( boolean side ) { return ( side == false ) ? new Vector2D( -y, x ) : new Vector2D( y, -x ); } public float normalize() { float length = magnitude(); float invMag = 1.0f/length; if( length <= EPSILON ) return 0.0f; x *= invMag; y *= invMag; return length; } public void scale( float s ) { x *= s; y *= s; } public float angleBetween( Vector2D v ) { return (float)(Math.atan2(v.y - y, v.x - x)); } public Vector2D reflect( Vector2D n ) { float dot = (x * n.x) + (y * n.y); Vector2D n2 = new Vector2D( n.x * -2.0f, n.y * -2.0f ); n2.scale(dot); n2.x += x; n2.y += y; return n2; } public void displayComponents() { System.out.println( "v.x = " + x + " :: v.y = " + y ); } public String toString() { return ( "v.x = " + x + " :: v.y = " + y ); } public static float magnitude( Vector2D v ) { return v.magnitude(); } public static float magnitudeSquared( Vector2D v ) { return v.magnitudeSquared(); } public static float normalize( Vector2D v ) { float length = v.magnitude(); float invMag = 1.0f/length; if( length <= EPSILON ) return 0.0f; v.x *= invMag; v.y *= invMag; return length; } public static float dot( Vector2D a, Vector2D b ) { return ( (a.x * b.x) + (a.y * b.y) ); } public static Vector2D Projection( Vector2D u, Vector2D v ) { float dotUV = dot(u,v); float dotUU = dot(u,u); u.scale( dotUV/dotUU ); return u; } public static Vector2D Normal( Vector2D v, boolean side ) { return ( side == false ) ? new Vector2D( -v.y, v.x ) : new Vector2D( v.y, -v.x ); } public static Vector2D Normal( Vector2D v1, Vector2D v2, boolean side ) { Vector2D v = new Vector2D( v2.x - v1.x, v2.y - v1.y ); return ( side == false ) ? new Vector2D( -v.y, v.x ) : new Vector2D( v.y, -v.x ); } public static float Distance( Vector2D a, Vector2D b ) { Vector2D d = new Vector2D( a.x - b.x, a.y - b.y ); return d.magnitude(); }
public static float angleBetween( Vector2D a, Vector2D b ) { return (float)(Math.atan2(b.y - a.y, b.x - a.x)); } public static Vector2D reflect( Vector2D v, Vector2D n ) { float dot = (v.x * n.x) + (v.y * n.y); Vector2D n2 = new Vector2D( n.x * -2.0f, n.y * -2.0f ); n2.scale(dot); n2.x += v.x; n2.y += v.y; return n2; } public static Vector2D midPoint( Vector2D a, Vector2D b ) { return new Vector2D( (b.x - a.x)/2.0f, (b.y - a.y)/2.0f ); } public static Vector2D randomVector( float maxX, float maxY ) { return new Vector2D( (float)(Math.random() * maxX), (float)(Math.random() * maxY) ); }
}
|