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

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

checkstyle: enable relevant whitespace checks and fix them

  • Property svn:eol-style set to native
File size: 4.1 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 * @return true, if line is visible in the given clip region
62 */
63 private boolean cohenSutherland(long x1, long y1, long x2, long y2, long xmin, long ymin, long xmax, long ymax) {
64 int outcode0, outcode1, outcodeOut;
65 boolean accept = false;
66 boolean done = false;
67
68 outcode0 = computeOutCode(x1, y1, xmin, ymin, xmax, ymax);
69 outcode1 = computeOutCode(x2, y2, xmin, ymin, xmax, ymax);
70
71 do {
72 if ((outcode0 | outcode1) == 0) {
73 accept = true;
74 done = true;
75 } else if ((outcode0 & outcode1) > 0) {
76 done = true;
77 } else {
78 long x = 0, y = 0;
79 outcodeOut = outcode0 != 0 ? outcode0 : outcode1;
80 if ((outcodeOut & OUT_TOP) > 0) {
81 x = x1 + (x2 - x1) * (ymax - y1)/(y2 - y1);
82 y = ymax;
83 } else if ((outcodeOut & OUT_BOTTOM) > 0) {
84 x = x1 + (x2 - x1) * (ymin - y1)/(y2 - y1);
85 y = ymin;
86 } else if ((outcodeOut & OUT_RIGHT) > 0) {
87 y = y1 + (y2 - y1) * (xmax - x1)/(x2 - x1);
88 x = xmax;
89 } else if ((outcodeOut & OUT_LEFT) > 0) {
90 y = y1 + (y2 - y1) * (xmin - x1)/(x2 - x1);
91 x = xmin;
92 }
93 if (outcodeOut == outcode0) {
94 x1 = x;
95 y1 = y;
96 outcode0 = computeOutCode(x1, y1, xmin, ymin, xmax, ymax);
97 } else {
98 x2 = x;
99 y2 = y;
100 outcode1 = computeOutCode(x2, y2, xmin, ymin, xmax, ymax);
101 }
102 }
103 }
104 while (!done);
105
106 if (accept) {
107 p1 = new Point((int) x1, (int) y1);
108 p2 = new Point((int) x2, (int) y2);
109 return true;
110 }
111 return false;
112 }
113
114 /**
115 * The outcode of the point.
116 * We cannot use Rectangle.outcode since it does not work with long ints.
117 */
118 private static int computeOutCode(long x, long y, long xmin, long ymin, long xmax, long ymax) {
119 int code = 0;
120 if (y > ymax) {
121 code |= OUT_TOP;
122 } else if (y < ymin) {
123 code |= OUT_BOTTOM;
124 }
125 if (x > xmax) {
126 code |= OUT_RIGHT;
127 } else if (x < xmin) {
128 code |= OUT_LEFT;
129 }
130 return code;
131 }
132}
Note: See TracBrowser for help on using the repository browser.