Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Java in General
Polygon Clipping in Java
tosh li
Greenhorn
Posts: 2
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hello, I am stuck on this Polygon Clipping Assignment.
This is what the result should look like.
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.
This is what I have so far:
// // Clipper.java // // /** * 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; } } }
clipTest.jpg
tosh li
Greenhorn
Posts: 2
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Here's the files I am not to touch but I need them to run. The main class is the ClipTest.java
http://www.megafileupload.com/en/file/383064/cg1-clip-zip.html
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Painting on glass panel
Convert Applet to Stand-alone app
Moving clickedShapes and only clickedShapes on a panel
Clock face
Zooming in and out - cursor not in the right place
More...