forked from lilypuye/cppLight2d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.h
More file actions
88 lines (84 loc) · 1.44 KB
/
Copy pathbasic.h
File metadata and controls
88 lines (84 loc) · 1.44 KB
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
#pragma once
#include <math.h> // fabsf(), fminf(), fmaxf(), sinf(), cosf(), sqrt()
struct Vector
{
float x;
float y;
Vector operator+(Vector v)
{
return{ x + v.x, y + v.y };
}
float operator*(Vector v) //µã³Ë
{
return x*v.x + y*v.y;
}
Vector operator*(float f) //±¶Êý
{
return{ x*f, y*f };
}
Vector operator/(float f)
{
return{ x / f, y / f };
}
float len() //ÏòÁ¿³¤¶È
{
return sqrt(x*x + y*y);
}
Vector reflect(const Vector normal)
{
float idotn2 = (normal.x * x + normal.y * y)*-2.f;
return{ x + idotn2 * normal.x, y + idotn2 * normal.y };
}
Vector normalize()
{
float length = len();
if (length > 0)
return{ x / length, y / length };
return{ 0.f, 0.f };
}
};
struct Point
{
float x;
float y;
Point operator+(Vector v)
{
return{ x + v.x, y + v.y };
}
Vector operator-(Point p)
{
return{ x - p.x, y - p.y };
}
Point operator-(Vector v)
{
return{ x - v.x, y - v.y };
}
bool IsValid()
{
return x >= 0.f && x <= 1.f && y > 0.f && y < 1.f;
}
};
struct Color
{
float r, g, b;
Color operator+(Color c)
{
return{ r + c.r, g + c.g, b + c.b };
}
Color operator*(float f)
{
return{ r*f, g*f, b*f };
}
Color operator/(float f)
{
return{ r / f, g / f,b / f };
}
bool operator>(Color c)
{
return (r + g + b > c.r + c.g + c.b);
}
bool operator<(Color c)
{
return (r + g + b < c.r + c.g + c.b);
}
};