EOS 2  1.1.0
Einfache Objektbasierte Sprache
Intersection.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.game.geom;
2 
3 import de.lathanda.eos.base.math.Vector;
4 import de.lathanda.eos.base.math.Range;
5 
16 public class Intersection {
17 
25  public static boolean intersects(Shape a, Shape b) {
26  //yes this is ugly, but java doesn't support polymorph parameters
27  //and i don't know a better solution atm.
28  switch (a.getOutlineType()) {
29  case GROUP:
30  return groupOutline((ShapeGroup) a, b);
31  case DOT:
32  return b.contains(((Dot) a).p);
33  case RECTANGLE:
34  switch (b.getOutlineType()) {
35  case RECTANGLE:
36  return true;
37  case POLYGON:
38  return polygonPolygon((Rectangle) a, (Polygon) b);
39  case CIRCLE:
40  return polygonCircle((Rectangle) a, (Circle) b);
41  case DOT:
42  return a.contains(((Dot) b).p);
43  case GROUP:
44  return groupOutline((ShapeGroup) b, a);
45  case UNDEFINED:
46  break;
47  default:
48  break;
49  }
50  case POLYGON:
51  switch (b.getOutlineType()) {
52  case RECTANGLE:
53  return polygonPolygon((Rectangle) b, (Polygon) a);
54  case POLYGON:
55  return polygonPolygon((Polygon) a, (Polygon) b);
56  case CIRCLE:
57  return polygonCircle((Polygon) a, (Circle) b);
58  case DOT:
59  return a.contains(((Dot) b).p);
60  case GROUP:
61  return groupOutline((ShapeGroup) b, a);
62  case UNDEFINED:
63  throw new RuntimeException("unknown outline typ!");
64  }
65  case CIRCLE:
66  switch (b.getOutlineType()) {
67  case RECTANGLE:
68  return polygonCircle((Rectangle) b, (Circle) a);
69  case POLYGON:
70  return polygonCircle((Polygon) b, (Circle) a);
71  case CIRCLE:
72  return circleCircle((Circle) a, (Circle) b);
73  case DOT:
74  return a.contains(((Dot) b).p);
75  case GROUP:
76  return groupOutline((ShapeGroup) b, a);
77  case UNDEFINED:
78  throw new RuntimeException("unknown outline typ!");
79  }
80  case UNDEFINED:
81  throw new RuntimeException("unknown outline typ!");
82  }
83  throw new RuntimeException("unknown outline typ!");
84  }
85 
93  private static boolean groupOutline(ShapeGroup a, Shape b) {
94  for (Shape o : a.getOutlines()) {
95  if (o.intersects(b)) {
96  return true;
97  }
98  }
99  return false;
100  }
101 
109  private static boolean polygonPolygon(Polygon p1, Polygon p2) {
110  // we use separated axis theorem
111 
112  Vector v;
113 
114  Range projectionA, projectionB;
115  int a, b; // edge from index a to index b
116  Polygon p;
117  // for all edges
118 
119  for (int i = 0; i < p1.n + p2.n; i++) {
120  if (i < p1.n) {
121  a = i;
122  b = (a + 1) % p1.n;
123  p = p1;
124  } else {
125  a = i - p1.n;
126  b = (a + 1) % p2.n;
127  p = p2;
128  }
129  // get edge, rotated by 90°
130  v = new Vector(
131  p.coordinates_final_y[b] - p.coordinates_final_y[a],
132  p.coordinates_final_x[a] - p.coordinates_final_x[b]
133  );
134 
135  projectionA = projectPolygon(v, p1);
136  projectionB = projectPolygon(v, p2);
137 
138  // and check if they overlap
139  if (!projectionA.overlap(projectionB)) {
140  // they do not intersect, finished
141  return false;
142  }
143  }
144  return true;
145  }
146 
155  private static Range projectPolygon(Vector axis, Polygon polygon) {
156  Range result = new Range(0, 0);
157  double scalar = axis.dotProduct(polygon.coordinates_final_x[0],
158  polygon.coordinates_final_y[0]);
159  result.min = scalar;
160  result.max = scalar;
161  for (int i = 1; i < polygon.n; i++) {
162  result.extend(axis.dotProduct(
163  polygon.coordinates_final_x[i],
164  polygon.coordinates_final_y[i])
165  );
166  }
167  return result;
168  }
169 
177  private static boolean polygonCircle(Polygon p, Circle c) {
178  return p.getDistance(c.p) < c.radius;
179  }
180 
188  private static boolean circleCircle(Circle c1, Circle c2) {
189  double dx = c2.p.getX() - c1.p.getX();
190  double dy = c2.p.getY() - c1.p.getY();
191  return (c1.radius + c2.radius) * (c1.radius + c2.radius) > dx * dx + dy * dy;
192  }
193 }
static boolean intersects(Shape a, Shape b)
boolean contains(double x, double y)
Definition: Polygon.java:174
ArrayList< Shape > getOutlines()
Definition: ShapeGroup.java:71
abstract Types getOutlineType()
boolean contains(Point p)
Definition: Shape.java:213
Impressum