5
* The JTS Topology Suite is a collection of Java classes that
6
* implement the fundamental operations required to validate a given
7
* geo-spatial data set to a known topological specification.
9
* Copyright (C) 2001 Vivid Solutions
11
* This library is free software; you can redistribute it and/or
12
* modify it under the terms of the GNU Lesser General Public
13
* License as published by the Free Software Foundation; either
14
* version 2.1 of the License, or (at your option) any later version.
16
* This library is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
* Lesser General Public License for more details.
21
* You should have received a copy of the GNU Lesser General Public
22
* License along with this library; if not, write to the Free Software
23
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
* For more information, contact:
29
* 2328 Government Street
34
* www.vividsolutions.com
36
package com.vividsolutions.jts.geomgraph;
41
import com.vividsolutions.jts.geom.Coordinate;
44
* Utility functions for working with quadrants, which are numbered as follows:
53
public class Quadrant {
55
* Returns the quadrant of a directed line segment (specified as x and y
56
* displacements, which cannot both be 0).
58
public static int quadrant(double dx, double dy)
60
if (dx == 0.0 && dy == 0.0)
61
throw new IllegalArgumentException("Cannot compute the quadrant for point ( "+ dx + ", " + dy + " )" );
77
* Returns the quadrant of a directed line segment from p0 to p1.
79
public static int quadrant(Coordinate p0, Coordinate p1)
81
double dx = p1.x - p0.x;
82
double dy = p1.y - p0.y;
83
if (dx == 0.0 && dy == 0.0)
84
throw new IllegalArgumentException("Cannot compute the quadrant for two identical points " + p0);
85
return quadrant(dx, dy);
89
* Returns true if the quadrants are 1 and 3, or 2 and 4
91
public static boolean isOpposite(int quad1, int quad2)
93
if (quad1 == quad2) return false;
94
int diff = (quad1 - quad2 + 4) % 4;
95
// if quadrants are not adjacent, they are opposite
96
if (diff == 2) return true;
101
* Returns the right-hand quadrant of the halfplane defined by the two quadrants,
102
* or -1 if the quadrants are opposite, or the quadrant if they are identical.
104
public static int commonHalfPlane(int quad1, int quad2)
106
// if quadrants are the same they do not determine a unique common halfplane.
107
// Simply return one of the two possibilities
108
if (quad1 == quad2) return quad1;
109
int diff = (quad1 - quad2 + 4) % 4;
110
// if quadrants are not adjacent, they do not share a common halfplane
111
if (diff == 2) return -1;
113
int min = (quad1 < quad2) ? quad1 : quad2;
114
int max = (quad1 > quad2) ? quad1 : quad2;
115
// for this one case, the righthand plane is NOT the minimum index;
116
if (min == 0 && max == 3) return 3;
117
// in general, the halfplane index is the minimum of the two adjacent quadrants
122
* Returns whether the given quadrant lies within the given halfplane (specified
123
* by its right-hand quadrant).
125
public static boolean isInHalfPlane(int quad, int halfPlane)
127
if (halfPlane == 3) {
128
return quad == 3 || quad == 0;
130
return quad == halfPlane || quad == halfPlane + 1;
134
* Returns true if the given quadrant is 0 or 1.
136
public static boolean isNorthern(int quad)
138
return quad == 0 || quad == 1;