给定一个矩形的四个点和一个点 P。编写一个函数来检查 P 是否位于给定的矩形内。
示例:
输入:R = [(10, 10), (10, -10),
(-10, -10), (-10, 10)]
P = (0, 0)
输出:yes
如图:
输入:R = [(10, 10), (10, -10),
(-10, -10), (-10, 10)],
P = (20, 20)
输出:no
如图:
先决条件:检查给定点是否位于三角形内
php 检查给定点是否位于三角形内:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/143114210
javascript 检查给定点是否位于三角形内:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/143114143
python 检查给定点是否位于三角形内:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/143113966
C# 检查给定点是否位于三角形内:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/143114070
java 检查给定点是否位于三角形内:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/143113892
c语言 检查给定点是否位于三角形内:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/143113851
c++ 检查给定点是否位于三角形内:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/143112928
方法:设四个角的坐标为 A(x1, y1)、B(x2, y2)、C(x3, y3) 和 D(x4, y4)。给定点 P 的坐标为 (x, y)
1) 计算给定矩形的面积,即矩形 ABCD 的面积为三角形 ABC 的面积 + 三角形 ACD 的面积。
面积 A = [ x1(y2 – y3) + x2(y3 – y1) + x3(y1-y2)]/2 + [ x1(y4 – y3) + x4(y3 – y1) + x3(y1-y4)]/2
2) 计算三角形 PAB 的面积为 A1。
3) 计算三角形 PBC 的面积为 A2。
4) 计算三角形 PCD 的面积为 A3。
5) 计算三角形 PAD 的面积为 A4。
6) 如果 P 位于三角形内部,则 A1 + A2 + A3 + A4 必须等于 A。
#include <bits/stdc++.h>
using namespace std;
/* A utility function to calculate area of
triangle formed by (x1, y1), (x2, y2) and
(x3, y3) */
float area(int x1, int y1, int x2, int y2,
int x3, int y3)
{
return abs((x1 * (y2 - y3) + x2 * (y3 - y1) +
x3 * (y1 - y2)) / 2.0);
}
/* A function to check whether point P(x, y)
lies inside the rectangle formed by A(x1, y1),
B(x2, y2), C(x3, y3) and D(x4, y4) */
bool check(int x1, int y1, int x2, int y2, int x3,
int y3, int x4, int y4, int x, int y)
{
/* Calculate area of rectangle ABCD */
float A = area(x1, y1, x2, y2, x3, y3) +
area(x1, y1, x4, y4, x3, y3);
/* Calculate area of triangle PAB */
float A1 = area(x, y, x1, y1, x2, y2);
/* Calculate area of triangle PBC */
float A2 = area(x, y, x2, y2, x3, y3);
/* Calculate area of triangle PCD */
float A3 = area(x, y, x3, y3, x4, y4);
/* Calculate area of triangle PAD */
float A4 = area(x, y, x1, y1, x4, y4);
/* Check if sum of A1, A2, A3 and A4
is same as A */
return (A == A1 + A2 + A3 + A4);
}
/* Driver program to test above function */
int main()
{
/* Let us check whether the point P(10, 15)
lies inside the rectangle formed by A(0, 10),
B(10, 0) C(0, -10) D(-10, 0) */
if (check(0, 10, 10, 0, 0, -10, -10, 0, 10, 15))
cout << "yes";
else
cout << "no";
return 0;
}
输出:
no
时间复杂度: O(1)
辅助空间: O(1)
方法#2:使用多边形内的点算法
检查点是否位于矩形内的一种方法是使用点在多边形内的算法。在这种情况下,我们可以将矩形视为具有四个顶点的多边形,并检查点 P 是否位于该多边形内。
算法
1. 定义一个函数 point_in_rect(rect, p),以矩形顶点 rect 和点 p 作为输入。
2. 使用射线投射算法确定点 p 是否位于矩形多边形内:
a. 将计数变量初始化为零
b. 对于多边形的每条边,检查它是否与从点 p 沿正 x 方向延伸到无穷远的射线相交
c. 如果边与射线相交,则增加计数
d. 如果计数为奇数,则该点位于多边形内;否则,它位于多边形外
#include <iostream>
#include <vector>
using namespace std;
bool point_in_rect(vector<pair<int, int> >& rect,
pair<int, int>& p)
{
int n = rect.size();
bool inside = false;
int j = n - 1;
for (int i = 0; i < n; i++) {
if ((rect[i].second > p.second)
!= (rect[j].second > p.second)) {
if (p.first
< (rect[j].first - rect[i].first)
* (p.second - rect[i].second)
/ (rect[j].second
- rect[i].second)
+ rect[i].first) {
inside = !inside;
}
}
j = i;
}
return inside;
}
int main()
{
vector<pair<int, int> > R = {
{ 10, 10 }, { 10, -10 }, { -10, -10 }, { -10, 10 }
};
pair<int, int> P = { 0, 0 };
cout << point_in_rect(R, P) << endl;
R = {
{ 10, 10 }, { 10, -10 }, { -10, -10 }, { -10, 10 }
};
P = { 20, 20 };
cout << point_in_rect(R, P) << endl;
return 0;
}
输出:
True
False
时间复杂度: O(n),其中 n 是多边形的边数(在本例中,n=4)
空间复杂度: O(1)