source: josm/trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/LineClip.java@ 10827

Last change on this file since 10827 was 10827, checked in by Don-vip, 8 years ago

fix #13306 - Make map paint code use double coordinates (patch by michael2402) - gsoc-core

  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm.visitor.paint;
3
4import static java.awt.geom.Rectangle2D.OUT_BOTTOM;
5import static java.awt.geom.Rectangle2D.OUT_LEFT;
6import static java.awt.geom.Rectangle2D.OUT_RIGHT;
7import static java.awt.geom.Rectangle2D.OUT_TOP;
8
9import java.awt.Rectangle;
10import java.awt.geom.Point2D;
11import java.awt.geom.Rectangle2D;
12
13/**
14 * Computes the part of a line that is visible in a given rectangle.
15 * Using int leads to overflow, so we need long int.
16 */
17public class LineClip {
18 private Point2D p1, p2;
19 private final Rectangle2D clipBounds;
20
21 /**
22 * Constructs a new {@code LineClip}.
23 * @param p1 start point of the clipped line
24 * @param p2 end point of the clipped line
25 * @param clipBounds Clip bounds
26 * @since 10826
27 */
28 public LineClip(Point2D p1, Point2D p2, Rectangle2D clipBounds) {
29 this.p1 = p1;
30 this.p2 = p2;
31 this.clipBounds = clipBounds;
32 }
33
34 /**
35 * run the clipping algorithm
36 * @return true if the some parts of the line lies within the clip bounds
37 */
38 public boolean execute() {
39 if (clipBounds == null) {
40 return false;
41 }
42 return cohenSutherland(p1.getX(), p1.getY(), p2.getX(), p2.getY(), clipBounds.getMinX(), clipBounds.getMinY(),
43 clipBounds.getMaxX(), clipBounds.getMaxY());
44 }
45
46 /**
47 * @return start point of the clipped line
48 * @since 10826
49 */
50 public Point2D getP1() {
51 return p1;
52 }
53
54 /**
55 * @return end point of the clipped line
56 * @since 10826
57 */
58 public Point2D getP2() {
59 return p2;
60 }
61
62 /**
63 * Cohen–Sutherland algorithm.
64 * See <a href="https://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm">Wikipedia article</a>
65 * @param x1 X coordinate of first point
66 * @param y1 Y coordinate of first point
67 * @param x2 X coordinate of second point
68 * @param y2 Y coordinate of second point
69 * @param xmin minimal X coordinate
70 * @param ymin minimal Y coordinate
71 * @param xmax maximal X coordinate
72 * @param ymax maximal Y coordinate
73 * @return true, if line is visible in the given clip region
74 */
75 private boolean cohenSutherland(double x1, double y1, double x2, double y2, double xmin, double ymin, double xmax, double ymax) {
76 int outcode0, outcode1, outcodeOut;
77 boolean accept = false;
78 boolean done = false;
79
80 outcode0 = computeOutCode(x1, y1, xmin, ymin, xmax, ymax);
81 outcode1 = computeOutCode(x2, y2, xmin, ymin, xmax, ymax);
82
83 do {
84 if ((outcode0 | outcode1) == 0) {
85 accept = true;
86 done = true;
87 } else if ((outcode0 & outcode1) > 0) {
88 done = true;
89 } else {
90 double x = 0;
91 double y = 0;
92 outcodeOut = outcode0 != 0 ? outcode0 : outcode1;
93 if ((outcodeOut & OUT_TOP) != 0) {
94 x = x1 + (x2 - x1) * (ymax - y1)/(y2 - y1);
95 y = ymax;
96 } else if ((outcodeOut & OUT_BOTTOM) != 0) {
97 x = x1 + (x2 - x1) * (ymin - y1)/(y2 - y1);
98 y = ymin;
99 } else if ((outcodeOut & OUT_RIGHT) != 0) {
100 y = y1 + (y2 - y1) * (xmax - x1)/(x2 - x1);
101 x = xmax;
102 } else if ((outcodeOut & OUT_LEFT) != 0) {
103 y = y1 + (y2 - y1) * (xmin - x1)/(x2 - x1);
104 x = xmin;
105 }
106 if (outcodeOut == outcode0) {
107 x1 = x;
108 y1 = y;
109 outcode0 = computeOutCode(x1, y1, xmin, ymin, xmax, ymax);
110 } else {
111 x2 = x;
112 y2 = y;
113 outcode1 = computeOutCode(x2, y2, xmin, ymin, xmax, ymax);
114 }
115 }
116 }
117 while (!done);
118
119 if (accept) {
120 p1 = new Point2D.Double(x1, y1);
121 p2 = new Point2D.Double(x2, y2);
122 return true;
123 }
124 return false;
125 }
126
127 /**
128 * The outcode of the point.
129 * We cannot use {@link Rectangle#outcode} since it does not work with long ints.
130 * @param x X coordinate
131 * @param y Y coordinate
132 * @param xmin minimal X coordinate
133 * @param ymin minimal Y coordinate
134 * @param xmax maximal X coordinate
135 * @param ymax maximal Y coordinate
136 * @return outcode
137 */
138 private static int computeOutCode(double x, double y, double xmin, double ymin, double xmax, double ymax) {
139 int code = 0;
140 // ignore rounding errors.
141 if (y > ymax + 1e-10) {
142 code |= OUT_TOP;
143 } else if (y < ymin - 1e-10) {
144 code |= OUT_BOTTOM;
145 }
146 if (x > xmax + 1e-10) {
147 code |= OUT_RIGHT;
148 } else if (x < xmin - 1e-10) {
149 code |= OUT_LEFT;
150 }
151 return code;
152 }
153}
Note: See TracBrowser for help on using the repository browser.