import java.awt.Point; import java.awt.Color; /** * Abstract representation of a polygon. * Includes code for clipping. * Subclasses need to provide implementation of drawing. */ abstract public class CG1Polygon { public static final int LEFT = 0; public static final int RIGHT = 1; public static final int BOTTOM = 2; public static final int TOP = 3; public static final int X = 0; public static final int Y = 1; public static final int Z = 2; /// unclipped vertices (in screen coordinates) protected CG1Point[] vertices; /// clipped vertices (in screen coordinates) protected int[][] clipped; /// depths associated with the vertices protected double[] depth; protected int numVertex; protected Color color; protected Point wMin, wMax; protected int cnt; protected CG1Point[] first; protected CG1Point[] s; public CG1Polygon (CG1Point[] _vertices, Color _color) { vertices = _vertices; color = _color; numVertex = vertices.length; clipped = new int[2*numVertex + 1][2]; depth = new double[2*numVertex + 1]; first= new CG1Point[4]; s= new CG1Point[4]; } /** * Draw the clipped polygon. */ abstract protected void drawClipped (CG1Canvas canvas); /** * Clip vertices of the polygon, then call the specific method for drawing. */ public void draw (CG1Canvas canvas) { clip (canvas); drawClipped (canvas); } /** * Find out if a point is inside the clipping window, as seen from the specified edge. */ protected boolean inside (CG1Point point, int edge) { /** * TODO 2: * - Implement this method. */ CG1Point p=point; //System.out.println("insidep:"+p+"||Edge:"+edge); switch (edge){ case LEFT: if(p.xwMax.x){ return false;} break; // else { return true; } //rechte clippingGrenze case BOTTOM: if(p.ywMax.y) {return false;} break;// else {return true; }//oben } return true; } /** * Find out if the line between two vertices cross the specified edge. */ protected boolean cross (CG1Point p1, CG1Point p2, int edge) { /** * TODO 2: * - Implement this method. */ CG1Point p1_=p1; CG1Point p2_=p2; if (inside(p1_, edge)==inside(p2_, edge)){ return false; //wenn inside fuer beide Pkte das gleiche ergibt } return true; } /** * Calculate intersection point of a line and a specified edge. */ protected CG1Point intersect (CG1Point p1, CG1Point p2, int edge) { /** * TODO 2: * - Implement this method. * - Note that our points have a z-component, which should also be set correctly for an intersection point! * To do this, calculate an interpolated value at the right distance between the z values of p1 and p2. * (This is an addition to the algorithm implementation in the Hearn&Baker book.) */ CG1Point ipt=new CG1Point(0.0, 0.0, 0.0); double t; //Verhaeltnis groesserem Dreieck zu kleinerem double s; switch (edge){ case LEFT: t=(wMin.x-p1.x)/(p2.x-p1.x); ipt.x=wMin.x; ipt.y=p1.y+t*(p2.y-p1.y); ipt.z=p1.z+t*(p2.z-p1.z); break; case RIGHT: t=(wMax.x-p1.x)/(p2.x-p1.x); ipt.x=wMax.x; ipt.y=p1.y+t*(p2.y-p1.y); ipt.z=p1.z+t*(p2.z-p1.z); break; case BOTTOM: t=(wMin.y-p1.y)/(p2.y-p1.y); ipt.y=wMin.y; ipt.x=p1.x+t*(p2.x-p1.x); ipt.z=p1.z+t*(p2.z-p1.z); break; case TOP: t=(wMax.y-p1.y)/(p2.y-p1.y); ipt.x=p1.x+t*(p2.x-p1.x); ipt.y=wMax.y; ipt.z=p1.z+t*(p2.z-p1.z); break; } if (edge<4) { return(ipt); }else{ return(null); } } /** * Clip a point to the specified edge. */ protected void clipPoint (CG1Point point, int edge) { /** * TODO 2: * - Implement clipping using the Sutherland-Hodgeman algorithm. * - Note that our points have a z-components. To 'output' a final clipped point, * put its x/y-coordinates into the clipped[] array, and its associated z-coordinate into depth[]. * (This is an addition to the algorithm implementation in the Hearn&Baker book.) */ CG1Point p=point; //System.out.println("clipPointp:"+p); if (first[edge]==null){ first[edge]=p; //erster Punkt, der bei der Kante ueberprueft wird, wird in das first[] gespeichert }else{ //wenn schon ein Punkt an der Kante behandelt worden ist //System.out.println("cross:"+(cross(p, s[edge], edge))); if (cross(p, s[edge], edge)){ //schneide die aktuelle Kante die vorherige an der Kante behandelte? CG1Point ipt = intersect(p, s[edge], edge); //Schnittpunkt wird ausgerechnet //System.out.println("intersect ipt:"+ipt); if (edge