EOS 2  1.1.0
Einfache Objektbasierte Sprache
Distance.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.game.geom;
2 
12 public class Distance {
13 
22  public static double distance(Shape a, Shape b) {
23  //yes this is ugly, but java doesn't support polymorph parameters
24  //and i don't know a better solution atm.
25  switch (a.getOutlineType()) {
26  case DOT:
27  return b.getDistance(a.p);
28  case GROUP:
29  return groupOutline((ShapeGroup) a, b);
30  case RECTANGLE:
31  switch (b.getOutlineType()) {
32  case RECTANGLE:
33  return polygonPolygon((Rectangle) b, (Rectangle) a);
34  case POLYGON:
35  return polygonPolygon((Rectangle) a, (Polygon) b);
36  case CIRCLE:
37  return a.getDistance(b.p) - ((Circle) b).radius;
38  case DOT:
39  return a.getDistance(b.p);
40  case GROUP:
41  return groupOutline((ShapeGroup) b, a);
42  case UNDEFINED:
43  throw new RuntimeException("unknown outline typ!");
44  }
45  case POLYGON:
46  switch (b.getOutlineType()) {
47  case RECTANGLE:
48  return polygonPolygon((Rectangle) b, (Polygon) a);
49  case POLYGON:
50  return polygonPolygon((Polygon) a, (Polygon) b);
51  case CIRCLE:
52  return a.getDistance(b.p) - ((Circle) b).radius;
53  case DOT:
54  return a.getDistance(b.p);
55  case GROUP:
56  return groupOutline((ShapeGroup) b, a);
57  case UNDEFINED:
58  throw new RuntimeException("unknown outline typ!");
59  }
60  case CIRCLE:
61  return a.getDistance(b.p) - ((Circle) b).radius;
62  case UNDEFINED:
63  throw new RuntimeException("unknown outline typ!");
64  }
65  throw new RuntimeException("unknown outline typ!");
66  }
67 
76  private static double groupOutline(ShapeGroup a, Shape b) {
77  double distance = Double.POSITIVE_INFINITY;
78  for (Shape o : a.getOutlines()) {
79  distance = Math.min(o.distance(b), distance);
80  }
81  return distance;
82  }
83 
91  private static double polygonPolygon(Polygon p1, Polygon p2) {
92  double distance;
93  if (p1.intersects(p2)) {
94  distance = Double.NEGATIVE_INFINITY;
95  for (int i = p1.coordinates_final_x.length; i-- > 0;) {
96  distance = Math.max(distance, p2.getDistance(
97  p1.coordinates_final_x[i],
98  p1.coordinates_final_y[i]));
99  }
100  for (int i = p2.coordinates_final_x.length; i-- > 0;) {
101  distance = Math.max(distance, p1.getDistance(
102  p2.coordinates_final_x[i],
103  p2.coordinates_final_y[i]));
104  }
105  distance = Math.min(distance, 0);
106  } else {
107  distance = Double.POSITIVE_INFINITY;
108  for (int i = p1.coordinates_final_x.length; i-- > 0;) {
109  distance = Math.min(distance, p2.getDistance(
110  p1.coordinates_final_x[i],
111  p1.coordinates_final_y[i]));
112  }
113  for (int i = p2.coordinates_final_x.length; i-- > 0;) {
114  distance = Math.min(distance, p1.getDistance(
115  p2.coordinates_final_x[i],
116  p2.coordinates_final_y[i]));
117  }
118  }
119  return distance;
120  }
121 }
Entfernungsberechnung.
Definition: Distance.java:12
static double distance(Shape a, Shape b)
Definition: Distance.java:22
double getDistance(double x, double y)
Definition: Polygon.java:200
double getDistance(double x, double y)
Definition: ShapeGroup.java:86
ArrayList< Shape > getOutlines()
Definition: ShapeGroup.java:71
abstract Types getOutlineType()
final double getDistance(Point p)
Definition: Shape.java:254
Impressum