Coverage Summary for Class: Utilities (com.mygdx.utils)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| Utilities | 100% (1/1) | 44.4% (8/18) | 60% (18/30) |
1 package com.mygdx.utils; 2 3 import com.badlogic.gdx.math.MathUtils; 4 import com.badlogic.gdx.math.Vector2; 5 6 import java.util.ArrayList; 7 import java.util.Random; 8 9 import static com.mygdx.utils.Constants.TILE_SIZE; 10 11 /** 12 * Helper functions 13 */ 14 public final class Utilities { 15 public static float vectorToAngle(Vector2 v) { 16 return (float) Math.atan2(-v.x, v.y); 17 } 18 19 public static Vector2 angleToVector(Vector2 out, float angle) { 20 out.x = -(float) Math.sin(angle); 21 out.y = (float) Math.cos(angle); 22 return out; 23 } 24 25 public static float tilesToDistance(float tiles) { 26 return TILE_SIZE * tiles; 27 } 28 29 public static Vector2 tilesToDistance(Vector2 tiles) { 30 return tiles.cpy().scl(TILE_SIZE); 31 } 32 33 public static int distanceToTiles(float dist) { 34 return (int) (dist / TILE_SIZE); 35 } 36 37 public static Vector2 distanceToTiles(Vector2 dist) { 38 return dist.cpy().scl(1.0f / TILE_SIZE); 39 } 40 41 /** 42 * checks the proximity of point a to point b 43 * 44 * @param a first point 45 * @param b second point 46 * @param radius min dist to be considered close 47 * @return |dist(a, b)| < radius 48 */ 49 public static boolean checkProximity(Vector2 a, Vector2 b, float radius) { 50 final float d2 = radius * radius; 51 final float d = Math.abs(a.dst2(b)); 52 return d < d2; 53 } 54 55 public static float angleBetween(Vector2 v, Vector2 w) { 56 return MathUtils.atan2(w.y * v.x - w.x * v.y, w.x * v.x + w.y * v.y); 57 } 58 59 public static float scale(float x, float min0, float max0, float min1, float max1) { 60 return (max1 - min1) * ((x - min0 * x) / (max0 * x - min0 * x)) + min1; 61 } 62 63 public static float scale(float x, Vector2 a, Vector2 b) { 64 return (b.y - b.x) * ((x - a.x * x) / (a.y * x - a.x * x)) + b.x; 65 } 66 67 /** 68 * @param x the vector to round 69 * @return x modified for chaining 70 */ 71 public static Vector2 round(Vector2 x) { 72 x.x = Math.round(x.x); 73 x.y = Math.round(x.y); 74 return x; 75 } 76 77 /** 78 * Random Vec2 in range 79 * 80 * @param min inclusive 81 * @param max exclusive 82 * @return rand Vector2 83 */ 84 public static Vector2 randomPos(float min, float max) { 85 Random r = new Random(); 86 return new Vector2(min + r.nextFloat() * (max - min), min + r.nextFloat() * (max - min)); 87 } 88 89 /** 90 * Chooses a random element 91 * 92 * @param list source 93 * @param choice the index of the chosen element 94 * @param <T> type of element to return 95 * @return the random element 96 */ 97 public static <T> T randomChoice(ArrayList<T> list, Integer choice) { 98 choice = new Random().nextInt(list.size()); 99 return list.get(choice); 100 } 101 102 /** 103 * floors the vector 104 * 105 * @param a given vector 106 * @return new vector floored 107 */ 108 public static Vector2 floor(final Vector2 a) { 109 return new Vector2(MathUtils.floor(a.x), MathUtils.floor(a.y)); 110 } 111 112 /** 113 * helper for System.out.print 114 * 115 * @param v string 116 * @param eol eol msg 117 */ 118 public static void print(String v, String eol) { 119 System.out.print(v + eol); 120 } 121 122 123 /** 124 * helper for System.out.println 125 * 126 * @param v string 127 */ 128 public static void print(String v) { 129 System.out.println(v); 130 } 131 132 /** 133 * does array contain a 134 * 135 * @param array source 136 * @param a desired 137 * @param <T> type of element looking for 138 * @return true if contained otherwise false 139 */ 140 public static <T> boolean contains(ArrayList<T> array, T a) { 141 for (T b : array) { 142 if (b == a) { 143 return true; 144 } 145 } 146 return false; 147 } 148 }