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(0,0,0);
16 }
17
18 public DefConLocation(double x, double y) {
19 super(correctX(x, y), y);
20
21 if (y > 100d || y < -100d) {
22 throw new IllegalArgumentException(
23 "DefConLocation Y coord must be inside <-100, 100>");
24 }
25 }
26
27 public DefConLocation(float f[]) {
28 this(f[0], f[1]);
29 }
30
31 public DefConLocation(double d[]) {
32 this(d[0], d[1]);
33 }
34
35 public DefConLocation(Location location) {
36 this(location.getX(), location.getY());
37 }
38
39 @Override
40 public double getDistance2D(Location location) {
41 return getDistance(location);
42 }
43
44 @Override
45 public double getDistance(Location location) {
46
47 double x = 0;
48 double y = 0;
49
50 double diff = this.x - location.x;
51
52
53 if (diff > 180d) {
54 x = (this.x - 360d) - location.x;
55 } else if (diff < -180d) {
56 x = (this.x + 360d) - location.x;
57 } else {
58 x = this.x - location.x;
59 }
60
61 y = this.y - location.y;
62
63
64
65 return new Location(x,y).getLength();
66 }
67
68 public static Location sub(DefConLocation l1, DefConLocation l2) {
69 double x = 0;
70 double y = 0;
71
72 double diff = l1.x - l2.x;
73
74
75 if (diff > 180d) {
76 x = (l1.x - 360d) - l2.x;
77 } else if (diff < -180d) {
78 x = (l1.x + 360d) - l2.x;
79 } else {
80 x = l1.x - l2.x;
81 }
82
83 y = (l1.y - l2.y);
84
85 return new Location(x,y);
86 }
87
88
89 protected static double correctX(double x, double y) {
90 if (y > 100d || y < -100d) {
91 throw new IllegalArgumentException(
92 "DefConLocation Y coord must be inside <-100, 100>. " + y);
93 }
94
95 return 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 double x = 0;
118 double y = 0;
119
120 double diff = x - l.x;
121
122
123 if (diff > 180d) {
124 x = (x + 360d) + l.x;
125 } else if (diff < -180d) {
126 x = (x - 360d) + l.x;
127 } else {
128 x = x + l.x;
129 }
130
131 y = y + l.y;
132
133 return new DefConLocation(x, y);
134 }
135
136
137
138
139
140
141
142
143 public Location sub(DefConLocation l) {
144 return sub(this, l);
145 }
146
147 @Override
148 public DefConLocation getNormalized() {
149 return new DefConLocation(super.getNormalized());
150 }
151
152
153
154
155
156
157 @Override
158 public DefConLocation invert() {
159 return new DefConLocation(-x, -y);
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 @Override
207 public double getDistanceSquare(Location l) {
208
209 l = this.sub(l);
210
211 double dx = l.x;
212 double dy = l.y;
213 double dz = l.z;
214
215
216 return dx * dx + dy * dy + dz * dz;
217 }
218
219
220 }