-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
117 lines (101 loc) · 2.68 KB
/
Main.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
#include <iostream>
#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"
#include <math.h>
using namespace std;
struct Vec2
{
float x;
float y;
};
class DrawPolygon : public olc::PixelGameEngine
{
public:
DrawPolygon()
{
sAppName = "DrawPolygon demo";
}
private:
float r = 10.0f;
Vec2 coords;
void DrawHexagon(Vec2 coords, float radius)
{
float inr = radius * (sqrt(3) / 2);
Vec2 corners[] = {
{0.0f + coords.x, radius + coords.y},
{inr + coords.x, (radius * 0.5f) + coords.y},
{inr + coords.x, (radius * -0.5f) + coords.y},
{0.0f + coords.x, -radius + coords.y},
{-inr + coords.x, (radius * -0.5f) + coords.y},
{-inr + coords.x, (radius * 0.5f) + coords.y}
};
for (int n = 0; n < 5; n++)
{
DrawLine(corners[n].x, corners[n].y, corners[n+1].x, corners[n+1].y, olc::WHITE);
}
DrawLine(corners[5].x, corners[5].y, corners[0].x, corners[0].y, olc::WHITE);
}
void DrawRPolygon(Vec2 coords, float radius, int N, float rot, olc::Pixel col)
{
float rotRad = rot * M_PI/180;
for (int n = 0; n < N; n++)
{
float x1 = radius * cos(2 * M_PI * n / N + rotRad) + coords.x;
float y1 = radius * sin(2 * M_PI * n / N + rotRad) + coords.y;
float x2 = radius * cos(2 * M_PI * (n+1) / N + rotRad) + coords.x;
float y2 = radius * sin(2 * M_PI * (n+1) / N + rotRad) + coords.y;
DrawLine(x1, y1, x2, y2, col);
}
}
void FillRPolygon(Vec2 coords, float radius, int N, float rot, olc::Pixel col)
{
float rotRad = rot * M_PI / 180;
for (int n = 0; n < N; n++)
{
float x1 = radius * cos(2 * M_PI * n / N + rotRad) + coords.x;
float y1 = radius * sin(2 * M_PI * n / N + rotRad) + coords.y;
float x2 = radius * cos(2 * M_PI * (n + 1) / N + rotRad) + coords.x;
float y2 = radius * sin(2 * M_PI * (n + 1) / N + rotRad) + coords.y;
FillTriangle(x1, y1, x2, y2, coords.x, coords.y, col);
}
}
public:
bool OnUserCreate() override
{
coords.x = ScreenWidth() / 2;
coords.y = ScreenHeight() / 2;
return true;
}
bool OnUserUpdate(float fElapsedTime) override
{
Clear(olc::BLUE);
/*if (GetKey(olc::Key::W).bHeld)
{
coords.y -= 100 * fElapsedTime;
}
if (GetKey(olc::Key::S).bHeld)
{
coords.y += 100 * fElapsedTime;
}
if (GetKey(olc::Key::A).bHeld)
{
coords.x -= 100 * fElapsedTime;
}
if (GetKey(olc::Key::D).bHeld)
{
coords.x += 100 * fElapsedTime;
}*/
FillRPoplygon(coords, 100, 6, 0, olc::Red);
DrawRPoplygon(coords, 100, 6, 0, olc::WHITE);
return true;
}
};
int main()
{
DrawPolygon demo;
if (demo.Construct(400, 400, 2, 2, false, true))
{
demo.Start();
}
return true;
}