#include <iostream>
#include <vector>
#include <list>
#include "Point.h"
using namespace std;
#pragma once
class eleRect
{
public:
eleRect(vector <CPoint> & rect, int index = 0);
~eleRect(void);
bool if_in_quadrilat(CPoint point);
private:
int index;
vector <CPoint> rect;
CPoint getUnitVector (CPoint pointO,CPoint pointA ) ;
float angle_get(CPoint cen,CPoint first,CPoint second);
};
#include "StdAfx.h"
#include "eleRect.h"
#define PI 3.1415926535897932384626433832795
#define RECT_ANGLE_THRESH 1
eleRect::eleRect(vector <CPoint> & rect, int index)
{
this->rect = rect;
this->index = index;
}
eleRect::~eleRect(void)
{
}
CPoint eleRect :: getUnitVector (CPoint pointO,CPoint pointA )
{
CPoint point;
float distance;
distance = powf((pointO.x - pointA.x),2) + powf((pointO.y - pointA.y),2);
distance = sqrtf(distance);
point.x = float(pointA.x - pointO.x)/distance;
point.y = float(pointA.y - pointO.y)/distance;
return point;
}
float eleRect :: angle_get(CPoint cen,CPoint first,CPoint second)
{
float temp,distance1,distance2;
float angle;
CPoint p1;
CPoint p2;
p1 = first - cen;
p2 = second - cen;
temp = p1.x * p2.x + p1.y * p2.y;
if(0 == temp)
{
if (getUnitVector (cen,first) == getUnitVector (cen,second))
{
return 0;
}
if (getUnitVector (cen,first) == getUnitVector (second,cen))
{
return 180;
}
return -1;
}
distance1 = powf((p1.x),2) + powf((p1.y),2);
distance1 = sqrtf(distance1);
distance2 = powf((p2.x),2) + powf((p2.y),2);
distance2 = sqrtf(distance2);
angle = (float)acos(temp/(distance1 * distance2));
angle = angle*180/PI ;
return angle;
}
bool eleRect::if_in_quadrilat(CPoint point)
{
float angle = 0;
angle = angle_get(point,rect[index+0],rect[index+1]);
angle = angle + angle_get(point,rect[index+1],rect[index+2]);
angle = angle + angle_get(point,rect[index+2],rect[index+3]);
angle = angle + angle_get(point,rect[index+3],rect[index+0]);
if /*(360 == (int)angle)*/ (fabsf(360 - (int)angle)<RECT_ANGLE_THRESH)
{
return true;
}
return false;
}