Hello, I am stuck on this Polygon Clipping Assignment.
This is what the result should look like.
clipTest.jpg
Here's the package I am working with. It's java so under the java folders. I am only allowed to edit the Clipper.java file.
cg1-clip.zip
This is what I have so far:
// // Clipper.java // // // Created by Joe Geigel on 1/21/10. // Copyright 2010 __MyCompanyName__. All rights reserved. // /** * Object for performing clipping * */ public class clipper { /** * clipPolygon * * Clip the polygon with vertex count in and vertices inx/iny * against the rectangular clipping region specified by lower-left corner * (x0,y0) and upper-right corner (x1,y1). The resulting vertices are * placed in outx/outy. * * The routine should return the with the vertex count of polygon * resultinhg from the clipping. * * @param in the number of vertices in the polygon to be clipped * @param inx - x coords of vertices of polygon to be clipped. * @param int - y coords of vertices of polygon to be clipped. * @param outx - x coords of vertices of polygon resulting after clipping. * @param outy - y coords of vertices of polygon resulting after clipping. * @param x0 - x coord of lower left of clipping rectangle. * @param y0 - y coord of lower left of clipping rectangle. * @param x1 - x coord of upper right of clipping rectangle. * @param y1 - y coord of upper right of clipping rectangle. * * @return number of vertices in the polygon resulting after clipping * */ public float intersectX; public float intersectY; public float clipXmin; public float clipYmin; public float clipYmax; public float clipXmax; public int clipBorder; public int clipPolygon(int in, float inx[], float iny[], float outx[], float outy[], float x0, float y0, float x1, float y1) { int numberOfVerticies = 0; int n; float Lx,Ly,Cx,Cy; Lx = inx[in-1]; Ly = iny[in-1]; for (n = 0; n < in; n++){ Cx = inx[n]; Cy = iny[n]; if(isInside(Cx, Cy, clipBorder)){ if(isInside(Lx,Ly,clipBorder)){ outx[numberOfVerticies] = Cx; outy[numberOfVerticies] = Cy; numberOfVerticies++; } else{ isIntersect(Lx,Ly,Cx,Cy,clipBorder); outx[numberOfVerticies] = intersectX; outy[numberOfVerticies] = intersectY; numberOfVerticies++; outx[numberOfVerticies] = Cx; outy[numberOfVerticies] = Cy; numberOfVerticies++; } } else{ if(isInside(Lx,Ly,clipBorder)){ isIntersect(Lx,Ly,Cx,Cy,clipBorder); outx[numberOfVerticies] = intersectX; outy[numberOfVerticies] = intersectY; numberOfVerticies++; } } Lx = Cx; Ly = Cy; } return numberOfVerticies; // should return number of verricies in clipped poly. } public boolean isInside(float newX, float newY, int clipBorder){ if(clipBorder == 0 && newY > clipYmin){ return true; } else if(clipBorder == 1 && newX > clipXmin){ return true; } else if(clipBorder == 2 && newY < clipYmax){ return true; } else if (clipBorder == 3 && newX < clipXmax){ return true; } return false; } public void isIntersect(float x0, float x1, float y0, float y1, int clipBorder){ float xDifference = x1 -x0; float yDifference = y1 - y0; if(xDifference == 0 || yDifference == 0){ if(clipBorder == 0){ intersectX = x0; intersectY = clipYmin; } else if(clipBorder == 1){ intersectX = clipXmin; intersectY = y0; } else if(clipBorder == 2){ intersectX = x0; intersectY = clipYmax; } else if(clipBorder == 3){ intersectX = clipXmax; intersectY = y0; } return; } if(clipBorder == 0){ //intersectX = x0 + ((((((int)xDifference) * ((int)clipYmin - (int)y0))<<8) / ((int)yDifference))>>8); intersectX = x0 + ((clipYmax - y0)/(y1-y0/x1-x0)); intersectY = clipYmin; } else if(clipBorder == 1){ intersectX = clipXmin; intersectY = (y1-y0/x1-x0)*(clipXmax-x0) + y0; //intersectY = y0 + ((((((int)yDifference) * ((int)clipXmin - (int)x0))<<8) / ((int)xDifference))>>8); } else if(clipBorder == 2){ //intersectX = x0 + ((((((int)xDifference) * ((int)clipYmax - (int)y0))<<8) / ((int)yDifference))>>8); intersectX = x0+((clipYmin-y0)/(y1-y0/x1-x0)); intersectY = clipYmax; } else if(clipBorder == 3){ intersectX = clipXmax; //intersectY = y0 + ((((((int)yDifference) * ((int)clipXmax - (int)x0))<<8) / ((int)xDifference))>>8); intersectY = (y1-y0/x1-x0)*(clipXmax-x0) + y0; } } }