给定一个矩形的四个点和一个点 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 检查给定点是否位于三角形内:php 检查给定点是否位于三角形内(Check whether a given point lies inside a triangle or not)_python判定点在三角形内部-优快云博客
javascript 检查给定点是否位于三角形内:javascript 检查给定点是否位于三角形内(Check whether a given point lies inside a triangle or not)_js 判断点是否在三角形内部的方法-优快云博客
python 检查给定点是否位于三角形内:python 检查给定点是否位于三角形内(Check whether a given point lies inside a triangle or not)_python 三维空间 判断点在三角形内-优快云博客
C# 检查给定点是否位于三角形内:C# 检查给定点是否位于三角形内(Check whether a given point lies inside a triangle or not)_c# 判断点是否在三角形内-优快云博客
java 检查给定点是否位于三角形内:java 检查给定点是否位于三角形内(Check whether a given point lies inside a triangle or not)_判断一个点是否在三角形内 java-优快云博客
c语言 检查给定点是否位于三角形内:c语言 检查给定点是否位于三角形内(Check whether a given point lies inside a triangle or not)_stm32 c 如何判断屏幕的哪些像素点在该三角形内部-优快云博客
c++ 检查给定点是否位于三角形内:c++ 检查给定点是否位于三角形内(Check whether a given point lies inside a triangle or not)_c++判断一个点是否在三角形内-优快云博客
方法:设四个角的坐标为 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。
<script>
/* A utility function to calculate area of
triangle formed by (x1, y1), (x2, y2) and
(x3, y3) */
function area(x1, y1, x2, y2,
x3, y3)
{
return Math.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) */
function check(x1, y1, x2, y2, x3,
y3, x4, y4, x, y)
{
/* Calculate area of rectangle ABCD */
let A = area(x1, y1, x2, y2, x3, y3) +
area(x1, y1, x4, y4, x3, y3);
/* Calculate area of triangle PAB */
let A1 = area(x, y, x1, y1, x2, y2);
/* Calculate area of triangle PBC */
let A2 = area(x, y, x2, y2, x3, y3);
/* Calculate area of triangle PCD */
let A3 = area(x, y, x3, y3, x4, y4);
/* Calculate area of triangle PAD */
let 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 */
/* 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))
document.write("yes");
else
document.write("no");
// This code is contributed by Mayank Tyagi
</script>
输出:
no
时间复杂度: O(1)
辅助空间: O(1)
方法#2:使用多边形内的点算法
检查点是否位于矩形内的一种方法是使用点在多边形内的算法。在这种情况下,我们可以将矩形视为具有四个顶点的多边形,并检查点 P 是否位于该多边形内。
算法
1. 定义一个函数 point_in_rect(rect, p),以矩形顶点 rect 和点 p 作为输入。
2. 使用射线投射算法确定点 p 是否位于矩形多边形内:
a. 将计数变量初始化为零
b. 对于多边形的每条边,检查它是否与从点 p 沿正 x 方向延伸到无穷远的射线相交
c. 如果边与射线相交,则增加计数
d. 如果计数为奇数,则该点位于多边形内;否则,它位于多边形外
// Javascript code addition
function point_in_rect(rect, p) {
const n = rect.length;
let inside = false;
let j = n - 1;
for (let i = 0; i < n; i++) {
if ((rect[i][1] > p[1]) != (rect[j][1] > p[1])) {
if (p[0] < (rect[j][0] - rect[i][0]) * (p[1] - rect[i][1]) / (rect[j][1] - rect[i][1]) + rect[i][0]) {
inside = !inside;
}
}
j = i;
}
return inside;
}
// Example usage
let R = [[10, 10], [10, -10], [-10, -10], [-10, 10]];
let P = [0, 0];
console.log(point_in_rect(R, P));
R = [[10, 10], [10, -10], [-10, -10], [-10, 10]];
P = [20, 20];
console.log(point_in_rect(R, P));
// The code is contributed by Nidhi goel.
输出:
True
False
时间复杂度: O(n),其中 n 是多边形的边数(在本例中,n=4)
空间复杂度: O(1)