-
Notifications
You must be signed in to change notification settings - Fork 0
/
float2.cpp
executable file
·79 lines (62 loc) · 1.26 KB
/
float2.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
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include "float2.h"
using namespace std;
float2 float2::operator+(float2 b) const{
return float2(x + b.x, y + b.y);
}
float2 float2::operator-(float2 b) const{
return float2(x - b.x, y - b.y);
}
float2 float2::operator*(float2 b) const{
return float2(x * b.x, y * b.y);
}
float2 float2::operator*(float s) const{
return float2(x * s, y * s);
}
float2 float2::operator/(float s) const{
return float2(x / s, y / s);
}
float2 float2::operator/(float2 b) const{
return float2(x / b.x, y / b.y);
}
void float2::operator*=(float s) {
x *= s;
y *= s;
}
void float2::operator+=(float2 b) {
x += b.x;
y += b.y;
}
void float2::operator/=(float s) {
x /= s;
y /= s;
}
void float2::operator*=(float2 s) {
x *= s.x;
y *= s.y;
}
float float2::lengthSquared() const{
return x*x + y*y;
}
float float2::length() const{
return sqrtf(x*x + y*y);
}
float2 float2::normalize()
{
*this /= length();
return *this;
}
float2 float2::random()
{
return float2(
((float)rand() / RAND_MAX) * 2-1,
((float)rand() / RAND_MAX) * 2-1);
}
float2 float2::random(float minX, float maxX, float minY, float maxY)
{
return float2(
((float)rand() / RAND_MAX) * (maxX-minX)+minX,
((float)rand() / RAND_MAX) * (maxY-minY)+minY);
}