1 // primShape.cpp
2 // Context Free
3 // ---------------------
4 // Copyright (C) 2006-2011 John Horigan
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 //
20 // John Horigan can be contacted at john@glyphic.com or at
21 // John Horigan, 1209 Villa St., Mountain View, CA 94041-1123, USA
22 //
23 //
24 
25 #include "primShape.h"
26 #include <cmath>
27 #include "agg2/agg_basics.h"
28 
29 using namespace agg;
30 
31 #ifndef M_PI
32 #define M_PI        3.14159265358979323846
33 #endif
34 
35 static const double h = 0.5 / cos(M_PI/6.0);
36 static const double hp = h;
37 static const double hn = -h / 2.0;
38 static const double t = sqrt(2.0) / 4.0;
39 
40 const primShape::primShapes_t primShape::shapeMap = { {
41     {   // circle pseudo-vertices
42         { 0.5,  0.0 },
43         {   t,    t },
44         { 0.0,  0.5 },
45         {  -t,    t },
46         {-0.5,  0.0 },
47         {  -t,   -t },
48         { 0.0, -0.5 },
49         {   t,   -t }
50     },
51     {   // square vertices
52         {  0.5,  0.5 },
53         { -0.5,  0.5 },
54         { -0.5, -0.5 },
55         {  0.5, -0.5 }
56     },
57     {   // triangle vertices
58         { 0.0, hp },
59         {-0.5, hn },
60         { 0.5, hn }
61     }
62 } };
63 
64 const primShape::primNames_t primShape::shapeNames = {
65     { "CIRCLE", "SQUARE", "TRIANGLE", "FILL" }
66 };
67 
68 unsigned
vertex(double * x,double * y)69 primIter::vertex(double* x, double* y)
70 {
71     if (mData && mIndex < mData->total_vertices()) {
72         return mData->vertex(mIndex++, x, y);
73     } else {
74         *x = *y = 0.0;
75         return agg::path_cmd_stop;
76     }
77 }
78 
79 void
rewind(unsigned i)80 primIter::rewind(unsigned i)
81 {
82     mIndex = i;
83 }
84 
85 void
init(const primShape * shape)86 primIter::init(const primShape* shape)
87 {
88     mData = shape;
89     mIndex = 0;
90 }
91