1 /**********************************************************************
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>
28 using namespace geos::geom
;
31 namespace algorithm
{ // geos.algorithm
35 CentroidLine::add(const Geometry
*geom
)
37 const LineString
* ls
= dynamic_cast<const LineString
*>(geom
);
40 add(ls
->getCoordinatesRO());
44 const GeometryCollection
* gc
= dynamic_cast<const GeometryCollection
*>(geom
);
47 for(std::size_t i
=0, n
=gc
->getNumGeometries(); i
<n
; i
++) {
48 add(gc
->getGeometryN(i
));
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
;
74 CentroidLine::getCentroid() const
76 return new Coordinate(centSum
.x
/totalLength
, centSum
.y
/totalLength
);
80 CentroidLine::getCentroid(Coordinate
& c
) const
82 if ( totalLength
== 0.0 ) return false;
83 c
=Coordinate(centSum
.x
/totalLength
, centSum
.y
/totalLength
);
87 } // namespace geos.algorithm
90 /**********************************************************************
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 **********************************************************************/