@@ -67,67 +67,60 @@ else if (y > yMax)
6767 /**
6868 * Clips a given line against the clip rectangle.
6969 * The modification (if needed) is done in place.
70- * @param line the line to clip
70+ * @param line the line to clip.
7171 * @return true if line is clipped, false if line is
7272 * totally outside the clip rect.
7373 */
7474 public boolean clip (Line2D .Float line ) {
75+ double point1X = line .getX1 (), point1Y = line .getY1 ();
76+ double point2X = line .getX2 (), point2Y = line .getY2 ();
77+ double outsidePointX = 0d , outsidePointY = 0d ;
7578
76- double p1x = line .getX1 ();
77- double p1y = line .getY1 ();
78- double p2x = line .getX2 ();
79- double p2y = line .getY2 ();
79+ boolean lineIsVertical = (point1X == point2X );
80+ double lineSlope = lineIsVertical ? 0d : (point2Y -point1Y )/(point2X -point1X );
8081
81- double qx = 0d ;
82- double qy = 0d ;
82+ int point1Region = regionCode ( point1X , point1Y ) ;
83+ int point2Region = regionCode ( point2X , point2Y ) ;
8384
84- boolean lineIsVertical = (p1x == p2x );
85-
86- double lineSlope = lineIsVertical ? 0d : (p2y -p1y )/(p2x -p1x );
87-
88- int p1Region = regionCode (p1x , p1y );
89- int p2Region = regionCode (p2x , p2y );
90-
91- while (p1Region != INSIDE || p2Region != INSIDE ) {
92-
93- if ((p1Region & p2Region ) != INSIDE )
85+ while (point1Region != INSIDE || point2Region != INSIDE ) {
86+ if ((point1Region & point2Region ) != INSIDE )
9487 return false ;
9588
96- int c = (p1Region == INSIDE ) ? p2Region : p1Region ;
89+ int outsidePointRegion = (point1Region == INSIDE ) ? point2Region : point1Region ;
9790
98- if ((c & LEFT ) != INSIDE ) {
99- qx = xMin ;
100- qy = (Utils .feq (qx , p1x ) ? 0 : qx - p1x )*lineSlope + p1y ;
91+ if ((outsidePointRegion & LEFT ) != INSIDE ) {
92+ outsidePointX = xMin ;
93+ outsidePointY = (Utils .feq (outsidePointX , point1X ) ? 0 : outsidePointX - point1X )*lineSlope + point1Y ;
10194 }
102- else if ((c & RIGHT ) != INSIDE ) {
103- qx = xMax ;
104- qy = (Utils .feq (qx , p1x ) ? 0 : qx - p1x )*lineSlope + p1y ;
95+ else if ((outsidePointRegion & RIGHT ) != INSIDE ) {
96+ outsidePointX = xMax ;
97+ outsidePointY = (Utils .feq (outsidePointX , point1X ) ? 0 : outsidePointX - point1X )*lineSlope + point1Y ;
10598 }
106- else if ((c & BOTTOM ) != INSIDE ) {
107- qy = yMin ;
108- qx = lineIsVertical
109- ? p1x
110- : (Utils .feq (qy , p1y ) ? 0 : qy - p1y )/lineSlope + p1x ;
99+ else if ((outsidePointRegion & BOTTOM ) != INSIDE ) {
100+ outsidePointY = yMin ;
101+ outsidePointX = lineIsVertical
102+ ? point1X
103+ : (Utils .feq (outsidePointY , point1Y ) ? 0 : outsidePointY - point1Y )/lineSlope + point1X ;
111104 }
112- else if ((c & TOP ) != INSIDE ) {
113- qy = yMax ;
114- qx = lineIsVertical
115- ? p1x
116- : (Utils .feq (qy , p1y ) ? 0 : qy - p1y )/lineSlope + p1x ;
105+ else if ((outsidePointRegion & TOP ) != INSIDE ) {
106+ outsidePointY = yMax ;
107+ outsidePointX = lineIsVertical
108+ ? point1X
109+ : (Utils .feq (outsidePointY , point1Y ) ? 0 : outsidePointY - point1Y )/lineSlope + point1X ;
117110 }
118111
119- if (c == p1Region ) {
120- p1x = qx ;
121- p1y = qy ;
122- p1Region = regionCode (p1x , p1y );
112+ if (outsidePointRegion == point1Region ) {
113+ point1X = outsidePointX ;
114+ point1Y = outsidePointY ;
115+ point1Region = regionCode (point1X , point1Y );
123116 }
124117 else {
125- p2x = qx ;
126- p2y = qy ;
127- p2Region = regionCode (p2x , p2y );
118+ point2X = outsidePointX ;
119+ point2Y = outsidePointY ;
120+ point2Region = regionCode (point2X , point2Y );
128121 }
129122 }
130- line .setLine (p1x , p1y , p2x , p2y );
123+ line .setLine (point1X , point1Y , point2X , point2Y );
131124 return true ;
132125 }
133126
0 commit comments