给定一个矩形的四个点和一个点 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)_判断点是否在三角形内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。
class GFG
{
/* A utility function to calculate area of
triangle formed by (x1, y1), (x2, y2) and
(x3, y3) */
static float area(int x1, int y1, int x2,
int y2, int x3, int y3)
{
return (float)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) */
static boolean 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 code
public static void main (String[] args)
{
/* 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))
System.out.print("yes");
else
System.out.print("no");
}
}
// This code is contributed by Anant Agarwal.
输出:
no
时间复杂度: O(1)
辅助空间: O(1)
方法#2:使用多边形内的点算法
检查点是否位于矩形内的一种方法是使用点在多边形内的算法。在这种情况下,我们可以将矩形视为具有四个顶点的多边形,并检查点 P 是否位于该多边形内。
算法
1. 定义一个函数 point_in_rect(rect, p),以矩形顶点 rect 和点 p 作为输入。
2. 使用射线投射算法确定点 p 是否位于矩形多边形内:
a. 将计数变量初始化为零
b. 对于多边形的每条边,检查它是否与从点 p 沿正 x 方向延伸到无穷远的射线相交
c. 如果边与射线相交,则增加计数
d. 如果计数为奇数,则该点位于多边形内;否则,它位于多边形外
import java.util.*;
public class Main {
// Function to check if a point lies inside a rectangle
static boolean pointInRect(int[][] rect, int[] p)
{
int n = rect.length;
boolean inside = false;
int j = n - 1;
for (int i = 0; i < n; i++) {
if ((rect[i][1] > p[1]) != (rect[j][1] > p[1])
&& (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;
}
public static void main(String[] args)
{
// Example usage
int[][] R = { { 10, 10 },
{ 10, -10 },
{ -10, -10 },
{ -10, 10 } };
int[] P = { 0, 0 };
System.out.println(pointInRect(R, P));
int[] P2 = { 20, 20 };
System.out.println(pointInRect(R2, P2));
}
}
输出:
True
False
时间复杂度: O(n),其中 n 是多边形的边数(在本例中,n=4)
空间复杂度: O(1)