Add a 'flags' parameter to GEOSisValidDetail.
[geos.git] / src / algorithm / CentroidLine.cpp
blob6879e357fc9667fd8d4aa43e9f47a9830e36dc55
1 /**********************************************************************
2 * $Id$
4 * GEOS - Geometry Engine Open Source
5 * http://geos.refractions.net
7 * Copyright (C) 2001-2002 Vivid Solutions Inc.
8 * Copyright (C) 2005 Refractions Research Inc.
10 * This is free software; you can redistribute and/or modify it under
11 * the terms of the GNU Lesser General Public Licence as published
12 * by the Free Software Foundation.
13 * See the COPYING file for more information.
15 **********************************************************************
17 **********************************************************************/
19 #include <geos/algorithm/CentroidLine.h>
20 #include <geos/geom/Coordinate.h>
21 #include <geos/geom/CoordinateSequence.h>
22 #include <geos/geom/Geometry.h>
23 #include <geos/geom/GeometryCollection.h>
24 #include <geos/geom/LineString.h>
26 #include <typeinfo>
28 using namespace geos::geom;
30 namespace geos {
31 namespace algorithm { // geos.algorithm
33 /*public*/
34 void
35 CentroidLine::add(const Geometry *geom)
37 const LineString* ls = dynamic_cast<const LineString*>(geom);
38 if ( ls )
40 add(ls->getCoordinatesRO());
41 return;
44 const GeometryCollection* gc = dynamic_cast<const GeometryCollection*>(geom);
45 if (gc)
47 for(std::size_t i=0, n=gc->getNumGeometries(); i<n; i++) {
48 add(gc->getGeometryN(i));
53 /*public*/
54 void
55 CentroidLine::add(const CoordinateSequence *pts)
57 std::size_t const npts=pts->getSize();
59 for(std::size_t i=1; i<npts; ++i)
61 const Coordinate &p1=pts->getAt(i-1);
62 const Coordinate &p2=pts->getAt(i);
64 double segmentLen=p1.distance(p2);
65 totalLength+=segmentLen;
66 double midx=(p1.x+p2.x)/2;
67 centSum.x+=segmentLen*midx;
68 double midy=(p1.y+p2.y)/2;
69 centSum.y+=segmentLen*midy;
73 Coordinate *
74 CentroidLine::getCentroid() const
76 return new Coordinate(centSum.x/totalLength, centSum.y/totalLength);
79 bool
80 CentroidLine::getCentroid(Coordinate& c) const
82 if ( totalLength == 0.0 ) return false;
83 c=Coordinate(centSum.x/totalLength, centSum.y/totalLength);
84 return true;
87 } // namespace geos.algorithm
88 } // namespace geos
90 /**********************************************************************
91 * $Log$
92 * Revision 1.16 2006/03/21 11:12:23 strk
93 * Cleanups: headers inclusion and Log section
95 * Revision 1.15 2006/03/09 16:46:45 strk
96 * geos::geom namespace definition, first pass at headers split
98 **********************************************************************/