View Javadoc

1   package cz.cuni.amis.pogamut.defcon.base3d.worldview.object;
2   
3   import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
4   
5   /**
6    * Checks and corrects coordinates for the ingame locations and provides useful
7    * methods.
8    * 
9    * @author Radek 'Black_Hand' Pibil
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  		// diff > 180 => l1.x > l2.x
53  		if (diff > 180d) {
54  			x = (this.x - 360d) - location.x;
55  		} else if (diff < -180d) { // diff < -180 => l1.x < l2.x
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  		// PogamutJBotSupport.writeToConsole("distancedfee: " + tmp);
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  		// diff > 180 => l1.x > l2.x
75  		if (diff > 180d) {
76  			x = (l1.x - 360d) - l2.x;
77  		} else if (diff < -180d) { // diff < -180 => l1.x < l2.x
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 	 * Retrieves sum of this location and given location.
110 	 * 
111 	 * @param l
112 	 *            Location to be added to this location.
113 	 * @return Sum of the two locations.
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 		// diff > 180 => l.x > x
123 		if (diff > 180d) {
124 			x = (x + 360d) + l.x;
125 		} else if (diff < -180d) { // diff < -180 => x > l.x
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 	 * Retrieves sum of this location and given location.
138 	 * 
139 	 * @param l
140 	 *            Location to be added to this location.
141 	 * @return Sum of the two locations.
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 	 * Calculates inverse Location
154 	 * 
155 	 * @return new inverted Location
156 	 */
157 	@Override
158 	public DefConLocation invert() {
159 		return new DefConLocation(-x, -y);
160 	}
161 
162 	// /**
163 	// * Set content of this location from passed location.
164 	// *
165 	// * @return this with newly set value
166 	// */
167 	// @Override
168 	// public DefConLocation setTo(Location l) {
169 	// this.x = l.x;
170 	// this.y = l.y;
171 	// correct();
172 	//
173 	// return this;
174 	// }
175 	//
176 	// /**
177 	// * Set content of this location from passed data.
178 	// *
179 	// * @return this with newly set value
180 	// */
181 	// @Override
182 	// public DefConLocation setTo(double x, double y, double z) {
183 	// return setTo(x, y);
184 	// }
185 
186 	// /**
187 	// * Set content of this location from passed data.
188 	// *
189 	// * @return this with newly set value
190 	// */
191 	// public DefConLocation setTo(double x, double y) {
192 	// this.x = x;
193 	// this.y = y;
194 	// correct();
195 	//
196 	// return this;
197 	// }
198 
199 	/**
200 	 * Calculates the square of the distance between this and given location.
201 	 * 
202 	 * @param l
203 	 *            Location to be calculated the distance to.
204 	 * @return Square of the euclidean distance between the two locations.
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 }