comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/geom/Polygon2D.java @ 1797:5eec623db50a

Polygon2D: moved 2D vector operation to separate class. flys-artifacts/trunk@3120 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 31 Oct 2011 10:03:32 +0000
parents fe7f9264a2ed
children 552888e9c64a
comparison
equal deleted inserted replaced
1796:ae6ace900c07 1797:5eec623db50a
14 import java.util.Collections; 14 import java.util.Collections;
15 15
16 public class Polygon2D 16 public class Polygon2D
17 implements Serializable 17 implements Serializable
18 { 18 {
19 public static final double EPSILON = 1e-4;
20
21 private static final double X(Point2D p) { 19 private static final double X(Point2D p) {
22 return p.getX(); 20 return p.getX();
23 } 21 }
24 22
25 private static final double Y(Point2D p) { 23 private static final double Y(Point2D p) {
50 48
51 public Polygon2D() { 49 public Polygon2D() {
52 points = new ArrayList<Point2D>(); 50 points = new ArrayList<Point2D>();
53 } 51 }
54 52
55 public static boolean epsilonEquals(Point2D a, Point2D b) {
56 return Math.abs(X(a)-X(b)) < EPSILON
57 && Math.abs(Y(a)-Y(b)) < EPSILON;
58 }
59
60 public void add(double x, double y) { 53 public void add(double x, double y) {
61 points.add(new Point2D.Double(x, y)); 54 points.add(new Point2D.Double(x, y));
62 } 55 }
63 56
64 public boolean addCheck(Point2D p) { 57 public boolean addCheck(Point2D p) {
65 switch (points.size()) { 58 switch (points.size()) {
66 case 0: 59 case 0:
67 points.add(p); 60 points.add(p);
68 return true; 61 return true;
69 case 1: 62 case 1:
70 if (epsilonEquals(points.get(0), p)) { 63 if (VectorUtils.epsilonEquals(points.get(0), p)) {
71 return false; 64 return false;
72 } 65 }
73 points.add(p); 66 points.add(p);
74 return true; 67 return true;
75 default: 68 default:
76 int L = points.size()-1; 69 int L = points.size()-1;
77 Point2D last = points.get(L); 70 Point2D last = points.get(L);
78 if (epsilonEquals(last, p)) { 71 if (VectorUtils.epsilonEquals(last, p)) {
79 return false; 72 return false;
80 } 73 }
81 Point2D before = points.get(L-1); 74 Point2D before = points.get(L-1);
82 if (collinear(before, last, p)) { 75 if (VectorUtils.collinear(before, last, p)) {
83 points.set(L, p); 76 points.set(L, p);
84 } 77 }
85 else { 78 else {
86 points.add(p); 79 points.add(p);
87 } 80 }
91 84
92 public void addReversed(List<Point2D> other) { 85 public void addReversed(List<Point2D> other) {
93 for (int i = other.size()-1; i >= 0; --i) { 86 for (int i = other.size()-1; i >= 0; --i) {
94 addCheck(other.get(i)); 87 addCheck(other.get(i));
95 } 88 }
96 }
97
98 private static final double L1(Point2D a, Point2D b) {
99 return Math.abs(X(a)-X(b)) + Math.abs(Y(a)-Y(b));
100 }
101
102 public static boolean collinear(Point2D a, Point2D b, Point2D c) {
103 double dab = L1(a, b);
104 double dac = L1(a, c);
105 double dbc = L1(b, c);
106
107 Point2D p1, p2, p3;
108
109 if (dab > dac) {
110 if (dab > dbc) {
111 p1 = a;
112 p2 = b;
113 p3 = c;
114 }
115 else { // dbc >= dab
116 p1 = b;
117 p2 = c;
118 p3 = a;
119 }
120 }
121 else { // dac >= dab
122 if (dac > dbc) {
123 p1 = a;
124 p2 = c;
125 p3 = b;
126 }
127 else { // dbc >= dac
128 p1 = b;
129 p2 = c;
130 p3 = a;
131 }
132 }
133
134 // TODO: Continue here.
135
136
137 return true;
138 }
139
140 public static Point2D sub(Point2D a, Point2D b) {
141 return new Point2D.Double(X(a)-X(b), Y(a)-Y(b));
142 }
143
144 public static Point2D scale(Point2D a, double s) {
145 return new Point2D.Double(s*X(a), s*Y(a));
146 } 89 }
147 90
148 public double area() { 91 public double area() {
149 double area = 0d; 92 double area = 0d;
150 93

http://dive4elements.wald.intevation.org