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