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
|
#ifndef POINT_H
#define POINT_H
#include <iostream>
#include <math.h>
struct point {
float X, Y;
point(float X, float Y) :X(X),Y(Y) {};
point(float N) :X(N),Y(N) {};
point() :X(0.0),Y(0.0) {};
float Slope(point p)
{
//Slope = m
// m = (y2-y1) / (x2-x1)
float m = ((p.Y-Y) / (p.X-X));
return m;
}
float Distance(point p)
{
//Distance = d
//d = square_root of [ (x2-x1)^2 + (y2-y1)^2 ]
float d = ((p.X-X) * (p.X-X)) + ((p.Y-Y) * (p.Y-Y));
d = sqrt(d);
return d;
}
point Increment(point p, float dist)
{
//Find a point between this point and point(p), adding a specified distance(dist)
//New x position = Nx
//New y position = Ny
//Nx = X + (dist / square root of [ (m^2 + 1) ])
//Ny = Y + (m*dist / square root of [ (m^2 + 1) ])
float m = Slope(p);
float Nx = dist / (sqrt((m*m) + 1));
if (m >= 0.0) Nx = ((X < p.X) ? (X+Nx):(p.X+Nx));
if (m < 0.0) Nx = ((X < p.X) ? (X+Nx):(p.X+Nx));
float Ny = (m * dist) / (sqrt((m*m) + 1));
if (m >= 0.0) Ny = ((Y < p.Y) ? (Y+Ny):(p.Y+Ny));
if (m < 0.0) Ny = ((Y < p.Y) ? (p.Y+Ny):(Y+Ny));
return point(Nx,Ny);
}
float Magnitude()
{
//Distance from 0,0 (if point is a vector)
return Distance(point(0,0));
}
point Normalize()
{
float nX = (X/Magnitude());
float nY = (Y/Magnitude());
return point(nX,nY);
}
//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); }
};
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); }
std::ostream& operator<<(std::ostream& os, const point a)
{
os << "(" << a.X << "," << a.Y << ")";
return os;
}
#endif
|