Class Quadrant

  1  
  2  
  3  
  4 /*
  5  * Copyright (c) 2016 Vivid Solutions.
  6  *
  7  * All rights reserved. This program and the accompanying materials
  8  * are made available under the terms of the Eclipse Public License 2.0
  9  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 10  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 11  * and the Eclipse Distribution License is available at
 12  *
 13  * http://www.eclipse.org/org/documents/edl-v10.php.
 14  */
 15 package org.locationtech.jts.geomgraph;
 16  
 17 /**
 18  * @version 1.7
 19  */
 20 import org.locationtech.jts.geom.Coordinate;
 21  
 22 /**
 23  * Utility functions for working with quadrants, which are numbered as follows:
 24  * <pre>
 25  * 1 | 0
 26  * --+--
 27  * 2 | 3
 28  * </pre>
 29  *
 30  * @version 1.7
 31  */
 32 public class Quadrant 
 33 {
 34     public static final int NE = 0;
 35     public static final int NW = 1;
 36     public static final int SW = 2;
 37     public static final int SE = 3;
 38     
 39   /**
 40    * Returns the quadrant of a directed line segment (specified as x and y
 41    * displacements, which cannot both be 0).
 42    * 
 43    * @throws IllegalArgumentException if the displacements are both 0
 44    */
 45   public static int quadrant(double dx, double dy)
 46   {
 47     if (dx == 0.0 && dy == 0.0)
 48       throw new IllegalArgumentException("Cannot compute the quadrant for point ( "+ dx + ", " + dy + " )" );
 49     if (dx >= 0.0) {
 50       if (dy >= 0.0)
 51         return NE;
 52       else
 53         return SE;
 54     }
 55     else {
 56         if (dy >= 0.0)
 57             return NW;
 58         else
 59             return SW;
 60     }
 61   }
 62  
 63   /**
 64    * Returns the quadrant of a directed line segment from p0 to p1.
 65    * 
 66    * @throws IllegalArgumentException if the points are equal
 67    */
 68   public static int quadrant(Coordinate p0, Coordinate p1)
 69   {
 70     if (p1.x == p0.x && p1.y == p0.y)
 71       throw new IllegalArgumentException("Cannot compute the quadrant for two identical points " + p0);
 72     
 73     if (p1.x >= p0.x) {
 74       if (p1.y >= p0.y)
 75         return NE;
 76       else
 77         return SE;
 78     }
 79     else {
 80         if (p1.y >= p0.y)
 81             return NW;
 82         else
 83             return SW;
 84     }
 85   }
 86  
 87   /**
 88    * Returns true if the quadrants are 1 and 3, or 2 and 4
 89    */
 90   public static boolean isOpposite(int quad1, int quad2)
 91   {
 92     if (quad1 == quad2) return false;
 93     int diff = (quad1 - quad2 + 4) % 4;
 94     // if quadrants are not adjacent, they are opposite
 95     if (diff == 2return true;
 96     return false;
 97   }
 98  
 99   /** 
100    * Returns the right-hand quadrant of the halfplane defined by the two quadrants,
101    * or -1 if the quadrants are opposite, or the quadrant if they are identical.
102    */
103   public static int commonHalfPlane(int quad1, int quad2)
104   {
105     // if quadrants are the same they do not determine a unique common halfplane.
106     // Simply return one of the two possibilities
107     if (quad1 == quad2) return quad1;
108     int diff = (quad1 - quad2 + 4) % 4;
109     // if quadrants are not adjacent, they do not share a common halfplane
110     if (diff == 2return -1;
111     //
112     int min = (quad1 < quad2) ? quad1 : quad2;
113     int max = (quad1 > quad2) ? quad1 : quad2;
114     // for this one case, the righthand plane is NOT the minimum index;
115     if (min == 0 && max == 3return 3;
116     // in general, the halfplane index is the minimum of the two adjacent quadrants
117     return min;
118   }
119  
120   /**
121    * Returns whether the given quadrant lies within the given halfplane (specified
122    * by its right-hand quadrant).
123    */
124   public static boolean isInHalfPlane(int quad, int halfPlane)
125   {
126     if (halfPlane == SE) {
127       return quad == SE || quad == SW;
128     }
129     return quad == halfPlane || quad == halfPlane + 1;
130   }
131     
132   /**
133    * Returns true if the given quadrant is 0 or 1.
134    */
135   public static boolean isNorthern(int quad)
136   {
137     return quad == NE || quad == NW;
138   }
139 }
140