Quotes around otherwise ambiguous (underline containing) name
[geos.git] / src / geom / util / SineStarFactory.cpp
blob40641d518a7957abae6971804e2d379126ff5b61
1 /**********************************************************************
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
6 * Copyright (C) 2011 Sandro Santilli <strk@kbt.io
8 * This is free software; you can redistribute and/or modify it under
9 * the terms of the GNU Lesser General Public Licence as published
10 * by the Free Software Foundation.
11 * See the COPYING file for more information.
13 **********************************************************************
15 * Last port: geom/util/SineStarFactory.java r378 (JTS-1.12)
17 **********************************************************************/
19 #include <geos/geom/util/SineStarFactory.h>
20 #include <geos/geom/Coordinate.h>
21 #include <geos/geom/CoordinateSequenceFactory.h>
22 #include <geos/geom/CoordinateSequence.h>
23 #include <geos/geom/GeometryFactory.h>
24 #include <geos/geom/Envelope.h>
25 #include <geos/geom/Polygon.h>
26 #include <geos/geom/LinearRing.h>
28 #include <vector>
29 #include <cmath>
30 #include <memory>
32 #ifndef M_PI
33 #define M_PI 3.14159265358979323846
34 #endif
36 using namespace std;
37 //using namespace geos::geom;
39 namespace geos {
40 namespace geom { // geos::geom
41 namespace util { // geos::geom::util
43 /* public */
44 auto_ptr<Polygon>
45 SineStarFactory::createSineStar() const
47 auto_ptr<Envelope> env ( dim.getEnvelope() );
48 double radius = env->getWidth() / 2.0;
50 double armRatio = armLengthRatio;
51 if (armRatio < 0.0) armRatio = 0.0;
52 if (armRatio > 1.0) armRatio = 1.0;
54 double armMaxLen = armRatio * radius;
55 double insideRadius = (1 - armRatio) * radius;
57 double centreX = env->getMinX() + radius;
58 double centreY = env->getMinY() + radius;
60 auto_ptr< vector<Coordinate> > pts ( new vector<Coordinate>(nPts+1) );
61 int iPt = 0;
62 for (int i = 0; i < nPts; i++) {
63 // the fraction of the way thru the current arm - in [0,1]
64 double ptArcFrac = (i / (double) nPts) * numArms;
65 double armAngFrac = ptArcFrac - floor(ptArcFrac);
67 // the angle for the current arm - in [0,2Pi]
68 // (each arm is a complete sine wave cycle)
69 double armAng = 2 * M_PI * armAngFrac;
70 // the current length of the arm
71 double armLenFrac = (cos(armAng) + 1.0) / 2.0;
73 // the current radius of the curve (core + arm)
74 double curveRadius = insideRadius + armMaxLen * armLenFrac;
76 // the current angle of the curve
77 double ang = i * (2 * M_PI / nPts);
78 double x = curveRadius * cos(ang) + centreX;
79 double y = curveRadius * sin(ang) + centreY;
80 (*pts)[iPt++] = coord(x, y);
82 (*pts)[iPt] = Coordinate((*pts)[0]);
84 auto_ptr<CoordinateSequence> cs (
85 geomFact->getCoordinateSequenceFactory()->create( pts.release() )
87 auto_ptr<LinearRing> ring ( geomFact->createLinearRing( cs.release() ) );
88 auto_ptr<Polygon> poly ( geomFact->createPolygon(ring.release(), 0) );
89 return poly;
92 } // namespace geos::geom::util
93 } // namespace geos::geom
94 } // namespace geos