80 System.out.println(
"--- GlobalPoint Operations ---");
84 GlobalPoint globalPoint1 =
new GlobalPoint(55.7558, 37.6176);
85 GlobalPoint globalPoint2 =
new GlobalPoint(59.9311, 30.3609);
86 GlobalPoint globalPoint3 =
new GlobalPoint(55.7522, 37.6156);
87 System.out.println(
"Created global points: GP1(" + globalPoint1.getLatitude() +
", " + globalPoint1.getLongitude() +
88 "), GP2(" + globalPoint2.getLatitude() +
", " + globalPoint2.getLongitude() +
")");
93 double lat1 = globalPoint1.getLatitude();
94 System.out.println(
"GlobalPoint1 latitude: " + lat1);
99 double lon1 = globalPoint1.getLongitude();
100 System.out.println(
"GlobalPoint1 longitude: " + lon1);
105 double globalDistance = GeometryUtils.distanceBetweenGlobalPoints(globalPoint1, globalPoint2);
106 System.out.printf(
"Distance between Moscow and St. Petersburg: %.2f meters%n", globalDistance);
110 double nearbyDistance = GeometryUtils.distanceBetweenGlobalPoints(globalPoint1, globalPoint3);
111 System.out.printf(
"Distance between Moscow points: %.2f meters%n", nearbyDistance);
118 System.out.println(
"--- Segment Operations ---");
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);
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() +
"))");
139 Point segment1Start = segment1.getStart();
140 System.out.println(
"Segment1 start point: (" + segment1Start.getX() +
", " + segment1Start.getY() +
")");
145 Point segment1End = segment1.getEnd();
146 System.out.println(
"Segment1 end point: (" + segment1End.getX() +
", " + segment1End.getY() +
")");
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);
159 double distanceToPoint = GeometryUtils.segmentPointDistance(segment1, testPoint);
160 System.out.printf(
"Distance from segment1 to test point: %.2f meters%n", distanceToPoint);
165 boolean intersects = GeometryUtils.segmentIntersectsSegment(segment1, segment2);
166 System.out.println(
"Segments intersect: " + intersects);
171 Point intersection = GeometryUtils.segmentIntersectionSegment(segment1, segment2);
172 System.out.printf(
"Intersection point: (%.2f, %.2f)%n", intersection.getX(), intersection.getY());
177 double divisionRatio = GeometryUtils.divisionRatioBySegment(segment1, segment2);
178 System.out.printf(
"Division ratio: %.2f%n", divisionRatio);
184 Point ratioPoint = GeometryUtils.getRatioPoint(segment1, ratio);
185 System.out.printf(
"Point at ratio %.1f: (%.2f, %.2f)%n", ratio, ratioPoint.getX(), ratioPoint.getY());
190 double projectionRatio = GeometryUtils.getProjectionRatio(segment1, testPoint);
191 System.out.printf(
"Projection ratio: %.2f%n", projectionRatio);
199 System.out.println(
"--- Polygon Operations ---");
202 List<Point> polygonPoints = Arrays.asList(
204 new Point(10.0, 0.0),
205 new Point(10.0, 10.0),
211 Polygon polygon =
new Polygon(polygonPoints);
212 System.out.println(
"Created polygon with " + polygon.getPoints().size() +
" 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));
225 double area = GeometryUtils.polygonArea(polygon);
226 System.out.printf(
"Polygon area: %.2f square meters%n", area);
231 Point center = GeometryUtils.polygonCenter(polygon);
232 System.out.printf(
"Polygon center: (%.2f, %.2f)%n", center.getX(), center.getY());
237 Point insidePoint =
new Point(5.0, 5.0);
238 Point outsidePoint =
new Point(15.0, 15.0);
240 boolean containsInside = GeometryUtils.polygonContainsPoint(polygon, insidePoint);
241 boolean containsOutside = GeometryUtils.polygonContainsPoint(polygon, outsidePoint);
243 System.out.println(
"Polygon contains inside point: " + containsInside);
244 System.out.println(
"Polygon contains outside point: " + containsOutside);
248 List<Point> complexPolygonPoints = Arrays.asList(
250 new Point(20.0, 0.0),
251 new Point(20.0, 20.0),
252 new Point(10.0, 10.0),
255 Polygon complexPolygon =
new Polygon(complexPolygonPoints);
257 double complexArea = GeometryUtils.polygonArea(complexPolygon);
258 Point complexCenter = GeometryUtils.polygonCenter(complexPolygon);
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());
268 System.out.println(
"--- Line Operations ---");
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))
279 Line line =
new Line(lineSegments);
280 System.out.println(
"Created line with " + line.getSegments().size() +
" segments");
285 List<Segment> segments = line.getSegments();
286 System.out.println(
"Line segments: " + segments.size() +
" segments");
290 double totalLength = 0.0;
291 for (Segment segment : segments) {
292 totalLength += GeometryUtils.segmentLength(segment);
294 System.out.printf(
"Total line length: %.2f meters%n", totalLength);
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))
303 Line complexLine =
new Line(complexLineSegments);
305 double complexTotalLength = 0.0;
306 for (Segment segment : complexLine.getSegments()) {
307 complexTotalLength += GeometryUtils.segmentLength(segment);
309 System.out.printf(
"Complex line total length: %.2f meters%n", complexTotalLength);
335 System.out.println(
"--- Distance Calculations ---");
338 Point origin =
new Point(0.0, 0.0);
339 List<Point> testPoints = Arrays.asList(
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);
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);
356 double moscowLondon = GeometryUtils.distanceBetweenGlobalPoints(moscow, london);
357 double moscowTokyo = GeometryUtils.distanceBetweenGlobalPoints(moscow, tokyo);
359 System.out.printf(
"Moscow to London: %.2f km%n", moscowLondon / 1000);
360 System.out.printf(
"Moscow to Tokyo: %.2f km%n", moscowTokyo / 1000);
367 System.out.println(
"--- Intersection Scenarios ---");
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));
373 boolean parallelIntersects = GeometryUtils.segmentIntersectsSegment(parallel1, parallel2);
374 System.out.println(
"Parallel segments intersect: " + parallelIntersects);
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));
380 boolean perpendicularIntersects = GeometryUtils.segmentIntersectsSegment(perpendicular1, perpendicular2);
381 System.out.println(
"Perpendicular segments intersect: " + perpendicularIntersects);
383 if (perpendicularIntersects) {
384 Point intersection = GeometryUtils.segmentIntersectionSegment(perpendicular1, perpendicular2);
385 System.out.printf(
"Intersection point: (%.2f, %.2f)%n", intersection.getX(), intersection.getY());
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));
392 boolean overlapIntersects = GeometryUtils.segmentIntersectsSegment(overlap1, overlap2);
393 System.out.println(
"Overlapping segments intersect: " + overlapIntersects);
400 System.out.println(
"--- Polygon Scenarios ---");
403 List<Point> square = Arrays.asList(
405 new Point(10.0, 0.0),
406 new Point(10.0, 10.0),
409 Polygon squarePolygon =
new Polygon(square);
411 double squareArea = GeometryUtils.polygonArea(squarePolygon);
412 Point squareCenter = GeometryUtils.polygonCenter(squarePolygon);
414 System.out.printf(
"Square area: %.2f square meters%n", squareArea);
415 System.out.printf(
"Square center: (%.2f, %.2f)%n", squareCenter.getX(), squareCenter.getY());
418 List<Point> triangle = Arrays.asList(
420 new Point(10.0, 0.0),
423 Polygon trianglePolygon =
new Polygon(triangle);
425 double triangleArea = GeometryUtils.polygonArea(trianglePolygon);
426 Point triangleCenter = GeometryUtils.polygonCenter(trianglePolygon);
428 System.out.printf(
"Triangle area: %.2f square meters%n", triangleArea);
429 System.out.printf(
"Triangle center: (%.2f, %.2f)%n", triangleCenter.getX(), triangleCenter.getY());
432 Point insideSquare =
new Point(5.0, 5.0);
433 Point outsideSquare =
new Point(15.0, 15.0);
435 boolean containsInside = GeometryUtils.polygonContainsPoint(squarePolygon, insideSquare);
436 boolean containsOutside = GeometryUtils.polygonContainsPoint(squarePolygon, outsideSquare);
438 System.out.println(
"Square contains (5,5): " + containsInside);
439 System.out.println(
"Square contains (15,15): " + containsOutside);
446 System.out.println(
"--- Projection Calculations ---");
448 Segment segment =
new Segment(
new Point(0.0, 0.0),
new Point(10.0, 10.0));
451 List<Double> ratios = Arrays.asList(0.0, 0.25, 0.5, 0.75, 1.0);
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());
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);
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);