-
Notifications
You must be signed in to change notification settings - Fork 0
/
g1.cpp
123 lines (112 loc) · 2.56 KB
/
g1.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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "font.hpp"
#include "line_segment_2d.hpp"
#include "point_2d.hpp"
#include "sdl_app.hpp"
class App : public SDLApp
{
public:
App();
protected:
bool initialize(int argc, char *argv[]);
void draw();
private:
Point2D center_;
float scale_;
void line(const float x1, const float y1, const float x2, const float y2, uint32_t color);
void line(const Point2D &p1, const Point2D &p2, uint32_t color);
void line(const LineSegment2D &l, uint32_t color);
void lines(const std::vector<Point2D> &ps, uint32_t color);
};
App::App()
: SDLApp("g1")
, center_(400, 300)
, scale_(100)
{
}
bool App::initialize(int argc, char *argv[])
{
set_bg_color(0xffffffff);
return true;
}
void App::draw()
{
#if 0
LineSegment2D l1(0.0f, 1.0f, 1.0f, 1.0f);
LineSegment2D l2(0.0f, 0.0f, 0.8f, 1.5f);
Point2D cp1;
std::vector<Point2D> v;
l2.bound(v, l1);
line(l1, 0xff0000ff);
lines(v, 0x0000ffff);
#endif
#if 0
{
LineSegment2D l1(0, -3, 0, 3);
LineSegment2D l2(-1, 0, 0.5, 0.8);
LineSegment2D l3(1, 1, -0.5, 1.8);
LineSegment2D l4(1, -1, -0.5, -1.8);
LineSegment2D l5(-1, -2, 0.5, -2.8);
Point2D cp;
std::vector<Point2D> v;
line(l1, 0xff0000ff);
if (l2.bound(v, l1)) {
lines(v, 0x0000ffff);
}
v.clear();
if (l3.bound(v, l1)) {
lines(v, 0x00ff00ff);
}
v.clear();
if (l4.bound(v, l1)) {
lines(v, 0xffff00ff);
}
v.clear();
if (l5.bound(v, l1)) {
lines(v, 0xffff00ff);
}
}
#endif
{
LineSegment2D l1(0.0f, 2.0f, 2.0f, 0.0f);
LineSegment2D l2(1.0f, 0.0f, 1.0f, 2.0f);
std::vector<Point2D> v;
line(l1, 0xff0000ff);
if (l2.bound(v, l1)) {
lines(v, 0x0000ffff);
}
}
}
void App::line(const float x1, const float y1, const float x2, const float y2, uint32_t color)
{
SDL_SetRenderDrawColor(renderer(), (color >> 24) & 0xff, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff);
SDL_RenderDrawLine(renderer(),
center_.x + x1 * scale_,
center_.y - y1 * scale_,
center_.x + x2 * scale_,
center_.y - y2 * scale_);
}
void App::line(const Point2D &p1, const Point2D &p2, uint32_t color)
{
line(p1.x, p1.y, p2.x, p2.y, color);
}
void App::line(const LineSegment2D &l, uint32_t color)
{
line(l.p1, l.p2, color);
}
void App::lines(const std::vector<Point2D> &ps, uint32_t color)
{
for (size_t i = 0; i < ps.size() - 1; ++i) {
line(ps[i], ps[i + 1], color);
}
}
#ifdef __cplusplus
extern "C"
#endif
int main(int argc, char *argv[])
{
App app;
return app.run(argc, argv);
}