判断一点是否在矩形中,已知矩形的顶点坐标


#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值