summaryrefslogtreecommitdiff
path: root/src/point.h
blob: 36cfda9859da37cb506072477ccb3364ce56b3c3 (plain)
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
//
// Created by saintruler on 11.04.19.
//

#ifndef OPENGLTEST_POINT_H
#define OPENGLTEST_POINT_H

#include <cmath>

class point
{
public:
    point(double x, double y)
    {
        this->x = x;
        this->y = y;
    }

    friend point operator+(const point& left, const point& right)
    {
        return point(left.x + right.x, left.y + right.y);
    }

    friend point operator+=(point& left, const point& right)
    {
        left.x += right.x;
        left.y += right.y;
        return left;
    }

    friend point operator-(const point& left, const point& right)
    {
        return point(left.x - right.x, left.y - right.y);
    }

    friend point operator-=(point& left, const double right)
    {
        left.x -= right;
        left.y -= right;
        return left;
    }

    friend point operator/(const point& left, const double right)
    {
        return point(left.x / right, left.y / right);
    }

    friend point operator/=(point& left, const double right)
    {
        left.x /= right;
        left.y /= right;
        return left;
    }

    friend point operator*(const point& left, const double right)
    {
        return point(left.x * right, left.y * right);
    }

    friend point operator*=(point& left, const double right)
    {
        left.x *= right;
        left.y *= right;
        return left;
    }

    double length()
    {
        return sqrt(pow(x, 2) + pow(y, 2));
    }

    double length_sqared()
    {
        return pow(x, 2) + pow(y, 2);
    }

    void normalize()
    {
        const double l = length();
        this->x /= l;
        this->y /= l;
    }

    double x, y;
};

#endif //OPENGLTEST_POINT_H