/* PROBLEM: Write a program with 2 classes ? Pacman and Director. The aim is to draw 4 pacman images at positions which would be taken as input from the user. The 4 pacmen should be so drawn that they are looking (targetting) at each other in cyclic fashion. Lets call them A,B,C,D. Then A should be looking at B, B at C, C at D, and D at A. */ import javabook.*; import java.awt.*; import java.lang.*; class Pacman { //Attributes private int xPosition, yPosition; private int alpha; private int theta; //Methods public void setX(int x) { xPosition = x; } public void setY(int y) { yPosition = y; } public void setAlpha(int a) { alpha = a; } public void setTheta(int t) { theta = t; } public void Draw(Graphics g) { g.fillArc(xPosition,yPosition,80,80,-alpha+theta/2,360-theta); } } class Director { public static void main(String[] args) { int x1,y1,x2,y2,x3,y3,x4,y4; double a1,t1,a2,t2,a3,t3,a4,t4; int Height = 600; Pacman p1,p2,p3,p4; Graphics g; MainWindow mainWindow; InputBox inputBox; mainWindow = new MainWindow("Pac Men"); mainWindow.setVisible(true); inputBox = new InputBox(mainWindow); g = mainWindow.getGraphics(); g.setColor(Color.black); //Take in the values of (xa,ya) for each pacman x1 = inputBox.getInteger("Enter x co-ordinate for first Pacman"); y1 = inputBox.getInteger("Enter y co-ordinate for first Pacman"); x2 = inputBox.getInteger("Enter x co-ordinate for second Pacman"); y2 = inputBox.getInteger("Enter y co-ordinate for second Pacman"); x3 = inputBox.getInteger("Enter x co-ordinate for third Pacman"); y3 = inputBox.getInteger("Enter y co-ordinate for third Pacman"); x4 = inputBox.getInteger("Enter x co-ordinate for fourth Pacman"); y4 = inputBox.getInteger("Enter y co-ordinate for fourth Pacman"); /* In the following lines, alpha for each pacman is calculated, as is theta. Alpha is calculated from the slope of the line connecting the two centres. A correction of 180 degrees is added when xa > xb since Math.atan has a range of -PI/2 to +PI/2. Also, when xa = xb, the value of the slope is infinity and alpha is set manually to 90. Theta is calculated from the tan inverse of the radius of a pac man divided by the distance between the centres of the pac men. */ //PacMan 1 if (x1 != x2) { a1 = Math.toDegrees(Math.atan((double)(y1-y2)/(x1-x2))); if(x1>x2) a1=180+a1; } else { if(y2>y1) a1 = 90; else a1 = -90; } t1=Math.toDegrees(2.0*Math.atan(40.0/Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)))); //PacMan 2 if (x2 != x3) { a2 = Math.toDegrees(Math.atan((double)(y2-y3)/(x2-x3))); if(x2>x3) a2=180+a2; } else { if(y3>y2) a2 = 90; else a2 = -90; } t2=Math.toDegrees(2.0*Math.atan(40.0/Math.sqrt((x2-x3)*(x2-x3) + (y2-y3)*(y2-y3)))); //PacMan 3 if (x3 != x4) { a3 = Math.toDegrees(Math.atan((double)(y3-y4)/(x3-x4))); if(x3>x4) a3=180+a3; } else { if(y4>y3) a3 = 90; else a3 = -90; } t3=Math.toDegrees(2.0*Math.atan(40.0/Math.sqrt((x3-x4)*(x3-x4) + (y3-y4)*(y3-y4)))); //PacMan 4 if (x4 != x1) { a4 = Math.toDegrees(Math.atan((double)(y4-y1)/(x4-x1))); if(x4>x1) a4=180+a4; } else { if(y1>y4) a4 = 90; else a4 = -90; } t4=Math.toDegrees(2.0*Math.atan(40.0/Math.sqrt((x4-x1)*(x4-x1) + (y4-y1)*(y4-y1)))); //Instantiate the 4 pac men and enter their attributes. p1 = new Pacman(); p2 = new Pacman(); p3 = new Pacman(); p4 = new Pacman(); p1.setX(x1); p1.setY(y1); p2.setX(x2); p2.setY(y2); p3.setX(x3); p3.setY(y3); p4.setX(x4); p4.setY(y4); p1.setAlpha ((int)a1); p1.setTheta ((int)t1); p2.setAlpha ((int)a2); p2.setTheta ((int)t2); p3.setAlpha ((int)a3); p3.setTheta ((int)t3); p4.setAlpha ((int)a4); p4.setTheta ((int)t4); //Draw the four pacman. g.setColor(Color.red); p1.Draw(g); g.setColor(Color.green); p2.Draw(g); g.setColor(Color.blue); p3.Draw(g); g.setColor(Color.black); p4.Draw(g); } }