1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm.visitor.paint;
|
---|
3 |
|
---|
4 | import static java.awt.geom.Rectangle2D.OUT_LEFT;
|
---|
5 | import static java.awt.geom.Rectangle2D.OUT_RIGHT;
|
---|
6 | import static java.awt.geom.Rectangle2D.OUT_TOP;
|
---|
7 | import static java.awt.geom.Rectangle2D.OUT_BOTTOM;
|
---|
8 | import java.awt.Point;
|
---|
9 | import java.awt.Rectangle;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Computes the part of a line that is visible in a given rectangle.
|
---|
13 | * Using int leads to overflow, so we need long int.
|
---|
14 | */
|
---|
15 | public class LineClip {
|
---|
16 | private Point p1, p2;
|
---|
17 | private final Rectangle clipBounds;
|
---|
18 |
|
---|
19 | public LineClip(Point p1, Point p2, Rectangle clipBounds) {
|
---|
20 | this.p1 = p1;
|
---|
21 | this.p2 = p2;
|
---|
22 | this.clipBounds = clipBounds;
|
---|
23 | }
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * run the clipping algorithm
|
---|
27 | * @return true if the some parts of the line lies within the clip bounds
|
---|
28 | */
|
---|
29 | public boolean execute() {
|
---|
30 | return cohenSutherland(p1.x, p1.y, p2.x, p2.y, clipBounds.x , clipBounds.y, clipBounds.x + clipBounds.width, clipBounds.y + clipBounds.height);
|
---|
31 | }
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @return start point of the clipped line
|
---|
35 | */
|
---|
36 | public Point getP1()
|
---|
37 | {
|
---|
38 | return p1;
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * @return end point of the clipped line
|
---|
43 | */
|
---|
44 | public Point getP2()
|
---|
45 | {
|
---|
46 | return p2;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * see http://en.wikipedia.org/wiki/Cohen-Sutherland
|
---|
51 | */
|
---|
52 | private boolean cohenSutherland( long x1, long y1, long x2, long y2, long xmin, long ymin, long xmax, long ymax)
|
---|
53 | {
|
---|
54 | int outcode0, outcode1, outcodeOut;
|
---|
55 | boolean accept = false;
|
---|
56 | boolean done = false;
|
---|
57 |
|
---|
58 | outcode0 = computeOutCode (x1, y1, xmin, ymin, xmax, ymax);
|
---|
59 | outcode1 = computeOutCode (x2, y2, xmin, ymin, xmax, ymax);
|
---|
60 |
|
---|
61 | do {
|
---|
62 | if ((outcode0 | outcode1) == 0 ) {
|
---|
63 | accept = true;
|
---|
64 | done = true;
|
---|
65 | }
|
---|
66 | else if ( (outcode0 & outcode1) > 0 ) {
|
---|
67 | done = true;
|
---|
68 | }
|
---|
69 | else {
|
---|
70 | long x = 0, y = 0;
|
---|
71 | outcodeOut = outcode0 != 0 ? outcode0: outcode1;
|
---|
72 | if ( (outcodeOut & OUT_TOP) > 0 ) {
|
---|
73 | x = x1 + (x2 - x1) * (ymax - y1)/(y2 - y1);
|
---|
74 | y = ymax;
|
---|
75 | }
|
---|
76 | else if ((outcodeOut & OUT_BOTTOM) > 0 ) {
|
---|
77 | x = x1 + (x2 - x1) * (ymin - y1)/(y2 - y1);
|
---|
78 | y = ymin;
|
---|
79 | }
|
---|
80 | else if ((outcodeOut & OUT_RIGHT)> 0) {
|
---|
81 | y = y1 + (y2 - y1) * (xmax - x1)/(x2 - x1);
|
---|
82 | x = xmax;
|
---|
83 | }
|
---|
84 | else if ((outcodeOut & OUT_LEFT) > 0) {
|
---|
85 | y = y1 + (y2 - y1) * (xmin - x1)/(x2 - x1);
|
---|
86 | x = xmin;
|
---|
87 | }
|
---|
88 | if (outcodeOut == outcode0) {
|
---|
89 | x1 = x;
|
---|
90 | y1 = y;
|
---|
91 | outcode0 = computeOutCode(x1, y1, xmin, ymin, xmax, ymax);
|
---|
92 | } else {
|
---|
93 | x2 = x;
|
---|
94 | y2 = y;
|
---|
95 | outcode1 = computeOutCode(x2, y2, xmin, ymin, xmax, ymax);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | while (!done);
|
---|
100 |
|
---|
101 | if(accept) {
|
---|
102 | p1 = new Point((int) x1, (int) y1);
|
---|
103 | p2 = new Point((int) x2, (int) y2);
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 | return false;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * The outcode of the point.
|
---|
111 | * We cannot use Rectangle.outcode since it does not work with long ints.
|
---|
112 | */
|
---|
113 | private static int computeOutCode (long x, long y, long xmin, long ymin, long xmax, long ymax) {
|
---|
114 | int code = 0;
|
---|
115 | if (y > ymax) {
|
---|
116 | code |= OUT_TOP;
|
---|
117 | }
|
---|
118 | else if (y < ymin) {
|
---|
119 | code |= OUT_BOTTOM;
|
---|
120 | }
|
---|
121 | if (x > xmax) {
|
---|
122 | code |= OUT_RIGHT;
|
---|
123 | }
|
---|
124 | else if (x < xmin) {
|
---|
125 | code |= OUT_LEFT;
|
---|
126 | }
|
---|
127 | return code;
|
---|
128 | }
|
---|
129 | }
|
---|