/*
* File name: SutherlandHodgmanClipping.java (package eas.miscellaneous.useful.polygonClipping)
* Author(s): http://rosettacode.org/wiki/Sutherland-Hodgman_polygon_clipping
* Java version: 8.0 (at generation time)
* Generation date: 09.06.2014 (10:37:12)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* (NOTE: If Rosetta code is offering more restrictive terms, those have to be
* met, too! Which I don't think is the case...)
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse
* you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same or a similar license to
* this one.
*
* + Detailed license conditions (Germany):
* http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
* http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/
package eas.miscellaneous.useful.polygonClipping;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Line2D;
import javax.swing.JPanel;
import eas.math.geometry.Polygon2D;
import eas.math.geometry.Vector2D;
/**
* The Sutherland-Hodgman clipping algorithm finds the polygon that is the
* intersection between an arbitrary polygon (the "subject polygon") and a
* convex polygon (the "clip polygon"). It is used in computer graphics
* (especially 2D graphics) to reduce the complexity of a scene being
* displayed by eliminating parts of a polygon that do not need to be displayed.
*
* @author http://rosettacode.org/wiki/Sutherland-Hodgman_polygon_clipping
*/
class SutherlandHodgmanClipping extends JPanel {
private static final long serialVersionUID = 8207420193440520664L;
Polygon2D p1, p2;
Polygon2D result;
public SutherlandHodgmanClipping() {
setPreferredSize(new Dimension(600, 500));
// these subject and clip points are assumed to be valid
Polygon2D subjPoints = new Polygon2D();
subjPoints.add(new Vector2D(50, 150));
subjPoints.add(new Vector2D(200, 50));
subjPoints.add(new Vector2D(350, 150));
subjPoints.add(new Vector2D(350, 300));
subjPoints.add(new Vector2D(250, 300));
subjPoints.add(new Vector2D(200, 250));
subjPoints.add(new Vector2D(150, 350));
subjPoints.add(new Vector2D(100, 250));
subjPoints.add(new Vector2D(100, 200));
Polygon2D clipPoints = new Polygon2D();
clipPoints.add(new Vector2D(125, 40));
clipPoints.add(new Vector2D(300, 100));
clipPoints.add(new Vector2D(350, 350));
clipPoints.add(new Vector2D(200, 350));
clipPoints.add(new Vector2D(100, 300));
p1 = new Polygon2D(subjPoints);
p2 = new Polygon2D(clipPoints);
result = p1.clipPolygonOneConvex(p2);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.translate(80, 60);
g2.setStroke(new BasicStroke(3));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
drawPolygon(g2, p1, Color.blue);
drawPolygon(g2, p2, Color.red);
drawPolygon(g2, result, Color.green);
}
private void drawPolygon(Graphics2D g2, Polygon2D points, Color color) {
g2.setColor(color);
int len = points.size();
for (int i = 0; i < len; i++) {
Vector2D p1 = points.get(i);
Vector2D p2 = points.get((i + 1) % len);
g2.draw(new Line2D.Double(p1.x, p1.y, p2.x, p2.y));
}
}
}