-
Notifications
You must be signed in to change notification settings - Fork 0
/
float2.cpp
111 lines (91 loc) · 1.92 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
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
//
// float2.cpp
// Homework 2: Curves
//
// Created by mahina kaholokula on 10/16/14.
// Copyright (c) 2014 mahina kaholokula. All rights reserved.
//
#include <math.h>
#include <stdlib.h>
class float2
{
public:
float x;
float y;
float2()
{
x = 0.0f;
y = 0.0f;
}
float2(float x, float y):x(x),y(y){}
float2 operator-() const
{
return float2(-x, -y);
}
float2 operator+(const float2& addOperand) const
{
return float2(x + addOperand.x, y + addOperand.y);
}
float2 operator-(const float2& operand) const
{
return float2(x - operand.x, y - operand.y);
}
float2 operator*(const float2& operand) const
{
return float2(x * operand.x, y * operand.y);
}
float2 operator*(float operand) const
{
return float2(x * operand, y * operand);
}
float2 operator/(float operand) const
{
return float2(x / operand, y / operand);
}
float2 operator-=(const float2& a)
{
x -= a.x;
y -= a.y;
return *this;
}
float2 operator+=(const float2& a)
{
x += a.x;
y += a.y;
return *this;
}
float2 operator*=(const float2& a)
{
x *= a.x;
y *= a.y;
return *this;
}
float2 operator*=(float a)
{
x *= a;
y *= a;
return *this;
}
float norm()
{
return sqrtf(x*x+y*y);
}
float norm2()
{
return x*x+y*y;
}
float2 normalize()
{
float oneOverLength = 1.0f / norm();
x *= oneOverLength;
y *= oneOverLength;
return *this;
}
//between -1 and 1
static float2 random()
{
return float2(
((float)rand() / RAND_MAX) * 2 - 1,
((float)rand() / RAND_MAX) * 2 - 1);
}
};