-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.cpp
220 lines (203 loc) · 5.34 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* utils.cpp
* 4/4/2016
*
* CS 372 Sp16
* Group Project 2: Postscript Generator
*/
#include "utils.h"
/**
* @brief generates ps code for ps file header
* @details returns a string for ps file headers
* @return string "%!\n"
*/
string psBegin()
{
return "%!\n";
}
/**
* @brief generates ps code for printing a page
* @details returns a string containing the ps code for printing a page
* @return string "showpage"
*/
string psPageBreak()
{
return "showpage";
}
/**
* @brief generates ps code for drawing a line
* @details returns a string containing the ps code for drawing a line, in the form "x y lineto\n"
*
* @param x x coordinate of endpoint
* @param y y coordinate of endpoint
* @return string with ps code
*/
string psLine(int x, int y){
return to_string(x) + " " + to_string(y) + " lineto\n";
}
/**
* @brief generates ps code for moving the cursor
* @details returns a string containing the ps code for moving the ps cursor, in the form "x y moveto\n"
*
* @param x x coordinate of target point
* @param y y coordinate of target point
*
* @return string with ps code
*/
string psMove(int x, int y){
return to_string(x) + " " + to_string(y) + " moveto\n";
}
/**
* @brief generates ps code for drawing an arc
* @details returns a string containing the ps code for drawing an arc, in the form 'x y r angle1 angle2 arc'
*
* @param x c position of the center
* @param y y position of the center
* @param r radius of the arc
* @param startAngle start angle for the arc, from 0 to 360 degrees
* @param endAngle end angle for the arc, from 0 to 360 degrees
* @return string containing ps code
*/
string psArc(int x, int y, double r, int startAngle, int endAngle){
return to_string(x) + " " + to_string(y) + " " + to_string(r) + " " + to_string(startAngle) + " " + to_string(endAngle) + " arc\n";
}
/**
* @brief generates ps code for header
* @details returns a string containing the ps code for a header for draw functions in this library
*
* @param x x position of the center of the shape
* @param y y position of the center of the shape
*
* @return string containing ps code
*/
string psHeader(int x, int y){
return "gsave\nnewpath\n" + to_string(x) + " " + to_string(y) + " translate\n";
}
/**
* @brief generates ps code for footer
* @details returns a string containing the ps code for a footer for draw functions in this library
* @return string containing ps code
*/
string psFooter(){
return "closepath\nstroke\ngrestore\n";
}
/**
* @brief calculate x
* @details calculate the x coordinate of a given vertex of an equilateral polygon
*
* @param k vertex number
* @param n number of sides
* @param l length of sides
* @return double, x coordinate of vertex
*/
double calcX(int k, int n, double l){
return (l/2)*( sin( (2*k+1)*M_PI/n ) / sin(M_PI/n) );
}
/**
* @brief calculate y
* @details calcualte the y coordinate of a given vertex of an equilateral polygon
*
* @param k vertex number
* @param n number of sides
* @param l length of sides
* @return double, y coordinate of vertex
*/
double calcY(int k, int n, double l){
return (-l/2)*( cos( (2*k+1)*M_PI/n ) / sin(M_PI/n) );
}
/**
* @brief cacluate width of polygon
* @details calculate the width of any equlateral polygon
*
* @param n number of sides
* @param l length of sides
*
* @return width of polygon
*/
double getWidth(int n, double l){
if(n%2 == 0){
if(n%4 == 0){
return (l*cos(M_PI/n)/sin(M_PI/n));
} else {
return (l / sin(M_PI/n));
}
} else {
return ( l * ( sin( M_PI * (n-1) / (2*n) ) ) / sin(M_PI/n) );
}
}
/**
* @brief cacluate height of polygon
* @details calculate the height of any equlateral polygon
*
* @param n number of sides
* @param l length of sides
*
* @return height of polygon
*/
double getHeight(int n, double l){
if(n%2 == 0){
return (l * (cos(M_PI/n)) / (sin(M_PI/n)));
} else {
return (l * (1+cos(M_PI/n)) / (2*sin(M_PI/n)));
}
}
/**
* @brief calculate radius
* @details calculate radius of an equlateral polygon
*
* @param n number of sides
* @param l length of sides
*
* @return double, radius
*/
double getRadius(int n, double l){
return l/(2*sin(M_PI/n));
}
/**
* @brief get x
* @details calculate x coordinate of a convex vertex of a star
*
* @param k vertex number, 0 - (n-1)
* @param n number of verticies
* @param r radius out outer circle
* @return double, x coordinate
*/
double getConvexX(int k, int n, double r){
return r * cos(2 * M_PI * k / n);
}
/**
* @brief get y
* @details calculate y coordinate of a convex vertex of a star
*
* @param k vertex number, 0 - (n-1)
* @param n number of verticies
* @param r radius out outer circle
* @return double, y coordinate
*/
double getConvexY(int k, int n, double r){
return r * sin(2 * M_PI * k / n);
}
/**
* @brief get x
* @details calculate x coordinate of a concave vertex of a star
*
* @param k vertex number, 0 - (n-1)
* @param n number of verticies
* @param r radius of inner circle
* @return double, x coordinate
*/
double getConcaveX(int k, int n, double r){
return r * cos( (2 * M_PI * k + M_PI) / n);
}
/**
* @brief get y
* @details calculate y coordinate of a concave vertex of a star
*
* @param k vertex number, 0 - (n-1)
* @param n number of verticies
* @param r radius of inner circle
* @return double, y coordinate
*/
double getConcaveY(int k, int n, double r){
return r * sin( (2 * M_PI * k + M_PI) / n);
}