1 package cz.cuni.amis.pogamut.defcon.base3d.worldview.object;
2
3 import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
4
5
6
7
8
9
10
11
12 public class DefConLocation extends Location {
13
14 public DefConLocation() {
15 super();
16 }
17
18 public DefConLocation(double x, double y) {
19 super(x, y);
20
21 if (y > 100d || y < -100d) {
22 throw new IllegalArgumentException(
23 "DefConLocation Y coord must be inside <-100, 100>");
24 }
25
26 correct();
27 }
28
29 public DefConLocation(float f[]) {
30 this(f[0], f[1]);
31 }
32
33 public DefConLocation(double d[]) {
34 this(d[0], d[1]);
35 }
36
37 public DefConLocation(Location location) {
38 this(location.getX(), location.getY());
39 }
40
41 @Override
42 public double getDistance2D(Location location) {
43 return getDistance(location);
44 }
45
46 @Override
47 public double getDistance(Location location) {
48
49 Location tmp = new Location();
50
51 double diff = this.x - location.x;
52
53
54 if (diff > 180d) {
55 tmp.x = (this.x - 360d) - location.x;
56 } else if (diff < -180d) {
57 tmp.x = (this.x + 360d) - location.x;
58 } else {
59 tmp.x = this.x - location.x;
60 }
61
62 tmp.y = this.y - location.y;
63
64
65
66 return tmp.getLength();
67 }
68
69 public static Location sub(DefConLocation l1, DefConLocation l2) {
70 Location out = new Location();
71
72 double diff = l1.x - l2.x;
73
74
75 if (diff > 180d) {
76 out.x = (l1.x - 360d) - l2.x;
77 } else if (diff < -180d) {
78 out.x = (l1.x + 360d) - l2.x;
79 } else {
80 out.x = l1.x - l2.x;
81 }
82
83 out.y = (l1.y - l2.y);
84
85 return out;
86 }
87
88
89 protected void correct() {
90 if (y > 100d || y < -100d) {
91 throw new IllegalArgumentException(
92 "DefConLocation Y coord must be inside <-100, 100>. " + y);
93 }
94
95 x = mathModulus(x + 180d, 360d) - 180d;
96 }
97
98 protected final static double mathModulus(double a, double b) {
99 return (a % b + b) % b;
100 }
101
102 @Override
103 public DefConLocation scale(double d) {
104 return new DefConLocation(super.scale(d));
105 }
106
107
108
109
110
111
112
113
114
115 @Override
116 public DefConLocation add(Location l) {
117
118 DefConLocation out = new DefConLocation();
119
120 double diff = x - l.x;
121
122
123 if (diff > 180d) {
124 out.x = (x + 360d) + l.x;
125 } else if (diff < -180d) {
126 out.x = (x - 360d) + l.x;
127 } else {
128 out.x = x + l.x;
129 }
130
131 out.y = y + l.y;
132
133 out.correct();
134
135 return out;
136 }
137
138
139
140
141
142
143
144
145 public Location sub(DefConLocation l) {
146 return sub(this, l);
147 }
148
149 @Override
150 public DefConLocation getNormalized() {
151 return new DefConLocation(super.getNormalized());
152 }
153
154
155
156
157
158
159 @Override
160 public DefConLocation invert() {
161 return new DefConLocation(-x, -y);
162 }
163
164
165
166
167
168
169 @Override
170 public DefConLocation setTo(Location l) {
171 this.x = l.x;
172 this.y = l.y;
173 correct();
174
175 return this;
176 }
177
178
179
180
181
182
183 @Override
184 public DefConLocation setTo(double x, double y, double z) {
185 return setTo(x, y);
186 }
187
188
189
190
191
192
193 public DefConLocation setTo(double x, double y) {
194 this.x = x;
195 this.y = y;
196 correct();
197
198 return this;
199 }
200
201
202
203
204
205
206
207
208 @Override
209 public double getDistanceSquare(Location l) {
210
211 l = this.sub(l);
212
213 double dx = l.x;
214 double dy = l.y;
215 double dz = l.z;
216
217
218 return dx * dx + dy * dy + dz * dz;
219 }
220
221
222 }