I've done a bit of research and created some code (with help from
http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/).
Below is my code, sloppy as it is, it does the trick. I hope it is helpful to those who need it. For this code, I found that I had to add the point twice into the vector. Can anyone refine the process, or am I on the right track?
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const double pi = 3.14;
const double twopi = 6.28;
struct Point
{
int x, y;
};
bool InsidePolygon(vector<Point> Poly, int sides, Point mouseCoods);
double Angle2D(double x1, double y1, double x2, double y2);
int main()
{
sf::RenderWindow App(sf::VideoMode(800,600,32), "Inside Polygon Test");
App.SetFramerateLimit(60);
sf::Event appEvent;
vector<Point> vPolygon;
sf::Shape Polygon;
while(App.IsOpened())
{
while(App.GetEvent(appEvent))
{
if(appEvent.Type == sf::Event::Closed)
{
App.Close();
}
if(appEvent.Type == sf::Event::MouseButtonPressed)
{
if(appEvent.MouseButton.Button == sf::Mouse::Left)
{
Point temp;
temp.x = appEvent.MouseButton.X;
temp.y = appEvent.MouseButton.Y;
if(InsidePolygon(vPolygon, vPolygon.size(), temp))
{
cout << "Inside." << endl;
}
}
if(appEvent.MouseButton.Button == sf::Mouse::Right)
{
Point temp;
temp.x = appEvent.MouseButton.X;
temp.y = appEvent.MouseButton.Y;
Polygon.AddPoint(temp.x, temp.y);
vPolygon.push_back(temp);
vPolygon.push_back(temp);
}
}
}
App.Clear();
App.Draw(Polygon);
App.Display();
}
return 0;
}
bool InsidePolygon(vector<Point> Poly, int sides, Point mouseCoods)
{
int i;
double angle=0;
Point p1,p2;
for (i=0;i<sides;i++) {
p1.x = Poly[i].x - mouseCoods.x;
p1.y = Poly[i].y - mouseCoods.y;
p2.x = Poly[(i+1)%sides].x - mouseCoods.x;
p2.y = Poly[(i+1)%sides].y - mouseCoods.y;
angle += Angle2D(p1.x,p1.y,p2.x,p2.y);
}
if (abs(angle) < pi)
return(FALSE);
else
return(TRUE);
}
double Angle2D(double x1, double y1, double x2, double y2)
{
double dtheta,theta1,theta2;
theta1 = atan2(y1,x1);
theta2 = atan2(y2,x2);
dtheta = theta2 - theta1;
while (dtheta > pi)
dtheta -= twopi;
while (dtheta < -pi)
dtheta += twopi;
return(dtheta);
}
Thanks.