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

« back to all changes in this revision

Viewing changes to src/com/vividsolutions/jts/algorithm/InteriorPointPoint.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
 * The JTS Topology Suite is a collection of Java classes that
 
4
 * implement the fundamental operations required to validate a given
 
5
 * geo-spatial data set to a known topological specification.
 
6
 *
 
7
 * Copyright (C) 2001 Vivid Solutions
 
8
 *
 
9
 * This library is free software; you can redistribute it and/or
 
10
 * modify it under the terms of the GNU Lesser General Public
 
11
 * License as published by the Free Software Foundation; either
 
12
 * version 2.1 of the License, or (at your option) any later version.
 
13
 *
 
14
 * This library is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
 * Lesser General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU Lesser General Public
 
20
 * License along with this library; if not, write to the Free Software
 
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
22
 *
 
23
 * For more information, contact:
 
24
 *
 
25
 *     Vivid Solutions
 
26
 *     Suite #1A
 
27
 *     2328 Government Street
 
28
 *     Victoria BC  V8T 5G5
 
29
 *     Canada
 
30
 *
 
31
 *     (250)385-6040
 
32
 *     www.vividsolutions.com
 
33
 */
 
34
package com.vividsolutions.jts.algorithm;
 
35
 
 
36
import com.vividsolutions.jts.geom.*;
 
37
 
 
38
/**
 
39
 * Computes a point in the interior of an point geometry.
 
40
 * <h2>Algorithm</h2>
 
41
 * Find a point which is closest to the centroid of the geometry.
 
42
 *
 
43
 * @version 1.6
 
44
 */
 
45
public class InteriorPointPoint {
 
46
 
 
47
  private Coordinate centroid;
 
48
  private double minDistance = Double.MAX_VALUE;
 
49
 
 
50
  private Coordinate interiorPoint = null;
 
51
 
 
52
  public InteriorPointPoint(Geometry g)
 
53
  {
 
54
    centroid = g.getCentroid().getCoordinate();
 
55
    add(g);
 
56
  }
 
57
 
 
58
  /**
 
59
   * Tests the point(s) defined by a Geometry for the best inside point.
 
60
   * If a Geometry is not of dimension 0 it is not tested.
 
61
   * @param geom the geometry to add
 
62
   */
 
63
  private void add(Geometry geom)
 
64
  {
 
65
    if (geom instanceof Point) {
 
66
      add(geom.getCoordinate());
 
67
    }
 
68
    else if (geom instanceof GeometryCollection) {
 
69
      GeometryCollection gc = (GeometryCollection) geom;
 
70
      for (int i = 0; i < gc.getNumGeometries(); i++) {
 
71
        add(gc.getGeometryN(i));
 
72
      }
 
73
    }
 
74
  }
 
75
  private void add(Coordinate point)
 
76
  {
 
77
    double dist = point.distance(centroid);
 
78
    if (dist < minDistance) {
 
79
      interiorPoint = new Coordinate(point);
 
80
      minDistance = dist;
 
81
    }
 
82
  }
 
83
 
 
84
  public Coordinate getInteriorPoint()
 
85
  {
 
86
    return interiorPoint;
 
87
  }
 
88
}