01: /*
02: * Geotools2 - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2002-2005, Geotools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.referencing.operation.builder;
17:
18: import org.opengis.geometry.DirectPosition;
19: import java.awt.geom.Line2D;
20: import java.util.ArrayList;
21: import java.util.List;
22:
23: /**
24: * A simple four-sided polygon.
25: *
26: * @since 2.4
27: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/main/java/org/geotools/referencing/operation/builder/Quadrilateral.java $
28: * @version $Id: Quadrilateral.java 28966 2008-01-27 17:36:43Z acuster $
29: * @author Jan Jezek
30: */
31: class Quadrilateral extends Polygon {
32: /** The first vertex. */
33: public DirectPosition p0;
34:
35: /** The second vertex. */
36: public DirectPosition p1;
37:
38: /** The third vertex */
39: public DirectPosition p2;
40:
41: /** the fourth vetrex */
42: public DirectPosition p3;
43:
44: /**
45: * Creates a Quadrilateral.
46: * @param p0 one vertex
47: * @param p1 another vertex
48: * @param p2 another vertex
49: * @param p3 another vertex
50: */
51: public Quadrilateral(DirectPosition p0, DirectPosition p1,
52: DirectPosition p2, DirectPosition p3) {
53: super (new DirectPosition[] { p0, p1, p2, p3, p0 });
54: this .p0 = p0;
55: this .p1 = p1;
56: this .p2 = p2;
57: this .p3 = p3;
58: }
59:
60: /**
61: * Test the Quadrilateral if it is a convex polygon.
62: *
63: * @return whether the diagonals intersects
64: */
65: public boolean isConvex() {
66: return Line2D.linesIntersect(p0.getCoordinates()[0], p0
67: .getCoordinates()[1], p2.getCoordinates()[0], p2
68: .getCoordinates()[1], p1.getCoordinates()[0], p1
69: .getCoordinates()[1], p3.getCoordinates()[0], p3
70: .getCoordinates()[1]);
71: }
72:
73: /**
74: * Splits the Quadrilateral into two triangles.
75: *
76: * @return two Triangles: p0-p1-p2 and p0-p3-p2
77: */
78: public List getTriangles() {
79: //Assert.isTrue(this.isValid());
80: ArrayList triangles = new ArrayList();
81: TINTriangle trigA = new TINTriangle(p0, p1, p2);
82: TINTriangle trigB = new TINTriangle(p0, p3, p2);
83:
84: try {
85: trigA.addAdjacentTriangle(trigB);
86: trigB.addAdjacentTriangle(trigA);
87: } catch (TriangulationException e) {
88: //should never reach here
89: e.printStackTrace();
90: }
91:
92: triangles.add(trigA);
93: triangles.add(trigB);
94:
95: return triangles;
96: }
97: }
|