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

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

javadoc update

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