Coverage Summary for Class: Utilities (com.mygdx.utils)

Class Class, % Method, % Line, %
Utilities 100% (1/1) 93.8% (15/16) 97% (32/33)


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 double angleBetween(Vector2 a, Vector2 b) { 56  if(a.equals(new Vector2(0,0)) || b.equals(new Vector2(0,0))){ 57  return 0; 58  } 59  float dot = Vector2.dot(a.x,a.y,b.x,b.y); 60  double absA = Math.sqrt( Math.pow(a.x,2) + Math.pow(a.y,2) ); 61  double absB = Math.sqrt( Math.pow(b.x,2) + Math.pow(b.y,2) ); 62  63  return Math.acos(dot/(absA*absB)); 64  } 65  66  public static float scale(float min0, float max0, float min1, float max1) { 67  return (max1 - min1) * ((1 - min0) / (max0 - min0)) + min1; 68  } 69  70  public static float scale(Vector2 a, Vector2 b) { 71  return (b.y - b.x) * ((1 - a.x) / (a.y - a.x)) + b.x; 72  } 73  74  /** 75  * @param x the vector to round 76  * @return x modified for chaining 77  */ 78  public static Vector2 round(Vector2 x) { 79  x.x = Math.round(x.x); 80  x.y = Math.round(x.y); 81  return x; 82  } 83  84  /** 85  * Random Vec2 in range 86  * 87  * @param min inclusive 88  * @param max exclusive 89  * @return rand Vector2 90  */ 91  public static Vector2 randomPos(float min, float max) { 92  Random r = new Random(); 93  return new Vector2(min + r.nextFloat() * (max - min), min + r.nextFloat() * (max - min)); 94  } 95  96  /** 97  * Chooses a random element 98  * 99  * @param list source 100  * @param <T> type of element to return 101  * @return the random element 102  */ 103  public static <T> T randomChoice(ArrayList<T> list) { 104  int choice = new Random().nextInt(list.size()); 105  return list.get(choice); 106  } 107  108  /** 109  * floors the vector 110  * 111  * @param a given vector 112  * @return new vector floored 113  */ 114  public static Vector2 floor(final Vector2 a) { 115  return new Vector2(MathUtils.floor(a.x), MathUtils.floor(a.y)); 116  } 117  118  /** 119  * does array contain a 120  * 121  * @param array source 122  * @param a desired 123  * @param <T> type of element looking for 124  * @return true if contained otherwise false 125  */ 126  public static <T> boolean contains(ArrayList<T> array, T a) { 127  for (T b : array) { 128  if (b == a) { 129  return true; 130  } 131  } 132  return false; 133  } 134  135  //public static void print(String v, String eol) { 136  // System.out.print(v + eol); 137  // } 138  139  //public static void print(String v) { 140  // System.out.println(v); 141  // } 142 }