Complete Note#1 in the http://wiki.osgeo.org/wiki/GEOS_Provenance_Review to get out...
[geos.git] / src / geomgraph / Quadrant.cpp
blob301ad45f58a7ecd3386de0cf8223ecf3907b9b36
1 /**********************************************************************
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
6 * Copyright (C) 2001-2002 Vivid Solutions Inc.
8 * This is free software; you can redistribute and/or modify it under
9 * the terms of the GNU Lesser General Public Licence as published
10 * by the Free Software Foundation.
11 * See the COPYING file for more information.
13 **********************************************************************
15 * Last port: geomgraph/Quadrant.java rev. 1.8 (JTS-1.10)
17 **********************************************************************/
19 #include <sstream>
21 #include <geos/geomgraph/Quadrant.h>
22 #include <geos/util/IllegalArgumentException.h>
24 #include <geos/geom/Coordinate.h>
26 using namespace std;
27 using namespace geos::geom;
29 namespace geos {
30 namespace geomgraph { // geos.geomgraph
32 /* public static */
33 int
34 Quadrant::quadrant(double dx, double dy)
36 if (dx == 0.0 && dy == 0.0) {
37 ostringstream s;
38 s<<"Cannot compute the quadrant for point ";
39 s<<"("<<dx<<","<<dy<<")"<<endl;
40 throw util::IllegalArgumentException(s.str());
42 if (dx >= 0) {
43 if (dy >= 0)
44 return NE;
45 else
46 return SE;
47 } else {
48 if (dy >= 0)
49 return NW;
50 else
51 return SW;
55 /* public static */
56 int
57 Quadrant::quadrant(const Coordinate& p0, const Coordinate& p1)
59 if (p1.x == p0.x && p1.y == p0.y)
61 throw util::IllegalArgumentException("Cannot compute the quadrant for two identical points " + p0.toString());
64 if (p1.x >= p0.x) {
65 if (p1.y >= p0.y)
66 return NE;
67 else
68 return SE;
70 else {
71 if (p1.y >= p0.y)
72 return NW;
73 else
74 return SW;
78 /* public static */
79 bool
80 Quadrant::isOpposite(int quad1, int quad2)
82 if (quad1==quad2) return false;
83 int diff=(quad1-quad2+4)%4;
84 // if quadrants are not adjacent, they are opposite
85 if (diff==2) return true;
86 return false;
89 /* public static */
90 int
91 Quadrant::commonHalfPlane(int quad1, int quad2)
93 // if quadrants are the same they do not determine a unique
94 // common halfplane.
95 // Simply return one of the two possibilities
96 if (quad1==quad2) return quad1;
97 int diff=(quad1-quad2+4)%4;
98 // if quadrants are not adjacent, they do not share a common halfplane
99 if (diff==2) return -1;
101 int min=(quad1<quad2)? quad1:quad2;
102 int max=(quad1>quad2)? quad1:quad2;
103 // for this one case, the righthand plane is NOT the minimum index;
104 if (min==0 && max==3) return 3;
105 // in general, the halfplane index is the minimum of the two
106 // adjacent quadrants
107 return min;
110 /* public static */
111 bool
112 Quadrant::isInHalfPlane(int quad, int halfPlane)
114 if (halfPlane==SE) {
115 return quad==SE || quad==SW;
117 return quad==halfPlane || quad==halfPlane+1;
120 /* public static */
121 bool
122 Quadrant::isNorthern(int quad)
124 return quad==NE || quad==NW;
127 } // namespace geos.geomgraph
128 } // namespace geos
130 /**********************************************************************
131 * $Log$
132 * Revision 1.10 2006/03/15 17:16:29 strk
133 * streamlined headers inclusion
135 * Revision 1.9 2006/03/09 16:46:47 strk
136 * geos::geom namespace definition, first pass at headers split
138 * Revision 1.8 2006/03/06 19:40:46 strk
139 * geos::util namespace. New GeometryCollection::iterator interface, many cleanups.
141 * Revision 1.7 2006/03/03 10:46:21 strk
142 * Removed 'using namespace' from headers, added missing headers in .cpp files, removed useless includes in headers (bug#46)
144 * Revision 1.6 2006/02/19 19:46:49 strk
145 * Packages <-> namespaces mapping for most GEOS internal code (uncomplete, but working). Dir-level libs for index/ subdirs.
147 * Revision 1.5 2006/02/09 15:52:47 strk
148 * GEOSException derived from std::exception; always thrown and cought by const ref.
150 * Revision 1.4 2005/02/05 05:44:47 strk
151 * Changed geomgraph nodeMap to use Coordinate pointers as keys, reduces
152 * lots of other Coordinate copies.
154 * Revision 1.3 2005/01/28 09:47:51 strk
155 * Replaced sprintf uses with ostringstream.
157 * Revision 1.2 2004/07/02 13:28:26 strk
158 * Fixed all #include lines to reflect headers layout change.
159 * Added client application build tips in README.
161 * Revision 1.1 2004/03/19 09:48:45 ybychkov
162 * "geomgraph" and "geomgraph/indexl" upgraded to JTS 1.4
164 * Revision 1.10 2003/11/07 01:23:42 pramsey
165 * Add standard CVS headers licence notices and copyrights to all cpp and h
166 * files.
169 **********************************************************************/