Loading...
Searching...
No Matches
GeometryUtilsExample.java
Go to the documentation of this file.
1package com.navigine.examples;
2
3import java.util.Arrays;
4import java.util.List;
5
11
15
29
34 System.out.println("--- Point Operations ---");
35
36 // [java_Point_constructor]
37 // Create points with x, y coordinates
38 Point point1 = new Point(10.0, 20.0);
39 Point point2 = new Point(30.0, 40.0);
40 Point point3 = new Point(50.0, 60.0);
41 System.out.println("Created points: P1(" + point1.getX() + ", " + point1.getY() +
42 "), P2(" + point2.getX() + ", " + point2.getY() +
43 "), P3(" + point3.getX() + ", " + point3.getY() + ")");
44 // [java_Point_constructor]
45
46 // [java_Point_getX]
47 // Get X coordinate
48 double x1 = point1.getX();
49 System.out.println("Point1 X coordinate: " + x1);
50 // [java_Point_getX]
51
52 // [java_Point_getY]
53 // Get Y coordinate
54 double y1 = point1.getY();
55 System.out.println("Point1 Y coordinate: " + y1);
56 // [java_Point_getY]
57
58 // [java_GeometryUtils_distanceBetweenPoints]
59 // Calculate distance between points
60 double distance = GeometryUtils.distanceBetweenPoints(point1, point2);
61 System.out.printf("Distance between P1 and P2: %.2f meters%n", distance);
62 // [java_GeometryUtils_distanceBetweenPoints]
63
64 // Create more points for testing
65 List<Point> testPoints = Arrays.asList(
66 new Point(0.0, 0.0),
67 new Point(10.0, 0.0),
68 new Point(10.0, 10.0),
69 new Point(0.0, 10.0),
70 new Point(5.0, 5.0)
71 );
72
73 System.out.println("Test points created for further calculations");
74 }
75
80 System.out.println("--- GlobalPoint Operations ---");
81
82 // [java_GlobalPoint_constructor]
83 // Create global points with latitude, longitude
84 GlobalPoint globalPoint1 = new GlobalPoint(55.7558, 37.6176); // Moscow
85 GlobalPoint globalPoint2 = new GlobalPoint(59.9311, 30.3609); // St. Petersburg
86 GlobalPoint globalPoint3 = new GlobalPoint(55.7522, 37.6156); // Moscow center
87 System.out.println("Created global points: GP1(" + globalPoint1.getLatitude() + ", " + globalPoint1.getLongitude() +
88 "), GP2(" + globalPoint2.getLatitude() + ", " + globalPoint2.getLongitude() + ")");
89 // [java_GlobalPoint_constructor]
90
91 // [java_GlobalPoint_getLatitude]
92 // Get latitude
93 double lat1 = globalPoint1.getLatitude();
94 System.out.println("GlobalPoint1 latitude: " + lat1);
95 // [java_GlobalPoint_getLatitude]
96
97 // [java_GlobalPoint_getLongitude]
98 // Get longitude
99 double lon1 = globalPoint1.getLongitude();
100 System.out.println("GlobalPoint1 longitude: " + lon1);
101 // [java_GlobalPoint_getLongitude]
102
103 // [java_GeometryUtils_distanceBetweenGlobalPoints]
104 // Calculate distance between global points
105 double globalDistance = GeometryUtils.distanceBetweenGlobalPoints(globalPoint1, globalPoint2);
106 System.out.printf("Distance between Moscow and St. Petersburg: %.2f meters%n", globalDistance);
107 // [java_GeometryUtils_distanceBetweenGlobalPoints]
108
109 // Calculate distance between nearby points
110 double nearbyDistance = GeometryUtils.distanceBetweenGlobalPoints(globalPoint1, globalPoint3);
111 System.out.printf("Distance between Moscow points: %.2f meters%n", nearbyDistance);
112 }
113
118 System.out.println("--- Segment Operations ---");
119
120 // Create points for segments
121 Point start1 = new Point(0.0, 0.0);
122 Point end1 = new Point(10.0, 10.0);
123 Point start2 = new Point(0.0, 10.0);
124 Point end2 = new Point(10.0, 0.0);
125 Point testPoint = new Point(5.0, 5.0);
126
127 // [java_Segment_constructor]
128 // Create segments
129 Segment segment1 = new Segment(start1, end1);
130 Segment segment2 = new Segment(start2, end2);
131 System.out.println("Created segments: S1((" + segment1.getStart().getX() + ", " + segment1.getStart().getY() +
132 ") -> (" + segment1.getEnd().getX() + ", " + segment1.getEnd().getY() +
133 ")), S2((" + segment2.getStart().getX() + ", " + segment2.getStart().getY() +
134 ") -> (" + segment2.getEnd().getX() + ", " + segment2.getEnd().getY() + "))");
135 // [java_Segment_constructor]
136
137 // [java_Segment_getStart]
138 // Get start point
139 Point segment1Start = segment1.getStart();
140 System.out.println("Segment1 start point: (" + segment1Start.getX() + ", " + segment1Start.getY() + ")");
141 // [java_Segment_getStart]
142
143 // [java_Segment_getEnd]
144 // Get end point
145 Point segment1End = segment1.getEnd();
146 System.out.println("Segment1 end point: (" + segment1End.getX() + ", " + segment1End.getY() + ")");
147 // [java_Segment_getEnd]
148
149 // [java_GeometryUtils_segmentLength]
150 // Calculate segment length
151 double length1 = GeometryUtils.segmentLength(segment1);
152 double length2 = GeometryUtils.segmentLength(segment2);
153 System.out.printf("Segment1 length: %.2f meters%n", length1);
154 System.out.printf("Segment2 length: %.2f meters%n", length2);
155 // [java_GeometryUtils_segmentLength]
156
157 // [java_GeometryUtils_segmentPointDistance]
158 // Calculate distance from segment to point
159 double distanceToPoint = GeometryUtils.segmentPointDistance(segment1, testPoint);
160 System.out.printf("Distance from segment1 to test point: %.2f meters%n", distanceToPoint);
161 // [java_GeometryUtils_segmentPointDistance]
162
163 // [java_GeometryUtils_segmentIntersectsSegment]
164 // Check if segments intersect
165 boolean intersects = GeometryUtils.segmentIntersectsSegment(segment1, segment2);
166 System.out.println("Segments intersect: " + intersects);
167 // [java_GeometryUtils_segmentIntersectsSegment]
168
169 // [java_GeometryUtils_segmentIntersectionSegment]
170 // Calculate intersection point
171 Point intersection = GeometryUtils.segmentIntersectionSegment(segment1, segment2);
172 System.out.printf("Intersection point: (%.2f, %.2f)%n", intersection.getX(), intersection.getY());
173 // [java_GeometryUtils_segmentIntersectionSegment]
174
175 // [java_GeometryUtils_divisionRatioBySegment]
176 // Calculate division ratio
177 double divisionRatio = GeometryUtils.divisionRatioBySegment(segment1, segment2);
178 System.out.printf("Division ratio: %.2f%n", divisionRatio);
179 // [java_GeometryUtils_divisionRatioBySegment]
180
181 // [java_GeometryUtils_getRatioPoint]
182 // Get point at specific ratio
183 double ratio = 0.5;
184 Point ratioPoint = GeometryUtils.getRatioPoint(segment1, ratio);
185 System.out.printf("Point at ratio %.1f: (%.2f, %.2f)%n", ratio, ratioPoint.getX(), ratioPoint.getY());
186 // [java_GeometryUtils_getRatioPoint]
187
188 // [java_GeometryUtils_getProjectionRatio]
189 // Calculate projection ratio
190 double projectionRatio = GeometryUtils.getProjectionRatio(segment1, testPoint);
191 System.out.printf("Projection ratio: %.2f%n", projectionRatio);
192 // [java_GeometryUtils_getProjectionRatio]
193 }
194
199 System.out.println("--- Polygon Operations ---");
200
201 // Create points for polygon
202 List<Point> polygonPoints = Arrays.asList(
203 new Point(0.0, 0.0),
204 new Point(10.0, 0.0),
205 new Point(10.0, 10.0),
206 new Point(0.0, 10.0)
207 );
208
209 // [java_Polygon_constructor]
210 // Create polygon
211 Polygon polygon = new Polygon(polygonPoints);
212 System.out.println("Created polygon with " + polygon.getPoints().size() + " points");
213 // [java_Polygon_constructor]
214
215 // [java_Polygon_getPoints]
216 // Get polygon points
217 List<Point> points = polygon.getPoints();
218 System.out.println("Polygon points: " + points.stream()
219 .map(p -> "(" + p.getX() + ", " + p.getY() + ")")
220 .reduce("", (a, b) -> a + (a.isEmpty() ? "" : ", ") + b));
221 // [java_Polygon_getPoints]
222
223 // [java_GeometryUtils_polygonArea]
224 // Calculate polygon area
225 double area = GeometryUtils.polygonArea(polygon);
226 System.out.printf("Polygon area: %.2f square meters%n", area);
227 // [java_GeometryUtils_polygonArea]
228
229 // [java_GeometryUtils_polygonCenter]
230 // Calculate polygon center
231 Point center = GeometryUtils.polygonCenter(polygon);
232 System.out.printf("Polygon center: (%.2f, %.2f)%n", center.getX(), center.getY());
233 // [java_GeometryUtils_polygonCenter]
234
235 // [java_GeometryUtils_polygonContainsPoint]
236 // Check if polygon contains point
237 Point insidePoint = new Point(5.0, 5.0);
238 Point outsidePoint = new Point(15.0, 15.0);
239
240 boolean containsInside = GeometryUtils.polygonContainsPoint(polygon, insidePoint);
241 boolean containsOutside = GeometryUtils.polygonContainsPoint(polygon, outsidePoint);
242
243 System.out.println("Polygon contains inside point: " + containsInside);
244 System.out.println("Polygon contains outside point: " + containsOutside);
245 // [java_GeometryUtils_polygonContainsPoint]
246
247 // Create complex polygon
248 List<Point> complexPolygonPoints = Arrays.asList(
249 new Point(0.0, 0.0),
250 new Point(20.0, 0.0),
251 new Point(20.0, 20.0),
252 new Point(10.0, 10.0),
253 new Point(0.0, 20.0)
254 );
255 Polygon complexPolygon = new Polygon(complexPolygonPoints);
256
257 double complexArea = GeometryUtils.polygonArea(complexPolygon);
258 Point complexCenter = GeometryUtils.polygonCenter(complexPolygon);
259
260 System.out.printf("Complex polygon area: %.2f square meters%n", complexArea);
261 System.out.printf("Complex polygon center: (%.2f, %.2f)%n", complexCenter.getX(), complexCenter.getY());
262 }
263
268 System.out.println("--- Line Operations ---");
269
270 // Create segments for line
271 List<Segment> lineSegments = Arrays.asList(
272 new Segment(new Point(0.0, 0.0), new Point(10.0, 10.0)),
273 new Segment(new Point(10.0, 10.0), new Point(20.0, 5.0)),
274 new Segment(new Point(20.0, 5.0), new Point(30.0, 15.0))
275 );
276
277 // [java_Line_constructor]
278 // Create line
279 Line line = new Line(lineSegments);
280 System.out.println("Created line with " + line.getSegments().size() + " segments");
281 // [java_Line_constructor]
282
283 // [java_Line_getSegments]
284 // Get line segments
285 List<Segment> segments = line.getSegments();
286 System.out.println("Line segments: " + segments.size() + " segments");
287 // [java_Line_getSegments]
288
289 // Calculate total length of line
290 double totalLength = 0.0;
291 for (Segment segment : segments) {
292 totalLength += GeometryUtils.segmentLength(segment);
293 }
294 System.out.printf("Total line length: %.2f meters%n", totalLength);
295
296 // Create complex line
297 List<Segment> complexLineSegments = Arrays.asList(
298 new Segment(new Point(0.0, 0.0), new Point(5.0, 5.0)),
299 new Segment(new Point(5.0, 5.0), new Point(10.0, 0.0)),
300 new Segment(new Point(10.0, 0.0), new Point(15.0, 10.0)),
301 new Segment(new Point(15.0, 10.0), new Point(20.0, 5.0))
302 );
303 Line complexLine = new Line(complexLineSegments);
304
305 double complexTotalLength = 0.0;
306 for (Segment segment : complexLine.getSegments()) {
307 complexTotalLength += GeometryUtils.segmentLength(segment);
308 }
309 System.out.printf("Complex line total length: %.2f meters%n", complexTotalLength);
310 }
311
316 System.out.println("--- Advanced Geometry Operations ---");
317
318 // Test distance calculations with various scenarios
320
321 // Test intersection scenarios
323
324 // Test polygon operations with different shapes
326
327 // Test projection and ratio calculations
329 }
330
335 System.out.println("--- Distance Calculations ---");
336
337 // Test point distances
338 Point origin = new Point(0.0, 0.0);
339 List<Point> testPoints = Arrays.asList(
340 new Point(3.0, 4.0), // Should be 5.0 distance
341 new Point(6.0, 8.0), // Should be 10.0 distance
342 new Point(1.0, 1.0) // Should be sqrt(2) distance
343 );
344
345 for (Point testPoint : testPoints) {
346 double distance = GeometryUtils.distanceBetweenPoints(origin, testPoint);
347 System.out.printf("Distance from origin to (%.1f, %.1f): %.2f meters%n",
348 testPoint.getX(), testPoint.getY(), distance);
349 }
350
351 // Test global point distances
352 GlobalPoint moscow = new GlobalPoint(55.7558, 37.6176);
353 GlobalPoint london = new GlobalPoint(51.5074, -0.1278);
354 GlobalPoint tokyo = new GlobalPoint(35.6762, 139.6503);
355
356 double moscowLondon = GeometryUtils.distanceBetweenGlobalPoints(moscow, london);
357 double moscowTokyo = GeometryUtils.distanceBetweenGlobalPoints(moscow, tokyo);
358
359 System.out.printf("Moscow to London: %.2f km%n", moscowLondon / 1000);
360 System.out.printf("Moscow to Tokyo: %.2f km%n", moscowTokyo / 1000);
361 }
362
367 System.out.println("--- Intersection Scenarios ---");
368
369 // Parallel segments (should not intersect)
370 Segment parallel1 = new Segment(new Point(0.0, 0.0), new Point(10.0, 0.0));
371 Segment parallel2 = new Segment(new Point(0.0, 5.0), new Point(10.0, 5.0));
372
373 boolean parallelIntersects = GeometryUtils.segmentIntersectsSegment(parallel1, parallel2);
374 System.out.println("Parallel segments intersect: " + parallelIntersects);
375
376 // Perpendicular segments (should intersect)
377 Segment perpendicular1 = new Segment(new Point(0.0, 0.0), new Point(10.0, 0.0));
378 Segment perpendicular2 = new Segment(new Point(5.0, -5.0), new Point(5.0, 5.0));
379
380 boolean perpendicularIntersects = GeometryUtils.segmentIntersectsSegment(perpendicular1, perpendicular2);
381 System.out.println("Perpendicular segments intersect: " + perpendicularIntersects);
382
383 if (perpendicularIntersects) {
384 Point intersection = GeometryUtils.segmentIntersectionSegment(perpendicular1, perpendicular2);
385 System.out.printf("Intersection point: (%.2f, %.2f)%n", intersection.getX(), intersection.getY());
386 }
387
388 // Overlapping segments
389 Segment overlap1 = new Segment(new Point(0.0, 0.0), new Point(10.0, 0.0));
390 Segment overlap2 = new Segment(new Point(5.0, 0.0), new Point(15.0, 0.0));
391
392 boolean overlapIntersects = GeometryUtils.segmentIntersectsSegment(overlap1, overlap2);
393 System.out.println("Overlapping segments intersect: " + overlapIntersects);
394 }
395
400 System.out.println("--- Polygon Scenarios ---");
401
402 // Regular square
403 List<Point> square = Arrays.asList(
404 new Point(0.0, 0.0),
405 new Point(10.0, 0.0),
406 new Point(10.0, 10.0),
407 new Point(0.0, 10.0)
408 );
409 Polygon squarePolygon = new Polygon(square);
410
411 double squareArea = GeometryUtils.polygonArea(squarePolygon);
412 Point squareCenter = GeometryUtils.polygonCenter(squarePolygon);
413
414 System.out.printf("Square area: %.2f square meters%n", squareArea);
415 System.out.printf("Square center: (%.2f, %.2f)%n", squareCenter.getX(), squareCenter.getY());
416
417 // Triangle
418 List<Point> triangle = Arrays.asList(
419 new Point(0.0, 0.0),
420 new Point(10.0, 0.0),
421 new Point(5.0, 10.0)
422 );
423 Polygon trianglePolygon = new Polygon(triangle);
424
425 double triangleArea = GeometryUtils.polygonArea(trianglePolygon);
426 Point triangleCenter = GeometryUtils.polygonCenter(trianglePolygon);
427
428 System.out.printf("Triangle area: %.2f square meters%n", triangleArea);
429 System.out.printf("Triangle center: (%.2f, %.2f)%n", triangleCenter.getX(), triangleCenter.getY());
430
431 // Test point containment
432 Point insideSquare = new Point(5.0, 5.0);
433 Point outsideSquare = new Point(15.0, 15.0);
434
435 boolean containsInside = GeometryUtils.polygonContainsPoint(squarePolygon, insideSquare);
436 boolean containsOutside = GeometryUtils.polygonContainsPoint(squarePolygon, outsideSquare);
437
438 System.out.println("Square contains (5,5): " + containsInside);
439 System.out.println("Square contains (15,15): " + containsOutside);
440 }
441
446 System.out.println("--- Projection Calculations ---");
447
448 Segment segment = new Segment(new Point(0.0, 0.0), new Point(10.0, 10.0));
449
450 // Test different ratio points
451 List<Double> ratios = Arrays.asList(0.0, 0.25, 0.5, 0.75, 1.0);
452
453 for (Double ratio : ratios) {
454 Point ratioPoint = GeometryUtils.getRatioPoint(segment, ratio);
455 System.out.printf("Ratio %.2f: (%.2f, %.2f)%n", ratio, ratioPoint.getX(), ratioPoint.getY());
456 }
457
458 // Test projection ratio calculation
459 Point testPoint = new Point(5.0, 5.0);
460 double projectionRatio = GeometryUtils.getProjectionRatio(segment, testPoint);
461 System.out.printf("Projection ratio for point (5,5): %.2f%n", projectionRatio);
462
463 // Test distance from segment to point
464 Point offSegmentPoint = new Point(5.0, 0.0);
465 double distanceToOffPoint = GeometryUtils.segmentPointDistance(segment, offSegmentPoint);
466 System.out.printf("Distance from segment to point (5,0): %.2f meters%n", distanceToOffPoint);
467 }
468
472 public void runExample() {
473 System.out.println("=== GeometryUtils Example ===");
474
475 // Wait a bit for processing
476 try {
477 Thread.sleep(1000);
478 } catch (InterruptedException e) {
479 Thread.currentThread().interrupt();
480 }
481
482 System.out.println("=== Example completed ===");
483 }
484
488 public static void main(String[] args) {
490 example.runExample();
491 }
492}