Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2287 | - | 1 | |
2 | namespace GMap.NET |
||
3 | { |
||
4 | using System; |
||
5 | using System.Globalization; |
||
6 | |||
7 | /// <summary> |
||
8 | /// the rect of coordinates |
||
9 | /// </summary> |
||
10 | public struct RectLatLng |
||
11 | { |
||
12 | public static readonly RectLatLng Empty; |
||
13 | private double lng; |
||
14 | private double lat; |
||
15 | private double widthLng; |
||
16 | private double heightLat; |
||
17 | |||
18 | public RectLatLng(double lat, double lng, double widthLng, double heightLat) |
||
19 | { |
||
20 | this.lng = lng; |
||
21 | this.lat = lat; |
||
22 | this.widthLng = widthLng; |
||
23 | this.heightLat = heightLat; |
||
24 | NotEmpty = true; |
||
25 | } |
||
26 | |||
27 | public RectLatLng(PointLatLng location, SizeLatLng size) |
||
28 | { |
||
29 | this.lng = location.Lng; |
||
30 | this.lat = location.Lat; |
||
31 | this.widthLng = size.WidthLng; |
||
32 | this.heightLat = size.HeightLat; |
||
33 | NotEmpty = true; |
||
34 | } |
||
35 | |||
36 | public static RectLatLng FromLTRB(double leftLng, double topLat, double rightLng, double bottomLat) |
||
37 | { |
||
38 | return new RectLatLng(topLat, leftLng, rightLng - leftLng, topLat - bottomLat); |
||
39 | } |
||
40 | |||
41 | public PointLatLng LocationTopLeft |
||
42 | { |
||
43 | get |
||
44 | { |
||
45 | return new PointLatLng(this.Lat, this.Lng); |
||
46 | } |
||
47 | set |
||
48 | { |
||
49 | this.Lng = value.Lng; |
||
50 | this.Lat = value.Lat; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | public PointLatLng LocationRightBottom |
||
55 | { |
||
56 | get |
||
57 | { |
||
58 | PointLatLng ret = new PointLatLng(this.Lat, this.Lng); |
||
59 | ret.Offset(HeightLat, WidthLng); |
||
60 | return ret; |
||
61 | } |
||
62 | } |
||
63 | |||
64 | public PointLatLng LocationMiddle |
||
65 | { |
||
66 | get |
||
67 | { |
||
68 | PointLatLng ret = new PointLatLng(this.Lat, this.Lng); |
||
69 | ret.Offset(HeightLat / 2, WidthLng / 2); |
||
70 | return ret; |
||
71 | } |
||
72 | } |
||
73 | |||
74 | public SizeLatLng Size |
||
75 | { |
||
76 | get |
||
77 | { |
||
78 | return new SizeLatLng(this.HeightLat, this.WidthLng); |
||
79 | } |
||
80 | set |
||
81 | { |
||
82 | this.WidthLng = value.WidthLng; |
||
83 | this.HeightLat = value.HeightLat; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | public double Lng |
||
88 | { |
||
89 | get |
||
90 | { |
||
91 | return this.lng; |
||
92 | } |
||
93 | set |
||
94 | { |
||
95 | this.lng = value; |
||
96 | } |
||
97 | } |
||
98 | |||
99 | public double Lat |
||
100 | { |
||
101 | get |
||
102 | { |
||
103 | return this.lat; |
||
104 | } |
||
105 | set |
||
106 | { |
||
107 | this.lat = value; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | public double WidthLng |
||
112 | { |
||
113 | get |
||
114 | { |
||
115 | return this.widthLng; |
||
116 | } |
||
117 | set |
||
118 | { |
||
119 | this.widthLng = value; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | public double HeightLat |
||
124 | { |
||
125 | get |
||
126 | { |
||
127 | return this.heightLat; |
||
128 | } |
||
129 | set |
||
130 | { |
||
131 | this.heightLat = value; |
||
132 | } |
||
133 | } |
||
134 | |||
135 | public double Left |
||
136 | { |
||
137 | get |
||
138 | { |
||
139 | return this.Lng; |
||
140 | } |
||
141 | } |
||
142 | |||
143 | public double Top |
||
144 | { |
||
145 | get |
||
146 | { |
||
147 | return this.Lat; |
||
148 | } |
||
149 | } |
||
150 | |||
151 | public double Right |
||
152 | { |
||
153 | get |
||
154 | { |
||
155 | return (this.Lng + this.WidthLng); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | public double Bottom |
||
160 | { |
||
161 | get |
||
162 | { |
||
163 | return (this.Lat - this.HeightLat); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | bool NotEmpty; |
||
168 | |||
169 | /// <summary> |
||
170 | /// returns true if coordinates wasn't assigned |
||
171 | /// </summary> |
||
172 | public bool IsEmpty |
||
173 | { |
||
174 | get |
||
175 | { |
||
176 | return !NotEmpty; |
||
177 | } |
||
178 | } |
||
179 | |||
180 | public override bool Equals(object obj) |
||
181 | { |
||
182 | if(!(obj is RectLatLng)) |
||
183 | { |
||
184 | return false; |
||
185 | } |
||
186 | RectLatLng ef = (RectLatLng)obj; |
||
187 | return ((((ef.Lng == this.Lng) && (ef.Lat == this.Lat)) && (ef.WidthLng == this.WidthLng)) && (ef.HeightLat == this.HeightLat)); |
||
188 | } |
||
189 | |||
190 | public static bool operator ==(RectLatLng left, RectLatLng right) |
||
191 | { |
||
192 | return ((((left.Lng == right.Lng) && (left.Lat == right.Lat)) && (left.WidthLng == right.WidthLng)) && (left.HeightLat == right.HeightLat)); |
||
193 | } |
||
194 | |||
195 | public static bool operator !=(RectLatLng left, RectLatLng right) |
||
196 | { |
||
197 | return !(left == right); |
||
198 | } |
||
199 | |||
200 | public bool Contains(double lat, double lng) |
||
201 | { |
||
202 | return ((((this.Lng <= lng) && (lng < (this.Lng + this.WidthLng))) && (this.Lat >= lat)) && (lat > (this.Lat - this.HeightLat))); |
||
203 | } |
||
204 | |||
205 | public bool Contains(PointLatLng pt) |
||
206 | { |
||
207 | return this.Contains(pt.Lat, pt.Lng); |
||
208 | } |
||
209 | |||
210 | public bool Contains(RectLatLng rect) |
||
211 | { |
||
212 | return ((((this.Lng <= rect.Lng) && ((rect.Lng + rect.WidthLng) <= (this.Lng + this.WidthLng))) && (this.Lat >= rect.Lat)) && ((rect.Lat - rect.HeightLat) >= (this.Lat - this.HeightLat))); |
||
213 | } |
||
214 | |||
215 | public override int GetHashCode() |
||
216 | { |
||
217 | if(this.IsEmpty) |
||
218 | { |
||
219 | return 0; |
||
220 | } |
||
221 | return (((this.Lng.GetHashCode() ^ this.Lat.GetHashCode()) ^ this.WidthLng.GetHashCode()) ^ this.HeightLat.GetHashCode()); |
||
222 | } |
||
223 | |||
224 | // from here down need to test each function to be sure they work good |
||
225 | // | |
||
226 | // . |
||
227 | |||
228 | #region -- unsure -- |
||
229 | public void Inflate(double lat, double lng) |
||
230 | { |
||
231 | this.Lng -= lng; |
||
232 | this.Lat += lat; |
||
233 | this.WidthLng += 2d * lng; |
||
234 | this.HeightLat += 2d * lat; |
||
235 | } |
||
236 | |||
237 | public void Inflate(SizeLatLng size) |
||
238 | { |
||
239 | this.Inflate(size.HeightLat, size.WidthLng); |
||
240 | } |
||
241 | |||
242 | public static RectLatLng Inflate(RectLatLng rect, double lat, double lng) |
||
243 | { |
||
244 | RectLatLng ef = rect; |
||
245 | ef.Inflate(lat, lng); |
||
246 | return ef; |
||
247 | } |
||
248 | |||
249 | public void Intersect(RectLatLng rect) |
||
250 | { |
||
251 | RectLatLng ef = Intersect(rect, this); |
||
252 | this.Lng = ef.Lng; |
||
253 | this.Lat = ef.Lat; |
||
254 | this.WidthLng = ef.WidthLng; |
||
255 | this.HeightLat = ef.HeightLat; |
||
256 | } |
||
257 | |||
258 | // ok ??? |
||
259 | public static RectLatLng Intersect(RectLatLng a, RectLatLng b) |
||
260 | { |
||
261 | double lng = Math.Max(a.Lng, b.Lng); |
||
262 | double num2 = Math.Min((double)(a.Lng + a.WidthLng), (double)(b.Lng + b.WidthLng)); |
||
263 | |||
264 | double lat = Math.Max(a.Lat, b.Lat); |
||
265 | double num4 = Math.Min((double)(a.Lat + a.HeightLat), (double)(b.Lat + b.HeightLat)); |
||
266 | |||
267 | if((num2 >= lng) && (num4 >= lat)) |
||
268 | { |
||
269 | return new RectLatLng(lat, lng, num2 - lng, num4 - lat); |
||
270 | } |
||
271 | return Empty; |
||
272 | } |
||
273 | |||
274 | // ok ??? |
||
275 | // http://greatmaps.codeplex.com/workitem/15981 |
||
276 | public bool IntersectsWith(RectLatLng a) |
||
277 | { |
||
278 | return this.Left < a.Right && this.Top > a.Bottom && this.Right > a.Left && this.Bottom < a.Top; |
||
279 | } |
||
280 | |||
281 | // ok ??? |
||
282 | // http://greatmaps.codeplex.com/workitem/15981 |
||
283 | public static RectLatLng Union(RectLatLng a, RectLatLng b) |
||
284 | { |
||
285 | return RectLatLng.FromLTRB( |
||
286 | Math.Min(a.Left, b.Left), |
||
287 | Math.Max(a.Top, b.Top), |
||
288 | Math.Max(a.Right, b.Right), |
||
289 | Math.Min(a.Bottom, b.Bottom)); |
||
290 | } |
||
291 | #endregion |
||
292 | |||
293 | // . |
||
294 | // | |
||
295 | // unsure ends here |
||
296 | |||
297 | public void Offset(PointLatLng pos) |
||
298 | { |
||
299 | this.Offset(pos.Lat, pos.Lng); |
||
300 | } |
||
301 | |||
302 | public void Offset(double lat, double lng) |
||
303 | { |
||
304 | this.Lng += lng; |
||
305 | this.Lat -= lat; |
||
306 | } |
||
307 | |||
308 | public override string ToString() |
||
309 | { |
||
310 | return ("{Lat=" + this.Lat.ToString(CultureInfo.CurrentCulture) + ",Lng=" + this.Lng.ToString(CultureInfo.CurrentCulture) + ",WidthLng=" + this.WidthLng.ToString(CultureInfo.CurrentCulture) + ",HeightLat=" + this.HeightLat.ToString(CultureInfo.CurrentCulture) + "}"); |
||
311 | } |
||
312 | |||
313 | static RectLatLng() |
||
314 | { |
||
315 | Empty = new RectLatLng(); |
||
316 | } |
||
317 | } |
||
318 | } |