-
Notifications
You must be signed in to change notification settings - Fork 7
/
11.2
69 lines (55 loc) · 2 KB
/
11.2
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
// -----------------------------------------------------------------------------
//
// Chapter 12 - Exercise 11
/*
Draw a series of regular polygons, one inside the other. The innermost should
be an equliateral triangle, enclosed by a square, enclosed by a pentagon, etc.
For the mathematically adept only: let all the points of each N-polygon touch
sides of the (N+1)-polygon.Hint: The trigonometric functions are found in
<cmath> (Section 24.8, Appendix B.9.2)
This short program tests how to draw any n-sized polygon given a radius and
number of sides.
*/
// https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files
// -----------------------------------------------------------------------------
//--INCLUDES--//
#include "Simple_window.h"
#include "Graph.h"
#include <cmath>
using namespace Graph_lib;
typedef Graph_lib::Rectangle rect;
const double PI = atan(1) * 4;
/*
regular polygon
- all sides are equal
- all angles equal
- all exterior angles of a polygon add up to 360
- exterior angle = 360 / n (n is number of sides)
- interior angle = 180 - exterior angle
or
interior angle = (n - 2) x 180 / n
*/
// -----------------------------------------------------------------------------
void drawPolygon(Simple_window& win, int numSides, int radius)
{
Graph_lib::Polygon poly;
Point p;
for (int i = 0; i < numSides; ++i)
{
p.x = radius * cos(2 * PI * i / numSides) + 500;
p.y = radius * sin(2 * PI * i / numSides) + 500;
poly.add(p);
}
poly.set_color(Color::black);
win.attach(poly);
win.wait_for_button();
}
// -----------------------------------------------------------------------------
int main()
{
Simple_window win{ Point{100,100}, 1000, 1000, "Chapter 12 - Exercise 11.2" };
drawPolygon(win, 4, 200);
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------