PHP 检查给定点是否位于矩形内(Check whether a given point lies inside a rectangle or not)

给定一个矩形的四个点和一个点 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。

<?php
// PHP program to check whether a 
// given point lies inside a 
// rectangle or not

// 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 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 
    $A = area($x1, $y1, $x2, $y2, $x3, $y3) + 
         area($x1, $y1, $x4, $y4, $x3, $y3);

    // Calculate area of triangle PAB 
    $A1 = area($x, $y, $x1, $y1, $x2, $y2);

    // Calculate area of triangle PBC 
    $A2 = area($x, $y, $x2, $y2, $x3, $y3);

    // Calculate area of triangle PCD
    $A3 = area($x, $y, $x3, $y3, $x4, $y4);

    // Calculate area of triangle PAD 
    $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

// 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))
    echo "yes";
    
else
    echo "no";
    
// This code is contributed by vt_m.
?>

输出: 

no

时间复杂度: O(1)

辅助空间: O(1)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn_aspnet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值