1
2
3
4
5 package cz.cuni.amis.pogamut.usar2004.samples.AirScanner;
6
7 import math.geom2d.Point2D;
8
9
10
11
12
13 public class Obstacle1
14 {
15 public static final double obstacleSize = 1d;
16 public static final double joinDistance = 1.5d;
17 public static final double maxExtendRatio = 4;
18 public static final double maxEdgeSize = 5;
19 private Point2D start;
20 private Point2D stop;
21
22 public Obstacle1(Point2D corner, double width, double height)
23 {
24 this.start = corner;
25 this.stop = new Point2D(corner.x + width, corner.y + height);
26 }
27
28 public Obstacle1(Point2D start, Point2D stop)
29 {
30 this.start = start;
31 this.stop = stop;
32 }
33
34
35
36
37
38
39
40
41 public Obstacle1(Point2D center)
42 {
43 this(center.x, center.y);
44 }
45
46 public Obstacle1(double centerX, double centerY)
47 {
48 this.start = new Point2D(centerX - obstacleSize / 2, centerY - obstacleSize / 2);
49 this.stop = new Point2D(centerX + obstacleSize / 2, centerY + obstacleSize / 2);
50 }
51
52
53
54
55
56
57
58
59 private boolean extend(double x, double y)
60 {
61 boolean success = false;
62
63 if(x + obstacleSize / 2 > stop.x)
64 {
65 stop.x = x + obstacleSize / 2;
66 success = true;
67 }
68 if(y + obstacleSize / 2 > stop.y)
69 {
70 stop.y = y + obstacleSize / 2;
71 success = true;
72 }
73 if(x - obstacleSize / 2 < start.x)
74 {
75 start.x = x - obstacleSize / 2;
76 success = true;
77 }
78 if(y - obstacleSize / 2 < start.y)
79 {
80 start.y = y - obstacleSize / 2;
81 success = true;
82 }
83
84 return success;
85 }
86
87
88 public double getWidth()
89 {
90 return stop.x - start.x;
91 }
92
93 public double getHeight()
94 {
95 return stop.y - start.y;
96 }
97
98 public Point2D getCorner()
99 {
100 return start;
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115 public Obstacle1 tryExtend(double x, double y)
116 {
117 if(x > this.start.x - joinDistance
118 && x < this.stop.x + joinDistance
119 && y > this.start.y - joinDistance
120 && y < this.stop.y + joinDistance)
121 {
122 if(extend(x, y))
123 extendCount++;
124 return this;
125 }
126 return null;
127 }
128 private int extendCount = 0;
129
130
131
132
133
134
135
136
137 public boolean canExtend()
138 {
139 return extendCount<3;
140
141
142
143
144
145
146
147
148
149
150
151
152 }
153
154 public boolean isInside(double x, double y)
155 {
156 if(x > this.start.x
157 && x < this.stop.x
158 && y > this.start.y
159 && y < this.stop.y)
160 {
161 return true;
162 }
163 return false;
164 }
165
166 public boolean isWithin(double x, double y)
167 {
168 if(x > this.start.x - obstacleSize / 2
169 && x < this.stop.x + obstacleSize / 2
170 && y > this.start.y - obstacleSize / 2
171 && y < this.stop.y + obstacleSize / 2)
172 {
173 return true;
174 }
175 return false;
176 }
177
178 public static Obstacle1 MergeObstacles(Obstacle1 blockA, Obstacle1 blockB)
179 {
180
181 double xA = blockA.start.x;
182 double xB = blockB.start.x;
183 double yA = blockA.start.y;
184 double yB = blockB.start.y;
185
186
187 double xC = blockA.start.x;
188 double yC = blockA.start.y;
189 double xD = blockB.stop.x;
190 double yD = blockB.stop.y;
191
192 Point2D newStart = new Point2D(Math.min(xA, xB), Math.min(yA, yB));
193 Point2D newStop = new Point2D(Math.min(xC, xD), Math.min(yC, yD));
194 return new Obstacle1(newStart, newStop);
195 }
196 }