source: josm/trunk/src/org/openstreetmap/josm/gui/MapViewState.java@ 16919

Last change on this file since 16919 was 16919, checked in by simon04, 5 years ago

Fix typos in Javadoc

  • Property svn:eol-style set to native
File size: 26.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import java.awt.Container;
5import java.awt.Point;
6import java.awt.geom.AffineTransform;
7import java.awt.geom.Area;
8import java.awt.geom.Path2D;
9import java.awt.geom.Point2D;
10import java.awt.geom.Point2D.Double;
11import java.awt.geom.Rectangle2D;
12import java.io.Serializable;
13import java.util.Objects;
14import java.util.Optional;
15
16import javax.swing.JComponent;
17
18import org.openstreetmap.josm.data.Bounds;
19import org.openstreetmap.josm.data.ProjectionBounds;
20import org.openstreetmap.josm.data.coor.EastNorth;
21import org.openstreetmap.josm.data.coor.ILatLon;
22import org.openstreetmap.josm.data.coor.LatLon;
23import org.openstreetmap.josm.data.osm.Node;
24import org.openstreetmap.josm.data.projection.Projecting;
25import org.openstreetmap.josm.data.projection.Projection;
26import org.openstreetmap.josm.data.projection.ProjectionRegistry;
27import org.openstreetmap.josm.gui.download.DownloadDialog;
28import org.openstreetmap.josm.tools.CheckParameterUtil;
29import org.openstreetmap.josm.tools.Geometry;
30import org.openstreetmap.josm.tools.JosmRuntimeException;
31import org.openstreetmap.josm.tools.bugreport.BugReport;
32
33/**
34 * This class represents a state of the {@link MapView}.
35 * @author Michael Zangl
36 * @since 10343
37 */
38public final class MapViewState implements Serializable {
39
40 private static final long serialVersionUID = 1L;
41
42 /**
43 * A flag indicating that the point is outside to the top of the map view.
44 * @since 10827
45 */
46 public static final int OUTSIDE_TOP = 1;
47
48 /**
49 * A flag indicating that the point is outside to the bottom of the map view.
50 * @since 10827
51 */
52 public static final int OUTSIDE_BOTTOM = 2;
53
54 /**
55 * A flag indicating that the point is outside to the left of the map view.
56 * @since 10827
57 */
58 public static final int OUTSIDE_LEFT = 4;
59
60 /**
61 * A flag indicating that the point is outside to the right of the map view.
62 * @since 10827
63 */
64 public static final int OUTSIDE_RIGHT = 8;
65
66 /**
67 * Additional pixels outside the view for where to start clipping.
68 */
69 private static final int CLIP_BOUNDS = 50;
70
71 private final transient Projecting projecting;
72
73 private final int viewWidth;
74 private final int viewHeight;
75
76 private final double scale;
77
78 /**
79 * Top left {@link EastNorth} coordinate of the view.
80 */
81 private final EastNorth topLeft;
82
83 private final Point topLeftOnScreen;
84 private final Point topLeftInWindow;
85
86 /**
87 * Create a new {@link MapViewState}
88 * @param projection The projection to use.
89 * @param viewWidth The view width
90 * @param viewHeight The view height
91 * @param scale The scale to use
92 * @param topLeft The top left corner in east/north space.
93 * @param topLeftInWindow The top left point in window
94 * @param topLeftOnScreen The top left point on screen
95 */
96 private MapViewState(Projecting projection, int viewWidth, int viewHeight, double scale, EastNorth topLeft,
97 Point topLeftInWindow, Point topLeftOnScreen) {
98 CheckParameterUtil.ensureParameterNotNull(projection, "projection");
99 CheckParameterUtil.ensureParameterNotNull(topLeft, "topLeft");
100 CheckParameterUtil.ensureParameterNotNull(topLeftInWindow, "topLeftInWindow");
101 CheckParameterUtil.ensureParameterNotNull(topLeftOnScreen, "topLeftOnScreen");
102
103 this.projecting = projection;
104 this.scale = scale;
105 this.topLeft = topLeft;
106
107 this.viewWidth = viewWidth;
108 this.viewHeight = viewHeight;
109 this.topLeftInWindow = topLeftInWindow;
110 this.topLeftOnScreen = topLeftOnScreen;
111 }
112
113 private MapViewState(Projecting projection, int viewWidth, int viewHeight, double scale, EastNorth topLeft) {
114 this(projection, viewWidth, viewHeight, scale, topLeft, new Point(0, 0), new Point(0, 0));
115 }
116
117 private MapViewState(EastNorth topLeft, MapViewState mvs) {
118 this(mvs.projecting, mvs.viewWidth, mvs.viewHeight, mvs.scale, topLeft, mvs.topLeftInWindow, mvs.topLeftOnScreen);
119 }
120
121 private MapViewState(double scale, MapViewState mvs) {
122 this(mvs.projecting, mvs.viewWidth, mvs.viewHeight, scale, mvs.topLeft, mvs.topLeftInWindow, mvs.topLeftOnScreen);
123 }
124
125 private MapViewState(JComponent position, MapViewState mvs) {
126 this(mvs.projecting, position.getWidth(), position.getHeight(), mvs.scale, mvs.topLeft,
127 findTopLeftInWindow(position), findTopLeftOnScreen(position));
128 }
129
130 private MapViewState(Projecting projecting, MapViewState mvs) {
131 this(projecting, mvs.viewWidth, mvs.viewHeight, mvs.scale, mvs.topLeft, mvs.topLeftInWindow, mvs.topLeftOnScreen);
132 }
133
134 private static Point findTopLeftInWindow(JComponent position) {
135 Point result = new Point();
136 // better than using swing utils, since this allows us to use the method if no screen is present.
137 Container component = position;
138 while (component != null) {
139 result.x += component.getX();
140 result.y += component.getY();
141 component = component.getParent();
142 }
143 return result;
144 }
145
146 private static Point findTopLeftOnScreen(JComponent position) {
147 try {
148 return position.getLocationOnScreen();
149 } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
150 throw BugReport.intercept(e).put("position", position).put("parent", position::getParent);
151 }
152 }
153
154 @Override
155 public String toString() {
156 return getClass().getName() + " [projecting=" + this.projecting
157 + " viewWidth=" + this.viewWidth
158 + " viewHeight=" + this.viewHeight
159 + " scale=" + this.scale
160 + " topLeft=" + this.topLeft + ']';
161 }
162
163 /**
164 * The scale in east/north units per pixel.
165 * @return The scale.
166 */
167 public double getScale() {
168 return scale;
169 }
170
171 /**
172 * Gets the MapViewPoint representation for a position in view coordinates.
173 * @param x The x coordinate inside the view.
174 * @param y The y coordinate inside the view.
175 * @return The MapViewPoint.
176 */
177 public MapViewPoint getForView(double x, double y) {
178 return new MapViewViewPoint(x, y);
179 }
180
181 /**
182 * Gets the {@link MapViewPoint} for the given {@link EastNorth} coordinate.
183 * @param eastNorth the position.
184 * @return The point for that position.
185 */
186 public MapViewPoint getPointFor(EastNorth eastNorth) {
187 return new MapViewEastNorthPoint(eastNorth);
188 }
189
190 /**
191 * Gets the {@link MapViewPoint} for the given {@link LatLon} coordinate.
192 * <p>
193 * This method exists to not break binary compatibility with old plugins
194 * @param latlon the position
195 * @return The point for that position.
196 * @since 10651
197 */
198 public MapViewPoint getPointFor(LatLon latlon) {
199 return getPointFor((ILatLon) latlon);
200 }
201
202 /**
203 * Gets the {@link MapViewPoint} for the given {@link LatLon} coordinate.
204 * @param latlon the position
205 * @return The point for that position.
206 * @since 12161
207 */
208 public MapViewPoint getPointFor(ILatLon latlon) {
209 try {
210 return getPointFor(Optional.ofNullable(latlon.getEastNorth(getProjection()))
211 .orElseThrow(IllegalArgumentException::new));
212 } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
213 throw BugReport.intercept(e).put("latlon", latlon);
214 }
215 }
216
217 /**
218 * Gets the {@link MapViewPoint} for the given node.
219 * This is faster than {@link #getPointFor(LatLon)} because it uses the node east/north cache.
220 * @param node The node
221 * @return The position of that node.
222 * @since 10827
223 */
224 public MapViewPoint getPointFor(Node node) {
225 return getPointFor((ILatLon) node);
226 }
227
228 /**
229 * Gets a rectangle representing the whole view area.
230 * @return The rectangle.
231 */
232 public MapViewRectangle getViewArea() {
233 return getForView(0, 0).rectTo(getForView(viewWidth, viewHeight));
234 }
235
236 /**
237 * Gets a rectangle of the view as map view area.
238 * @param rectangle The rectangle to get.
239 * @return The view area.
240 * @since 10827
241 */
242 public MapViewRectangle getViewArea(Rectangle2D rectangle) {
243 return getForView(rectangle.getMinX(), rectangle.getMinY()).rectTo(getForView(rectangle.getMaxX(), rectangle.getMaxY()));
244 }
245
246 /**
247 * Gets the center of the view.
248 * @return The center position.
249 */
250 public MapViewPoint getCenter() {
251 return getForView(viewWidth / 2.0, viewHeight / 2.0);
252 }
253
254 /**
255 * Gets the width of the view on the Screen;
256 * @return The width of the view component in screen pixel.
257 */
258 public double getViewWidth() {
259 return viewWidth;
260 }
261
262 /**
263 * Gets the height of the view on the Screen;
264 * @return The height of the view component in screen pixel.
265 */
266 public double getViewHeight() {
267 return viewHeight;
268 }
269
270 /**
271 * Gets the current projection used for the MapView.
272 * @return The projection.
273 * @see #getProjecting()
274 */
275 public Projection getProjection() {
276 return projecting.getBaseProjection();
277 }
278
279 /**
280 * Gets the current projecting instance that is used to convert between east/north and lat/lon space.
281 * @return The projection.
282 * @since 12161
283 */
284 public Projecting getProjecting() {
285 return projecting;
286 }
287
288 /**
289 * Creates an affine transform that is used to convert the east/north coordinates to view coordinates.
290 * @return The affine transform. It should not be changed.
291 * @since 10375
292 */
293 public AffineTransform getAffineTransform() {
294 return new AffineTransform(1.0 / scale, 0.0, 0.0, -1.0 / scale, -topLeft.east() / scale,
295 topLeft.north() / scale);
296 }
297
298 /**
299 * Gets a rectangle that is several pixel bigger than the view. It is used to define the view clipping.
300 * @return The rectangle.
301 */
302 public MapViewRectangle getViewClipRectangle() {
303 return getForView(-CLIP_BOUNDS, -CLIP_BOUNDS).rectTo(getForView(getViewWidth() + CLIP_BOUNDS, getViewHeight() + CLIP_BOUNDS));
304 }
305
306 /**
307 * Returns the area for the given bounds.
308 * @param bounds bounds
309 * @return the area for the given bounds
310 */
311 public Area getArea(Bounds bounds) {
312 Path2D area = new Path2D.Double();
313 getProjection().visitOutline(bounds, en -> {
314 MapViewPoint point = getPointFor(en);
315 if (area.getCurrentPoint() == null) {
316 area.moveTo(point.getInViewX(), point.getInViewY());
317 } else {
318 area.lineTo(point.getInViewX(), point.getInViewY());
319 }
320 });
321 area.closePath();
322 return new Area(area);
323 }
324
325 /**
326 * Creates a new state that is the same as the current state except for that it is using a new center.
327 * @param newCenter The new center coordinate.
328 * @return The new state.
329 * @since 10375
330 */
331 public MapViewState usingCenter(EastNorth newCenter) {
332 return movedTo(getCenter(), newCenter);
333 }
334
335 /**
336 * Creates a new state that is moved to an east/north coordinate.
337 * @param mapViewPoint The reference point.
338 * @param newEastNorthThere The east/north coordinate that should be there.
339 * @return The new state.
340 * @since 10375
341 */
342 public MapViewState movedTo(MapViewPoint mapViewPoint, EastNorth newEastNorthThere) {
343 EastNorth delta = newEastNorthThere.subtract(mapViewPoint.getEastNorth());
344 if (delta.distanceSq(0, 0) < .1e-20) {
345 return this;
346 } else {
347 return new MapViewState(topLeft.add(delta), this);
348 }
349 }
350
351 /**
352 * Creates a new state that is the same as the current state except for that it is using a new scale.
353 * @param newScale The new scale to use.
354 * @return The new state.
355 * @since 10375
356 */
357 public MapViewState usingScale(double newScale) {
358 return new MapViewState(newScale, this);
359 }
360
361 /**
362 * Creates a new state that is the same as the current state except for that it is using the location of the given component.
363 * <p>
364 * The view is moved so that the center is the same as the old center.
365 * @param position The new location to use.
366 * @return The new state.
367 * @since 10375
368 */
369 public MapViewState usingLocation(JComponent position) {
370 EastNorth center = this.getCenter().getEastNorth();
371 return new MapViewState(position, this).usingCenter(center);
372 }
373
374 /**
375 * Creates a state that uses the projection.
376 * @param projection The projection to use.
377 * @return The new state.
378 * @since 10486
379 */
380 public MapViewState usingProjection(Projection projection) {
381 if (projection.equals(this.projecting)) {
382 return this;
383 } else {
384 return new MapViewState(projection, this);
385 }
386 }
387
388 /**
389 * Create the default {@link MapViewState} object for the given map view. The screen position won't be set so that this method can be used
390 * before the view was added to the hierarchy.
391 * @param width The view width
392 * @param height The view height
393 * @return The state
394 * @since 10375
395 */
396 public static MapViewState createDefaultState(int width, int height) {
397 Projection projection = ProjectionRegistry.getProjection();
398 double scale = projection.getDefaultZoomInPPD();
399 MapViewState state = new MapViewState(projection, width, height, scale, new EastNorth(0, 0));
400 EastNorth center = calculateDefaultCenter();
401 return state.movedTo(state.getCenter(), center);
402 }
403
404 private static EastNorth calculateDefaultCenter() {
405 Bounds b = Optional.ofNullable(DownloadDialog.getSavedDownloadBounds()).orElseGet(
406 () -> ProjectionRegistry.getProjection().getWorldBoundsLatLon());
407 return b.getCenter().getEastNorth(ProjectionRegistry.getProjection());
408 }
409
410 /**
411 * Check if this MapViewState equals another one, disregarding the position
412 * of the JOSM window on screen.
413 * @param other the other MapViewState
414 * @return true if the other MapViewState has the same size, scale, position and projection,
415 * false otherwise
416 */
417 public boolean equalsInWindow(MapViewState other) {
418 return other != null &&
419 this.viewWidth == other.viewWidth &&
420 this.viewHeight == other.viewHeight &&
421 this.scale == other.scale &&
422 Objects.equals(this.topLeft, other.topLeft) &&
423 Objects.equals(this.projecting, other.projecting);
424 }
425
426 /**
427 * A class representing a point in the map view. It allows to convert between the different coordinate systems.
428 * @author Michael Zangl
429 */
430 public abstract class MapViewPoint {
431 /**
432 * Gets the map view state this path is used for.
433 * @return The state.
434 * @since 12505
435 */
436 public MapViewState getMapViewState() {
437 return MapViewState.this;
438 }
439
440 /**
441 * Get this point in view coordinates.
442 * @return The point in view coordinates.
443 */
444 public Point2D getInView() {
445 return new Point2D.Double(getInViewX(), getInViewY());
446 }
447
448 /**
449 * Get the x coordinate in view space without creating an intermediate object.
450 * @return The x coordinate
451 * @since 10827
452 */
453 public abstract double getInViewX();
454
455 /**
456 * Get the y coordinate in view space without creating an intermediate object.
457 * @return The y coordinate
458 * @since 10827
459 */
460 public abstract double getInViewY();
461
462 /**
463 * Convert this point to window coordinates.
464 * @return The point in window coordinates.
465 */
466 public Point2D getInWindow() {
467 return getUsingCorner(topLeftInWindow);
468 }
469
470 /**
471 * Convert this point to screen coordinates.
472 * @return The point in screen coordinates.
473 */
474 public Point2D getOnScreen() {
475 return getUsingCorner(topLeftOnScreen);
476 }
477
478 private Double getUsingCorner(Point corner) {
479 return new Point2D.Double(corner.getX() + getInViewX(), corner.getY() + getInViewY());
480 }
481
482 /**
483 * Gets the {@link EastNorth} coordinate of this point.
484 * @return The east/north coordinate.
485 */
486 public EastNorth getEastNorth() {
487 return new EastNorth(topLeft.east() + getInViewX() * scale, topLeft.north() - getInViewY() * scale);
488 }
489
490 /**
491 * Create a rectangle from this to the other point.
492 * @param other The other point. Needs to be of the same {@link MapViewState}
493 * @return A rectangle.
494 */
495 public MapViewRectangle rectTo(MapViewPoint other) {
496 return new MapViewRectangle(this, other);
497 }
498
499 /**
500 * Gets the current position in LatLon coordinates according to the current projection.
501 * @return The position as LatLon.
502 * @see #getLatLonClamped()
503 */
504 public LatLon getLatLon() {
505 return projecting.getBaseProjection().eastNorth2latlon(getEastNorth());
506 }
507
508 /**
509 * Gets the latlon coordinate clamped to the current world area.
510 * @return The lat/lon coordinate
511 * @since 10805
512 */
513 public LatLon getLatLonClamped() {
514 return projecting.eastNorth2latlonClamped(getEastNorth());
515 }
516
517 /**
518 * Add the given offset to this point
519 * @param en The offset in east/north space.
520 * @return The new point
521 * @since 10651
522 */
523 public MapViewPoint add(EastNorth en) {
524 return new MapViewEastNorthPoint(getEastNorth().add(en));
525 }
526
527 /**
528 * Check if this point is inside the view bounds.
529 *
530 * This is the case iff <code>getOutsideRectangleFlags(getViewArea())</code> returns no flags
531 * @return true if it is.
532 * @since 10827
533 */
534 public boolean isInView() {
535 return inRange(getInViewX(), 0, getViewWidth()) && inRange(getInViewY(), 0, getViewHeight());
536 }
537
538 private boolean inRange(double val, int min, double max) {
539 return val >= min && val < max;
540 }
541
542 /**
543 * Gets the direction in which this point is outside of the given view rectangle.
544 * @param rect The rectangle to check against.
545 * @return The direction in which it is outside of the view, as OUTSIDE_... flags.
546 * @since 10827
547 */
548 public int getOutsideRectangleFlags(MapViewRectangle rect) {
549 Rectangle2D bounds = rect.getInView();
550 int flags = 0;
551 if (getInViewX() < bounds.getMinX()) {
552 flags |= OUTSIDE_LEFT;
553 } else if (getInViewX() > bounds.getMaxX()) {
554 flags |= OUTSIDE_RIGHT;
555 }
556 if (getInViewY() < bounds.getMinY()) {
557 flags |= OUTSIDE_TOP;
558 } else if (getInViewY() > bounds.getMaxY()) {
559 flags |= OUTSIDE_BOTTOM;
560 }
561
562 return flags;
563 }
564
565 /**
566 * Gets the sum of the x/y view distances between the points. |x1 - x2| + |y1 - y2|
567 * @param p2 The other point
568 * @return The norm
569 * @since 10827
570 */
571 public double oneNormInView(MapViewPoint p2) {
572 return Math.abs(getInViewX() - p2.getInViewX()) + Math.abs(getInViewY() - p2.getInViewY());
573 }
574
575 /**
576 * Gets the squared distance between this point and an other point.
577 * @param p2 The other point
578 * @return The squared distance.
579 * @since 10827
580 */
581 public double distanceToInViewSq(MapViewPoint p2) {
582 double dx = getInViewX() - p2.getInViewX();
583 double dy = getInViewY() - p2.getInViewY();
584 return dx * dx + dy * dy;
585 }
586
587 /**
588 * Gets the distance between this point and an other point.
589 * @param p2 The other point
590 * @return The distance.
591 * @since 10827
592 */
593 public double distanceToInView(MapViewPoint p2) {
594 return Math.sqrt(distanceToInViewSq(p2));
595 }
596
597 /**
598 * Do a linear interpolation to the other point
599 * @param p1 The other point
600 * @param i The interpolation factor. 0 is at the current point, 1 at the other point.
601 * @return The new point
602 * @since 10874
603 */
604 public MapViewPoint interpolate(MapViewPoint p1, double i) {
605 return new MapViewViewPoint((1 - i) * getInViewX() + i * p1.getInViewX(), (1 - i) * getInViewY() + i * p1.getInViewY());
606 }
607 }
608
609 private class MapViewViewPoint extends MapViewPoint {
610 private final double x;
611 private final double y;
612
613 MapViewViewPoint(double x, double y) {
614 this.x = x;
615 this.y = y;
616 }
617
618 @Override
619 public double getInViewX() {
620 return x;
621 }
622
623 @Override
624 public double getInViewY() {
625 return y;
626 }
627
628 @Override
629 public String toString() {
630 return "MapViewViewPoint [x=" + x + ", y=" + y + ']';
631 }
632 }
633
634 private class MapViewEastNorthPoint extends MapViewPoint {
635
636 private final EastNorth eastNorth;
637
638 MapViewEastNorthPoint(EastNorth eastNorth) {
639 this.eastNorth = Objects.requireNonNull(eastNorth, "eastNorth");
640 }
641
642 @Override
643 public double getInViewX() {
644 return (eastNorth.east() - topLeft.east()) / scale;
645 }
646
647 @Override
648 public double getInViewY() {
649 return (topLeft.north() - eastNorth.north()) / scale;
650 }
651
652 @Override
653 public EastNorth getEastNorth() {
654 return eastNorth;
655 }
656
657 @Override
658 public String toString() {
659 return "MapViewEastNorthPoint [eastNorth=" + eastNorth + ']';
660 }
661 }
662
663 /**
664 * A rectangle on the MapView. It is rectangular in screen / EastNorth space.
665 * @author Michael Zangl
666 */
667 public class MapViewRectangle {
668 private final MapViewPoint p1;
669 private final MapViewPoint p2;
670
671 /**
672 * Create a new MapViewRectangle
673 * @param p1 The first point to use
674 * @param p2 The second point to use.
675 */
676 MapViewRectangle(MapViewPoint p1, MapViewPoint p2) {
677 this.p1 = p1;
678 this.p2 = p2;
679 }
680
681 /**
682 * Gets the projection bounds for this rectangle.
683 * @return The projection bounds.
684 */
685 public ProjectionBounds getProjectionBounds() {
686 ProjectionBounds b = new ProjectionBounds(p1.getEastNorth());
687 b.extend(p2.getEastNorth());
688 return b;
689 }
690
691 /**
692 * Gets a rough estimate of the bounds by assuming lat/lon are parallel to x/y.
693 * @return The bounds computed by converting the corners of this rectangle.
694 * @see #getLatLonBoundsBox()
695 */
696 public Bounds getCornerBounds() {
697 Bounds b = new Bounds(p1.getLatLon());
698 b.extend(p2.getLatLon());
699 return b;
700 }
701
702 /**
703 * Gets the real bounds that enclose this rectangle.
704 * This is computed respecting that the borders of this rectangle may not be a straignt line in latlon coordinates.
705 * @return The bounds.
706 * @since 10458
707 */
708 public Bounds getLatLonBoundsBox() {
709 // TODO @michael2402: Use hillclimb.
710 return projecting.getBaseProjection().getLatLonBoundsBox(getProjectionBounds());
711 }
712
713 /**
714 * Gets this rectangle on the screen.
715 * @return The rectangle.
716 * @since 10651
717 */
718 public Rectangle2D getInView() {
719 double x1 = p1.getInViewX();
720 double y1 = p1.getInViewY();
721 double x2 = p2.getInViewX();
722 double y2 = p2.getInViewY();
723 return new Rectangle2D.Double(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
724 }
725
726 /**
727 * Check if the rectangle intersects the map view area.
728 * @return <code>true</code> if it intersects.
729 * @since 10827
730 */
731 public boolean isInView() {
732 return getInView().intersects(getViewArea().getInView());
733 }
734
735 /**
736 * Gets the entry point at which a line between start and end enters the current view.
737 * @param start The start
738 * @param end The end
739 * @return The entry point or <code>null</code> if the line does not intersect this view.
740 */
741 public MapViewPoint getLineEntry(MapViewPoint start, MapViewPoint end) {
742 ProjectionBounds bounds = getProjectionBounds();
743 EastNorth enStart = start.getEastNorth();
744 if (bounds.contains(enStart)) {
745 return start;
746 }
747
748 EastNorth enEnd = end.getEastNorth();
749 double dx = enEnd.east() - enStart.east();
750 double boundX = dx > 0 ? bounds.minEast : bounds.maxEast;
751 EastNorth borderIntersection = Geometry.getSegmentSegmentIntersection(enStart, enEnd,
752 new EastNorth(boundX, bounds.minNorth),
753 new EastNorth(boundX, bounds.maxNorth));
754 if (borderIntersection != null) {
755 return getPointFor(borderIntersection);
756 }
757
758 double dy = enEnd.north() - enStart.north();
759 double boundY = dy > 0 ? bounds.minNorth : bounds.maxNorth;
760 borderIntersection = Geometry.getSegmentSegmentIntersection(enStart, enEnd,
761 new EastNorth(bounds.minEast, boundY),
762 new EastNorth(bounds.maxEast, boundY));
763 if (borderIntersection != null) {
764 return getPointFor(borderIntersection);
765 }
766
767 return null;
768 }
769
770 @Override
771 public String toString() {
772 return "MapViewRectangle [p1=" + p1 + ", p2=" + p2 + ']';
773 }
774 }
775}
Note: See TracBrowser for help on using the repository browser.