EOS 2  1.1.0
Einfache Objektbasierte Sprache
Polygon.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.geo;
2 
3 import java.util.ArrayList;
4 import java.util.LinkedList;
5 
6 import de.lathanda.eos.base.FillStyle;
7 import de.lathanda.eos.base.Picture;
8 import de.lathanda.eos.base.layout.BalancePoint;
9 import de.lathanda.eos.base.layout.BoundingBox;
10 import de.lathanda.eos.base.layout.Transform;
11 import de.lathanda.eos.base.math.Point;
12 import de.lathanda.eos.base.math.Vector;
13 import de.lathanda.eos.game.geom.Tesselation;
14 import de.lathanda.eos.game.geom.tesselation.TesselationFailedException;
15 
21 public class Polygon extends FilledFigure {
22  private ArrayList<Point> points;
23  private LinkedList<? extends Point> bound;
24  private double weight = 0;
25  public Polygon() {
26  points = new ArrayList<>();
27  line.setDrawWidth(0.5f);
28  updatePolygon();
29  }
30 
31  @Override
32  protected void drawObject(Picture p) {
33  synchronized (this) {
35  if (bound != null) {
36  p.drawPolygon(bound);
37  } else {
38  p.drawPolygon(points);
39  }
40  } else {
41  for (int i = 1; i < points.size(); i++) {
42  p.drawLine(points.get(i - 1), points.get(i));
43  }
44  }
45  }
46  }
47  @Override
48  protected void scaleInternal(double factor) {
49  synchronized (this) {
50  for (int i = points.size(); i-- > 0;) {
51  points.get(i).scale(factor);
52  }
53  updatePolygon();
54  }
55  }
56 
57  @Override
59  return new BalancePoint(weight, getX(), getY());
60  }
61 
62  public boolean isValid() {
63  synchronized (this) {
64  return points.size() > 1;
65  }
66  }
67 
68  public void addPoint(Point p) {
69  synchronized (this) {
70  Point copy = new Point(p);
71  copy.move(-getX(), -getY());
72  points.add(copy);
73  updatePolygon();
74  }
76  }
77  private void updatePolygon() {
78  if (points.isEmpty()) return;
79  double x = 0;
80  double y = 0;
81  weight = 0;
82 
84  try {
86  tess.addVertices(points);
87  bound = tess.getOuterBorder();
88  Point a = points.get(points.size() - 1);
89  for (int i = 0; i < points.size(); i++) {
90  Point b = points.get(i);
91  double w = a.getX() * b.getY() - b.getX() * a.getY();
92  weight += w;
93  x += (a.getX() + b.getX()) * w;
94  y += (a.getY() + b.getY()) * w;
95  a = b;
96  }
97  x /= 6;
98  y /= 6;
99  weight /= 2;
100  } catch (TesselationFailedException tfe) {
101  bound = null;
102  BoundingBox bb = new BoundingBox();
103  for (Point p:points) {
104  bb.add(p);
105  }
106  weight = bb.getArea();
107  x = bb.getCenter().getX();
108  y = bb.getCenter().getY();
109  }
110  } else {
111  bound = null;
112  Point a = points.get(0);
113  for (int i = 1; i < points.size(); i++) {
114  Point b = points.get(i);
115  double line_weight = new Vector(a, b).getLength() * line.getDrawWidth();
116  x += (a.getX() + b.getX()) * line_weight / 2;
117  y += (b.getY() + b.getY()) * line_weight / 2;
118  weight += line_weight;
119  a = b;
120  }
121  }
122  if (weight > 0) {
123  x = x / weight;
124  y = y / weight;
125  } else {
126  x = 0;
127  y = 0;
128  }
129 
130  moveInternal(x, y);
131  for(Point p : points) {
132  p.move(-x, -y);
133  }
134  if (bound != null) {
135  for(Point p : bound) {
136  p.move(-x, -y);
137  }
138  }
139  }
140  @Override
142  Transform t = base.transform(own);
143  BoundingBox bound = new BoundingBox();
144  synchronized (this) {
145  for (Point p:points) {
146  bound.add(t.transform(p));
147  }
148  }
149  return bound;
150  }
151  @Override
152  public Figure copy() {
153  synchronized (this) {
154  Polygon poly = (Polygon)super.copy();
155  poly.points = new ArrayList<>();
156  for(Point p : points) {
157  poly.points.add(new Point(p));
158  }
159 
160  poly.updatePolygon();
161  return poly;
162  }
163  }
164  @Override
165  public String toString() {
166  return "Polygon{" + points + '}';
167  }
168 }
abstract void drawLine(double x1, double y1, double x2, double y2)
abstract void drawPolygon(double[] x, double[] y)
Geometrische Transformation.
Definition: Transform.java:13
Transform transform(Transform child)
Definition: Transform.java:83
void moveInternal(double x, double y)
Definition: Figure.java:118
void move(double dx, double dy)
Definition: Figure.java:128
BoundingBox calculateBoundingBox(Transform base, Transform own)
Definition: Polygon.java:141
BalancePoint getBalancePoint()
Definition: Polygon.java:58
void scaleInternal(double factor)
Definition: Polygon.java:48
void addPoint(Point p)
Definition: Polygon.java:68
void drawObject(Picture p)
Definition: Polygon.java:32
LinkedList<? extends Point > getOuterBorder()
static Tesselation getDefaultTesselation()
default void addVertices(Collection<? extends Point > points)
Impressum