/** Returns distance between specified coordinates. */ public static double getDistance(Point origin, Point destination) { int x = destination.getx() - origin.getx(); int y = destination.gety() - origin.gety(); double distance = Math.sqrt(x * x + y * y); return distance; }
/** Returns angle in degrees from specified origin to specified destination. */ public static int getAngle(Point origin, Point destination) // origin != destination { double distance = getDistance(origin, destination); int add = 0; int x = destination.getx() - origin.getx(); int y = origin.gety() - destination.gety(); // Java flips things around double angleRad = Math.asin(Math.abs(y) / distance); double angleDeg = Math.toDegrees(angleRad); if ((x >= 0) && (y >= 0)) // Quadrant 1 { angleDeg = angleDeg; } if ((x < 0) && (y > 0)) // Quadrant 2 { angleDeg = 180 - angleDeg; } if ((x <= 0) && (y <= 0)) // Quadrant 3 { angleDeg = 180 + angleDeg; } if ((x > 0) && (y < 0)) // Quadrant 4 { angleDeg = 360 - angleDeg; } float angleFloat = Math.round(angleDeg); int angleInt = Math.round(angleFloat); return (angleInt); }
/** Dummy test method. */ public static boolean dan(Point p1, Point p2) { // Point p1 = mech1.getLocation(); // Point p2 = mech2.getLocation(); int p1x = p1.getx(); int p1y = p1.gety(); int p2x = p2.getx(); int p2y = p2.gety(); int dx = p2x - p1x; int dy = p2y - p1y; int xdir = sign(dx); int ydir = sign(dy); // double dist = Math.sqrt(dx*dx+dy*dy); double changex = (double) Math.abs(dx); // Diff in x double changey = (double) Math.abs(dy); // Diff in y double currentx = p1x; double currenty = p1y; int nextx = p1x; int nexty = p1y; double xdist, ydist; double xcost, ycost; int width = 5; while ((p2x - nextx) * xdir > 0 || (p2y - nexty) * ydir > 0) { xdist = dist( currentx, width, xdir); // total distance need to encounter next terrain object in x direction ydist = dist(currenty, width, ydir); xcost = (changex == 0) ? 10000 : xdist / changex; ycost = (changey == 0) ? 10000 : ydist / changey; if (xcost <= ycost) ydist = xdist * (changey / changex); else xdist = ydist * (changex / changey); currentx += xdir * xdist; currenty += ydir * ydist; nextx = (int) round(currentx, ydir); nexty = (int) round(currenty, ydir); // if (Map.getNearestGrid(nextx,nexty).getType() !=0) return false; } System.out.println("Nextx: " + nextx + " Nexty: " + nexty); System.out.println("Currentx: " + currentx + " Currenty: " + currenty); return true; }