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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#ifndef POINT_H
#define POINT_H
struct point{
public:
/* The x and y variable are made PUBLIC because they are only limited by
* the float data type constraints.
*/
float x, y;
/* The constructor will accept float and int data types or none at all.
* Two parameters sets to x,y respectively; one parameter sets both
* x and y to the same value. An empty constructor sets x,y = 0.0
*
* There is no destructor since the point is essential just two floats
*/
point() : x(0.0), y(0.0) {};
point(float x, float y) : x(x), y(y) {};
point(float val) : x(val), y(val) {};
point(int x, int y) : x(static_cast<float>(x)), y(static_cast<float>(y)) {};
point(int val) : x(static_cast<float>(val)), y(static_cast<float>(val)) {};
/* Returns the slope of two points.
* Formula for finding the slope(m) between
* two coordinate points is: (y2-y1) / (x2-x1)
*/
float slope(point p) {
return ((p.y-y) / (p.x-x));
}
/* Returns the distance between two points.
* Formula for finding the distance(d) between
* two points: square_root of [ (x2-x1)^2 + (y2-y1)^2 ]
*/
float distance(point p) {
float d = ((p.x-x) * (p.x-x)) + ((p.y-y) * (p.y-y));
return sqrt(d);
}
/* Increments the distance between two points.
* Formula for finding a new point between this point
* and point 'p' given a distance to increment:
* New(x) = x + (dist / square root of [ (m^2 + 1) ])
* New(y) = y + (m*dist / square root of [ (m^2 + 1) ])
*/
point increment(point p, float distance) {
float m = slope(p);
float newX = (distance / (sqrt((m*m) + 1))) ;
newX = ((x < p.x) ? (x + newX) : (x - newX));
float newY = ((m * distance) / (sqrt((m*m) + 1)));
if (m >= 0.0) newY = ((y < p.y) ? (y + newY) : (y - newY));
if (m < 0.0) newY = ((y < p.y) ? (y - newY) : (y + newY));
return point(newX, newY);
}
//Returns the magnitude of a point.
//Distance from point to (0,0)
float magnitude() {
return distance(point(0,0));
}
//Returns a normalized point
point normalize() {
return point(x/magnitude(), y/magnitude());
}
//Operators:
//Addition
point operator+=(point pnt){ (*this).x += pnt.x; (*this).y += pnt.y; return (*this); }
point operator+=(float num){ (*this).x += num; (*this).y += num; return (*this); }
point operator+(point pnt) { return point((*this).x + pnt.x, (*this).y + pnt.y); }
point operator+(float num) { return point((*this).x + num, (*this).y + num); }
//Subtraction
point operator-=(point pnt){ (*this).x -= pnt.x; (*this).y -= pnt.y; return (*this); }
point operator-=(float num){ (*this).x -= num; (*this).y -= num; return (*this); }
point operator-(point pnt) { return point((*this).x - pnt.x, (*this).y - pnt.y); }
point operator-(float num) { return point((*this).x - num, (*this).y - num); }
//Multiplication
point operator*=(point pnt){ (*this).x *= pnt.x; (*this).y *= pnt.y; return (*this); }
point operator*=(float num){ (*this).x *= num; (*this).y *= num; return (*this); }
point operator*(point pnt) { return point((*this).x * pnt.x, (*this).y * pnt.y); }
point operator*(float num) { return point((*this).x * num, (*this).y * num); }
//Division
point operator/=(point pnt){ (*this).x /= pnt.x; (*this).y /= pnt.y; return (*this); }
point operator/=(float num){ (*this).x /= num; (*this).y /= num; return (*this); }
point operator/(point pnt) { return point((*this).x / pnt.x, (*this).y / pnt.y); }
point operator/(float num) { return point((*this).x / num, (*this).y / num); }
//Equal (Assignment)
point operator=(point pnt) { (*this).x = pnt.x; (*this).y = pnt.y; return (*this); }
point operator=(float num) { (*this).x = num; (*this).y = num; return (*this); }
};
//Comparative operators:
bool operator==(point a, point b) { return a.x==b.x && a.y==b.y; }
bool operator==(point a, float num) { return a.x==num && a.y==num; }
bool operator!=(point a, point b) { return !(a==b); }
bool operator!=(point a, float num) { return !(a.x==num && a.y==num); }
//Other operators:
point operator+(float num, point pnt) { return (pnt + num); }
point operator*(float num, point pnt) { return (pnt * num); }
//Allows the display of the points in a 'coordinate' format (x,y) to the output stream
std::ostream& operator<<(std::ostream& os, const point a) {
os << "(" << a.x << "," << a.y << ")";
return os;
}
#endif
|