/* Smart Rover */ import java.awt.*; import java.util.Vector; public class Rover { public Polygon body; public int width, height, angle, cmx, cmy; private int myxpoints[] = new int [4]; private int myypoints[] = new int [4]; public Path myApplet; public Rover(Path applet) { myApplet = applet; height = 20; width =30; angle = 0; cmx = 15; cmy = 10; myxpoints[0] = cmx - width/2; myxpoints[1] = cmx - width/2; myxpoints[2] = myxpoints[0] + width - 1; myxpoints[3] = myxpoints[1] + width - 1; myypoints[0] = cmy - height/2; myypoints[1] = cmy + height/2; myypoints[2] = myypoints[1]; myypoints[3] = myypoints[0]; Polygon tmp = new Polygon(myxpoints, myypoints, 4); // rotate by angle double f = angle * Math.PI / 180.0; for (int i=0;i<4;i++) { myxpoints[i] += (tmp.xpoints[i] - cmx) * (Math.cos(f)-1) - (tmp.ypoints[i] - cmy) * Math.sin(f); myypoints[i] += (tmp.xpoints[i] - cmx) * Math.sin(f) + (tmp.ypoints[i] - cmy) * (Math.cos(f)-1); } body = new Polygon(myxpoints, myypoints, 4); } public void put (int cmx, int cmy, int rover_width, int rover_height, int angle) { myxpoints[0] = cmx - rover_width/2; myxpoints[1] = cmx - rover_width/2; myxpoints[2] = myxpoints[0] + rover_width - 1; myxpoints[3] = myxpoints[1] + rover_width - 1; myypoints[0] = cmy - rover_height/2; myypoints[1] = cmy + rover_height/2; myypoints[2] = myypoints[1]; myypoints[3] = myypoints[0]; Polygon tmp = new Polygon(myxpoints, myypoints, 4); // rotate by angle double f = angle * Math.PI / 180.0; for (int i=0;i<4;i++) { myxpoints[i] += (tmp.xpoints[i] - cmx) * (Math.cos(f)-1) - (tmp.ypoints[i] - cmy) * Math.sin(f); myypoints[i] += (tmp.xpoints[i] - cmx) * Math.sin(f) + (tmp.ypoints[i] - cmy) * (Math.cos(f)-1); } this.cmx = cmx; this.cmy = cmy; this.angle = angle; body = new Polygon(myxpoints, myypoints, 4); } public boolean move (int dx, int dy, D_Obstacle obstacles[], int num_obstacle) { // this rover automatically shuns all obstacles // returns if the move is successful int i, j; boolean success = true; if ((Math.abs(dx) > 1) || (Math.abs(dy) > 1)) return (false); for (i=0; i<4; i++) { myxpoints[i] = body.xpoints[i] + dx; myypoints[i] = body.ypoints[i] + dy; } Polygon trial = new Polygon(myxpoints, myypoints, 4); for (j=1; (j