P7 Codes


P7A.java

import java.io.*;
import java.awt.*;

public class P7Graphics extends Frame
{
    // In the following two methods, real coordinate (x,y) is mapped to
    // pixel (400+x,300-y), with the usual rounding. 

    // Displays P  with color c.
    public void showPoly(Graphics g, closedPolyline P, Color c)
    {
       point[] vertex = P.get_absVertex();
       int n = vertex.length;
       int[] h = new int[n];
       int[] v = new int[n];
       for(int k=0;k< n;k++)
       {
          h[k] = (int) (400 + vertex[k].get_x());
          v[k] = (int) (300 - vertex[k].get_y());
       }
       g.setColor(c);
       g.fillPolygon(h,v,h.length);   
    }
    
    // Displays the point P 
    public void showPoint(Graphics g, point P)
    {
       int hc = (int)(400 + P.get_x());
       int vc = (int)(300 - P.get_y());
       g.setColor(Color.black);
       g.fillOval(hc-5,vc-5,10,10);
    }
    
    
	public void paint(Graphics g)
	{
	   // After you get the regular polygon constructor working...
	   
	   polygon P1 = new polygon(new point(-300,200),6,70);
	   showPoly(g,P1,Color.magenta);
	   
	   // After you get centroid working...
	   
	   point theCentroid;
	   theCentroid = P1.centroid();
	   showPoint(g,theCentroid);
	   g.drawString("Centroid = " + theCentroid.pointString(),150,50);
	   P1.translate(600,-400);
	   showPoly(g,P1,Color.blue);
	   theCentroid = P1.centroid();
	   showPoint(g,theCentroid);
	   g.drawString("Centroid = " + theCentroid.pointString(),650,575);
	   
	   // After you get the general constructor working...
	   
	   double[] theta2 = {0, Math.PI/3,(2/3.)*Math.PI, Math.PI, (4/3.)*Math.PI, (5/3.)*Math.PI };
	   double[] r2 = {120,40,120,40,120,40};
	   polygon P2 = new polygon(new point(250,150),theta2,r2);
	   showPoly(g,P2,Color.red);
	   showPoint(g,P2.centroid());
	   
	   // After you get normalize working ....
	   
	   g.drawLine(200,300,600,300);
	   g.drawLine(400,100,400,500);
	   P1.normalize();
	   P2.normalize();
	   showPoly(g,P1,Color.blue);
	   showPoly(g,P2,Color.red);
	   showPoint(g,P1.centroid());
	  
	   // After you get intersect working ...
	   
	   
	   polygon P3 = new polygon(new point(-280,-170),8,100);
	   showPoly(g,P3,Color.magenta);
	   polygon P4 = new polygon(new point(-280,-170),5,50);
	   showPoly(g,P4,Color.orange);
	   polygon P5 = new polygon(new point(-150,-170),7,80);
	   showPoly(g,P5,Color.green);
	   
	   g.setColor(Color.black);
	   if(P3.intersect(P4))
	       g.drawString("P3 and P4 intersect",350,530);
	   else
	       g.drawString("P3 and P4 do not intersect",350,530);
	   if(P3.intersect(P5))
	       g.drawString("P3 and P5 intersect",350,550);
	   else
	       g.drawString("P3 and P5 do not intersect",350,550);
	   if(P4.intersect(P5))
	       g.drawString("P4 and P5 intersect",350,570);
	   else
	       g.drawString("P4 and P5 do not intersect",350,570);
	   
	   
	}
}

public class P7A
{
	public static void main(String args[])
	{
		P7Graphics d = new P7Graphics();
		d.resize(800,600);                    
		d.move(0,75);
		d.setTitle("Drawing");                 
		d.show();
		d.toFront();
	}
}

Geometry.java

// An instance of this class is a point in the xy-plane.
public class point
{
   private double x;
   private double y;
   
   // The Constructors:
   
   public point(double xVal, double yVal)
   {
      x = xVal;
      y = yVal;
   }

   public point(point P)
   {
      x = P.x;
      y = P.y;
   }

   
   // Move the point a units in the x-direction and b units in the y-direction.
   public void translate(double a, double b)
   {
      x = x+a;
      y = y+b;
   }
   
   // Yields the x-coordinate of the point.
   public double get_x()
   {
      return x;
   }
   
   // Yields the y-coordinate of the point.
   public double get_y()
   {
      return y;
   }
   
   // Yields the distance from P0 to P1.
   public static double dist(point P0, point P1)
   {
      return Math.sqrt((P0.x-P1.x)*(P0.x-P1.x) + (P0.y-P1.y)*(P0.y-P1.y));
   }
   
   // Yields the centroid of the triangle defined by P0, P1, and P2.
   public static point triangleCentroid(point P0, point P1, point P2)
   {
      return new point((P0.get_x()+P1.get_x()+P2.get_x())/3,(P0.get_y()+P1.get_y()+P2.get_y())/3);
   }
   
   // Yields the area of the triangle defined by P0, P1, and P2.
   public static double triangleArea(point P0, point P1, point P2)
   {
      double a = dist(P0,P1);
      double b = dist(P1,P2);
      double c = dist(P2,P0);
      double s = (a+b+c)/2;
      return Math.sqrt(s*(s-a)*(s-b)*(s-c));
   }
   
   // Yields true if the line segment that connects P0 and P1 intersects the line segment 
   // that connects Q0 and Q1. Assume P0 and P1 are distinct and Q0 and Q1 are distinct.
   public static boolean intersect(point P0, point P1, point Q0, point Q1)
   {
      double a0 = P0.get_x(); double b0 = P0.get_y();
      double a1 = P1.get_x(); double b1 = P1.get_y(); 
      double c0 = Q0.get_x(); double d0 = Q0.get_y(); 
      double c1 = Q1.get_x(); double d1 = Q1.get_y(); 
      double denom = (a0-a1)*(d1-d0)-(b0-b1)*(c1-c0);
      if (denom!=0)
      {
         // The line defined by P0 and P1 is not parallel to the line defined by Q0 and Q1.
         double tP = ((a0-c0)*(d1-d0)-(b0-d0)*(c1-c0))/denom;
         double tQ = ((a0-a1)*(b0-d0)-(b0-b1)*(a0-c0))/denom;
         // (a0+tP*(a1-a0),b0+tP*(b1-b0)) = (c0+tQ*(c1-c0),d0+tQ*(d1-d0)) = intersection point
         return ( 0< =tP && tP < =1 && 0< =tQ && tQ< =1 );
      }
      else
         // The line defined by P0 and P1 is parallel to the line defined by Q0 and Q1.
          if (triangleArea(P0,P1,Q0) + triangleArea(P0,P1,Q1) > 0)
             // These lines do not intersect.
             return false;
          else
          {
             // The two line segments lie on the same line. Do they overlap?
             boolean Q0onP0P1 = dist(P0,Q0)+dist(Q0,P1) == dist(P0,P1);
             boolean Q1onP0P1 = dist(P0,Q1)+dist(Q1,P1) == dist(P0,P1);
             boolean P0onQ0Q1 = dist(Q0,P0)+dist(P0,Q1) == dist(Q0,Q1);
             boolean P1onQ0Q1 = dist(Q0,P1)+dist(P1,Q1) == dist(Q0,Q1);
             return Q0onP0P1 || Q1onP0P1 || P0onQ0Q1 || P1onQ0Q1;
          }       
         
   }
   
   // Yields a string representation of the point.
   public String pointString()
   {
      return "(" + String.valueOf(x) + "," + String.valueOf(y) + ")";
   }
}

// An instance of this class is a closed polygonal line.
public class closedPolyline
{	
        protected point alpha;          // The reference point.
	protected int n;                // Number of vertices.
	protected point[] relVertex;    // The array of vertices.
	
	// The Constructors:
	
	public closedPolyline()
	{
	}
	
	public closedPolyline(point alpha_point, point[] v)
    {
       alpha = alpha_point;
       n = v.length;
       relVertex = new point[n];
       for(int k=0;k < n;k++)
          relVertex[k] = new point(v[k]);
    } 
    
    // Yields an array whose k-th component is the k-th absolute vertex.
    public point[] get_absVertex()
    {
       point[] q = new point[n];
       for (int k=0;k < n;k++)
          q[k] = new point(alpha.get_x() + relVertex[k].get_x(),alpha.get_y() + relVertex[k].get_y());
       return q;
    }
    
    // Translates each absolute vertex a units in the x-direction and b units in the y-direction.
    public void translate(double a, double b)
    {
       alpha.translate(a,b);
    }   
    
    // Yields the length of the closed polygonal line     
    public double perimeter()
    {
       double s=0;
       for(int k=0;k < n;k++)
          s = s + point.dist(relVertex[k],relVertex[(k+1)%n]);
       return s;
    }    
}


P7B.java

import java.io.*;
import java.awt.*;

public class ShowP7b extends Frame
{
    // Displays the chessboard showing threatened tiles.
    public void showThreats(Graphics g, chess C)
    {
       int L = 50; 
       int T = 50;
       int s = 60;
       String piece;
       g.setFont(new Font("TimesRoman",Font.PLAIN, 18));
       for(int r=0;r<=7;r++)
          for(int c=0;c<=7;c++)
          {
             if(C.getThreat(r,c))
                g.setColor(Color.magenta);
             else
                g.setColor(Color.white);
             g.fillRect(L+c*s,T+r*s,s,s);
             g.setColor(Color.black);
             g.drawRect(L+c*s,T+r*s,s,s);
             g.drawString(C.getPiece(r,c),25+L+c*s,35+T+r*s);     
          }
    }
  
	public void paint(Graphics g)
	{
	   String[][] B = { 
	                     {"" , ""   , "" , ""  , "" , ""  , "" , "" }, 
	                     {"" , "Kn" , "" , ""  , "" , ""  , "" , "" }, 
	                     {"" , ""   , "" , "K" , "" , ""  , "" , "B"}, 
	                     {"" , "R"  , "" , ""  , "" , "B" , "" , "" }, 
	                     {"" , "Kn" , "" , ""  , "" , ""  , "" , "" }, 
	                     {"" , ""   , "" , ""  , "" , ""  , "" , "" }, 
	                     {"" , ""   , "" , ""  , "" , ""  , "" , "" }, 
	                     {"" , ""   , "" , "R" , "" , ""  , "" , "Q"}
	                   }; 
	  
	   chess C = new chess(B);
	  
	   showThreats(g,C);
	
	}
	
}

public class P7B
{
	public static void main(String args[])
	{
		ShowP7b d = new ShowP7b();
		d.resize(800,600);                    
		d.move(0,75);
		d.setTitle("Chess");                 
		d.show();
		d.toFront();
	}
}

chess.java

public class chess
{	
    private String[][]  pieceBoard;
    private boolean[][] threatBoard;   
    
    // An instance of this class is chessboard with the piece positions indicated by pieceBoard
    // and the threatened positions indicated by threatBoard. These are both 8-by-8 arrays.
    
    // pieceBoard[i][j] = "K", "Q", "R", "B", "Kn", or "" according to whether there is a King,
    // Queen, Rook, Bishop, Knight, or nothing at position (i,j).
    
    // threatBoard[i][j] is true if position (i,j) is threatened and is false otherwise.
    
    // Constructor. B must be 8-by-8 with entrie equal to "K", "Q", "R", "B", "Kn" or "".
    // Thus, B specifies the location of the pieces.
    public chess(String[][] B)
    {
       pieceBoard  = new String[8][8];
       threatBoard = new boolean[8][8];
       
       for(int r=0;r<=7;r++)
          for(int c=0;c<=7;c++)
             pieceBoard[r][c] = new String(B[r][c]);
       for(int r=0;r<=7;r++)
          for(int c=0;c<=7;c++)
             threatBoard[r][c] =  kingThreat(r,c)||knightThreat(r,c)||qrbThreat(r,c);
    }
    
    // Yields true if position (r,c) is threatened.
    public boolean getThreat(int r, int c) { return threatBoard[r][c];}
    
    // Yields the piece situated at position (r,c).
    public String getPiece(int r, int c) {return new String(pieceBoard[r][c]);}
     
    // Yields true if (r,c) is a valid board position.  
	public static boolean onBoard(int r, int c){return 0<=r && r<=7 && 0<=c && c<=7;}
	
	// Yields true if position (r,c) is threatened by a King.
	public boolean kingThreat(int r, int c)
	{
	   if       (onBoard(r-1,c-1) && pieceBoard[r-1][c-1].equals("K")) {return true;}
	    else if  (onBoard(r-1,c )  && pieceBoard[r-1][c  ].equals("K")) {return true;}
       else if  (onBoard(r-1,c+1) && pieceBoard[r-1][c+1].equals("K")) {return true;}
       else if  (onBoard(r  ,c-1) && pieceBoard[r  ][c-1].equals("K")) {return true;}
       else if  (onBoard(r  ,c+1) && pieceBoard[r  ][c+1].equals("K")) {return true;}
       else if  (onBoard(r+1,c-1) && pieceBoard[r+1][c-1].equals("K")) {return true;}
       else if  (onBoard(r+1,c  ) && pieceBoard[r+1][c  ].equals("K")) {return true;}
       else if  (onBoard(r+1,c+1) && pieceBoard[r+1][c+1].equals("K")) {return true;}
       else {return false;}
    }
    
    // Yields true if position (r,c) is threatened by a Knight.
    public boolean knightThreat(int r, int c)
	{
	   if        (onBoard(r-2,c-1) && pieceBoard[r-2][c-1].equals("Kn")) {return true;}
	    else if  (onBoard(r-2,c+1) && pieceBoard[r-2][c+1].equals("Kn")) {return true;}
       else if  (onBoard(r-1,c-2) && pieceBoard[r-1][c-2].equals("Kn")) {return true;}
       else if  (onBoard(r-1,c+2) && pieceBoard[r-1][c+2].equals("Kn")) {return true;}
       else if  (onBoard(r+1,c-2) && pieceBoard[r+1][c-2].equals("Kn")) {return true;}
       else if  (onBoard(r+1,c+2) && pieceBoard[r+1][c+2].equals("Kn")) {return true;}
       else if  (onBoard(r+2,c-1) && pieceBoard[r+2][c-1].equals("Kn")) {return true;}
       else if  (onBoard(r+2,c+1) && pieceBoard[r+2][c+1].equals("Kn")) {return true;}
       else return false;
    }
    
    // Yields true if position (r,c) is threatened by a Queen, a Rook, or a Bishop.
    public boolean qrbThreat(int r, int c)
    {
        return false;
    }
}