1 /**********************************************************************
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
6 * Copyright (C) 2001-2002 Vivid Solutions Inc.
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 **********************************************************************/
17 #include <geos/geom/GeometryFactory.h>
18 #include <geos/geom/CoordinateArraySequence.h>
19 #include <geos/geom/Coordinate.h>
22 #define PI 3.14159265358979
24 using namespace geos::geom
;
26 Polygon
* GeometryTestFactory::createBox(GeometryFactory
*fact
,double minx
,double miny
,int nSide
,double segLen
) {
27 CoordinateSequence
*pts
=createBox(minx
, miny
, nSide
, segLen
);
28 return fact
->createPolygon(fact
->createLinearRing(pts
),NULL
);
31 CoordinateSequence
* GeometryTestFactory::createBox(double minx
, double miny
,int nSide
,double segLen
) {
33 CoordinateSequence
*pts
=new CoordinateArraySequence();
34 double maxx
=minx
+nSide
*segLen
;
35 double maxy
=miny
+nSide
*segLen
;
37 for(i
=0;i
<nSide
;i
++) {
38 double x
=minx
+i
*segLen
;
40 pts
->add(Coordinate(x
,y
));
42 for(i
=0;i
<nSide
;i
++) {
44 double y
=miny
+i
*segLen
;
45 pts
->add(Coordinate(x
,y
));
47 for(i
=0;i
<nSide
;i
++) {
48 double x
=maxx
-i
*segLen
;
50 pts
->add(Coordinate(x
,y
));
52 for(i
=0;i
<nSide
;i
++) {
54 double y
=maxy
-i
*segLen
;
55 pts
->add(Coordinate(x
,y
));
57 pts
->add(pts
->getAt(0));
63 * @param x the centre x coord
64 * @param y the centre y coord
65 * @param size the size of the envelope of the star
66 * @param nPts the number of points in the star
68 CoordinateSequence
* GeometryTestFactory::createCircle(double basex
,double basey
,double size
,int nPts
) {
69 CoordinateSequence
*pts
=new CoordinateArraySequence(nPts
+1);
72 for(int i
=0;i
<nPts
;i
++) {
73 double ang
=i
*(2*PI
/nPts
);
74 double x
=len
*cos(ang
)+basex
;
75 double y
=len
*sin(ang
)+basey
;
76 pts
->add(Coordinate(x
,y
));
78 pts
->add(Coordinate(pts
->getAt(0)));
82 Polygon
* GeometryTestFactory::createCircle(GeometryFactory
*fact
,double basex
,double basey
,double size
,int nPts
) {
83 CoordinateSequence
*pts
=createCircle(basex
, basey
, size
, nPts
);
84 return fact
->createPolygon(fact
->createLinearRing(pts
),NULL
);
88 * Creates a star from a "circular" sine wave
89 * @param basex the centre x coord
90 * @param basey the centre y coord
91 * @param size the size of the envelope of the star
92 * @param armLen the length of an arm of the star
93 * @param nArms the number of arms of the star
94 * @param nPts the number of points in the star
96 CoordinateSequence
* GeometryTestFactory::createSineStar(double basex
,double basey
,double size
,double armLen
,int nArms
,int nPts
) {
97 double armBaseLen
=size
/2-armLen
;
98 if (armBaseLen
<0) armBaseLen
=0.5;
100 double angInc
=2*PI
/nArms
;
101 int nArmPt
=nPts
/nArms
;
102 if (nArmPt
<5) nArmPt
=5;
104 //int nPts2=nArmPt*nArms;
105 CoordinateSequence
*pts
=new CoordinateArraySequence();
109 for(int iArm
=0;iArm
<nArms
;iArm
++) {
110 for(int iArmPt
=0;iArmPt
<nArmPt
;iArmPt
++) {
111 double ang
=iArmPt
*(2*PI
/nArmPt
);
112 double len
=armLen
*(1-cos(ang
)/2)+armBaseLen
;
113 double x
=len
*cos(starAng
+iArmPt
*angInc
/nArmPt
)+basex
;
114 double y
=len
*sin(starAng
+iArmPt
*angInc
/nArmPt
)+basey
;
116 // FIXME - mloskot: Number of problems here:
117 // - new'd Coordinate definitely leaks
118 // - add() method makes a copy
119 // - why temporarily used Coordinate is allocated on the heap?!?
120 pts
->add(*(new Coordinate(x
,y
)));
124 // FIXME - mloskot: The same problems as above
125 pts
->add(*(new Coordinate(pts
->getAt(0))));
129 Polygon
* GeometryTestFactory::createSineStar(GeometryFactory
*fact
,double basex
,double basey
,double size
,double armLen
,int nArms
,int nPts
){
130 CoordinateSequence
*pts
=createSineStar(basex
, basey
, size
, armLen
, nArms
, nPts
);
131 return fact
->createPolygon(fact
->createLinearRing(pts
),NULL
);