-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.cpp
94 lines (81 loc) · 2.8 KB
/
point.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
#include "point.h"
#include <stdexcept>
#include <random>
#include <cmath>
/**
* @brief Point::Point Creates a new point with specified coordinates.
* @param x X-coordinate must be within range [0,1]
* @param y Y-coordinate must be within range [0,1]
*/
Point::Point(double x, double y)
{
if (x > 1 || x < 0 || y > 1 || y < 0){
throw std::out_of_range("Point must be within range [(0,0)-(0,0)]");
}
this->x = x;
this->y = y;
}
/**
* @brief Point::Point Creates a point with random coordinates in range [0,1].
*/
Point::Point(){
//Initialize RNG
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<double> dist(0,1);
//Randomize coordinates
this->x = dist(gen);
this->y = dist(gen);
}
//Getters
double Point::getX() const {return x;}
double Point::getY() const {return y;}
/**
* @brief Point::distance returns the euclidean distance between this and another point
* @param anotherPoint
* @return
*/
double Point::distance(const Point& anotherPoint) const{
return sqrt(pow( (x - anotherPoint.getX()), 2) + pow( (y - anotherPoint.getY()), 2));
}
/**
* @brief Point::move moves this point towards another point.
* @param anotherPoint gives the direction for movement. Will not be modified.
* @param precent double value in range [0,1] tells how much to move the point.
* 0 won't move at all and 1 will move this point to the same location as anotherPoint.
*/
void Point::move(const Point& anotherPoint, double percent){
if (percent > 1 || percent < 0){
throw std::out_of_range("Point must be within range [(0,0)-(0,0)]");
}
//Handles the special case of two points with the same x-coordinates
if (x == anotherPoint.getX()){
double distance = this->distance(anotherPoint);
double Yupdate = distance * percent;
//Apply a positive update if anotherPoint is above this one
if (y < anotherPoint.getY()){
y = y + Yupdate;
} else{ //Negative update if anotherPoint is below this one
y = y - Yupdate;
}
return; //Prevent the normal update
}
//The normal update
double distance = this->distance(anotherPoint);
double slope = (anotherPoint.getY() - y) / (anotherPoint.getX() - x);
double updateLength = distance * percent;
double slopeLength = sqrt(1 + pow(slope, 2));
double Xupdate = updateLength / slopeLength;
double Yupdate = (updateLength * slope) / slopeLength;
//Apply a positive update if another point is to the right
if (x < anotherPoint.getX()){
x = x + Xupdate;
y = y + Yupdate;
}else{ //Negative update if another point is to the left
x = x - Xupdate;
y = y - Yupdate;
}
}
bool operator== (Point &p1, const Point &p2){
return (p1.getX() == p2.getX() && p1.getY() == p2.getY());
}