int[] setRunningDirection(int distX, int distY) { int[] directions = new int[] {0, 0}; if (Math.abs(distX) > Math.abs(distY)) { directions[0] = (int) Math.signum(distX); } else { directions[1] = (int) Math.signum(distY); } return avoidWalls(directions); }
public Complex sqrt() { float a = abs(); float s2 = (float) Math.sqrt(2); float p = (float) Math.sqrt(a + re) / s2; float sgn = Math.signum(im); if (sgn == 0.0f) { sgn = 1.0f; } float q = (float) Math.sqrt(a - re) / s2 * Math.signum(sgn); return new Complex(p, q); }
int[] avoidWalls(int[] directions) { Node current = level.getNodeAt(pacX, pacY); Node toGoToNode = level.getNodeAt(pacX + directions[0], pacY + directions[1]); ArrayList<Edge> neighbourEdges = current.getEdges(); for (int i = 0; i < neighbourEdges.size(); i++) if (neighbourEdges.get(i).start == current && neighbourEdges.get(i).end == toGoToNode) return directions; System.out.println("collision detected"); System.out.println(directions[0] + " " + directions[1]); int randomDirection = (int) Math.signum(r.nextFloat() - .5); System.out.println(randomDirection); return new int[] {directions[1] * randomDirection, directions[0] * randomDirection}; }
/** * Helper method for translating (x,y) scroll vectors into scalar rotation of the pie. * * @param dx The x component of the current scroll vector. * @param dy The y component of the current scroll vector. * @param x The x position of the current touch, relative to the pie center. * @param y The y position of the current touch, relative to the pie center. * @return The scalar representing the change in angular position for this scroll. */ private static float vectorToScalarScroll(float dx, float dy, float x, float y) { // get the length of the vector float l = (float) Math.sqrt(dx * dx + dy * dy); // decide if the scalar should be negative or positive by finding // the dot product of the vector perpendicular to (x,y). float crossX = -y; float crossY = x; float dot = (crossX * dx + crossY * dy); float sign = Math.signum(dot); return l * sign; }