-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.h
194 lines (158 loc) · 4.65 KB
/
object.h
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
//object.h
//
//You do not need to modify this file
//C.P
////////////////////////////////////////////////////////////
#ifndef __OBJECT_H__
#define __OBJECT_H__
#include <vector>
struct Color
{
double r;
double g;
double b;
double a;
Color() : r(0.0), g(0.0), b(0.0), a(0.0) {}
Color(const double & nr, const double & ng, const double & nb, const double & na = 0.0)
: r(nr), g(ng), b(nb), a(na)
{}
};
#define WHITE Color(1.0, 1.0, 1.0)
#define BLACK Color(0.0, 0.0, 0.0)
#define RED Color(1.0, 0.0, 0.0)
#define GREEN Color(0.0, 1.0, 0.0)
#define BLUE Color(0.0, 0.0, 1.0)
#define YELLOW Color(1.0, 1.0, 0.0)
#define PINK Color(1.0, 0.0, 1.0)
#define CYAN Color(0.0, 1.0, 1.0)
#define ORANGE Color(1.0, 0.5, 0.0)
#define VIOLET Color(0.5, 0.0, 1.0)
#define GRAY Color(0.5, 0.5, 0.5)
#define BROWN Color(0.6, 0.3, 0.0)
struct Object2D;
struct Shape2D;
struct Point2D;
struct Line2D;
struct Rect2D;
struct Circle2D;
struct Object2D
{
Color color;
Object2D() : color() {}
Object2D(const Color & c) : color(c) {}
virtual void move(double dx, double dy) = 0;
virtual void render() const;
virtual void render(const Color & col) const;
virtual ~Object2D() {}
};
struct Shape2D : public Object2D
{
bool filled;
Shape2D() : Object2D(), filled(false) {}
Shape2D(const Color & c, bool f = false)
: Object2D(c), filled(f)
{}
virtual void present(bool ignoreParam = true, bool fil = false) const = 0;
virtual void render(const Color & col, bool fil) const = 0;
virtual ~Shape2D() {}
};
struct Point2D : public Object2D
{
double x;
double y;
Point2D();
Point2D(const double & nx, const double & ny, const Color & col = WHITE);
Point2D operator+(const Point2D & rhs) const { return Point2D(x + rhs.x, y + rhs.y); }
Point2D operator-(const Point2D & rhs) const { return Point2D(x - rhs.x, y - rhs.y); }
Point2D operator*(double val) const { return Point2D(x * val, y * val); }
Point2D operator/(double val) const { return Point2D(x / val, y / val); }
Point2D operator+=(const Point2D & rhs) { x += rhs.x; y += rhs.y; return *this; }
Point2D operator-=(const Point2D & rhs) { x -= rhs.x; y -= rhs.y; return *this; }
Point2D operator*=(double val) { x *= val; y *= val; return *this; }
Point2D operator/=(double val) { x /= val; y /= val; return *this; }
double magnitude() const;
double magnitude2() const;
Point2D normalized() const;
void set(const Point2D & pos);
virtual void move(double dx, double dy);
virtual void render() const;
virtual void render(const Color & col) const;
virtual ~Point2D() {}
};
struct Line2D : public Object2D
{
Point2D p1;
Point2D p2;
Line2D();
Line2D(double x1, double y1, double x2, double y2, const Color & col = WHITE);
Line2D(const Point2D & p1, const Point2D & p2, const Color & col = WHITE);
double magnitude() const;
double magnitude2() const;
double angle() const;
double dot() const;
double cross() const;
Point2D delta() const;
Point2D midpoint() const;
Rect2D boundingRect() const;
void set(const Point2D & pos);
virtual void move(double dx, double dy);
virtual void render() const;
virtual void render(const Color & col) const;
virtual ~Line2D() {}
};
struct Rect2D : public Shape2D
{
Point2D p;
double width;
double height;
Rect2D();
Rect2D(const Point2D & p, double width, double height, const Color & col = WHITE, bool fill = false);
double perimeter() const;
double area() const;
Point2D center() const;
void set(const Point2D & pos);
virtual void move(double dx, double dy);
virtual void present(bool ignoreParam = true, bool fil = false) const;
virtual void render() const;
virtual void render(const Color & col) const;
virtual void render(const Color & col, bool fil) const;
virtual ~Rect2D() {}
};
struct Circle2D : public Shape2D
{
Point2D p;
double radius;
Circle2D();
Circle2D(const Point2D & p, double radius, const Color & col = Color(), bool fill = false);
double perimeter() const;
double area() const;
Rect2D boundingRect() const;
void set(const Point2D & pos);
virtual void move(double dx, double dy);
virtual void present(bool ignoreParam = true, bool fil = false) const;
virtual void render() const;
virtual void render(const Color & col) const;
virtual void render(const Color & col, bool fil) const;
virtual ~Circle2D() {}
};
struct Player : public Circle2D
{
Circle2D c;
double vx;
double vy;
bool up;
bool down;
bool left;
bool right;
Player() : Circle2D(), vx(0.0), vy(0.0)
{}
Player(Circle2D c1, double a, double b) : vx(a), vy(b)
{
c = Circle2D(c1.p, c1.radius, c1.color, true);
}
virtual void render() const;
virtual void move(double dx, double dy);
virtual void updatePlayer(double x, double y, std::vector <Line2D> l);
virtual ~Player() {}
};
#endif