~ubuntu-branches/ubuntu/natty/jts/natty

« back to all changes in this revision

Viewing changes to src/com/vividsolutions/jts/geomgraph/Quadrant.java

  • Committer: Bazaar Package Importer
  • Author(s): Wolfgang Baer
  • Date: 2005-08-07 14:12:35 UTC
  • Revision ID: james.westby@ubuntu.com-20050807141235-7hy3ll3xpq79djcb
Tags: upstream-1.6
ImportĀ upstreamĀ versionĀ 1.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
 
 
4
/*
 
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.
 
8
 *
 
9
 * Copyright (C) 2001 Vivid Solutions
 
10
 *
 
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.
 
15
 *
 
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.
 
20
 *
 
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
 
24
 *
 
25
 * For more information, contact:
 
26
 *
 
27
 *     Vivid Solutions
 
28
 *     Suite #1A
 
29
 *     2328 Government Street
 
30
 *     Victoria BC  V8T 5G5
 
31
 *     Canada
 
32
 *
 
33
 *     (250)385-6040
 
34
 *     www.vividsolutions.com
 
35
 */
 
36
package com.vividsolutions.jts.geomgraph;
 
37
 
 
38
/**
 
39
 * @version 1.6
 
40
 */
 
41
import com.vividsolutions.jts.geom.Coordinate;
 
42
 
 
43
/**
 
44
 * Utility functions for working with quadrants, which are numbered as follows:
 
45
 * <pre>
 
46
 * 1 | 0
 
47
 * --+--
 
48
 * 2 | 3
 
49
 * <pre>
 
50
 *
 
51
 * @version 1.6
 
52
 */
 
53
public class Quadrant {
 
54
  /**
 
55
   * Returns the quadrant of a directed line segment (specified as x and y
 
56
   * displacements, which cannot both be 0).
 
57
   */
 
58
  public static int quadrant(double dx, double dy)
 
59
  {
 
60
    if (dx == 0.0 && dy == 0.0)
 
61
      throw new IllegalArgumentException("Cannot compute the quadrant for point ( "+ dx + ", " + dy + " )" );
 
62
    if (dx >= 0) {
 
63
      if (dy >= 0)
 
64
        return 0;
 
65
      else
 
66
        return 3;
 
67
    }
 
68
    else {
 
69
        if (dy >= 0)
 
70
          return 1;
 
71
        else
 
72
          return 2;
 
73
    }
 
74
  }
 
75
 
 
76
  /**
 
77
   * Returns the quadrant of a directed line segment from p0 to p1.
 
78
   */
 
79
  public static int quadrant(Coordinate p0, Coordinate p1)
 
80
  {
 
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);
 
86
  }
 
87
 
 
88
  /**
 
89
   * Returns true if the quadrants are 1 and 3, or 2 and 4
 
90
   */
 
91
  public static boolean isOpposite(int quad1, int quad2)
 
92
  {
 
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;
 
97
    return false;
 
98
  }
 
99
 
 
100
  /** 
 
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.
 
103
   */
 
104
  public static int commonHalfPlane(int quad1, int quad2)
 
105
  {
 
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;
 
112
    //
 
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
 
118
    return min;
 
119
  }
 
120
 
 
121
  /**
 
122
   * Returns whether the given quadrant lies within the given halfplane (specified
 
123
   * by its right-hand quadrant).
 
124
   */
 
125
  public static boolean isInHalfPlane(int quad, int halfPlane)
 
126
  {
 
127
    if (halfPlane == 3) {
 
128
      return quad == 3 || quad == 0;
 
129
    }
 
130
    return quad == halfPlane || quad == halfPlane + 1;
 
131
  }
 
132
    
 
133
  /**
 
134
   * Returns true if the given quadrant is 0 or 1.
 
135
   */
 
136
  public static boolean isNorthern(int quad)
 
137
  {
 
138
    return quad == 0 || quad == 1;
 
139
  }
 
140
}