简体   繁体   English

指定矩形内边和中心的点

[英]Point inside rectangle given sides and center

I'm given rectangle by it's height( h ) and width( w ), and it's center O(x0,y0) . 我通过它的height( h )和width( w )以及它的中心O(x0,y0)给定矩形。 I need to calculate if given point A(x,y) is inside that rectangle. 我需要计算给定点A(x,y)是否在该矩形内。 It is parallel to x and y axis. 它平行于x和y轴。 All values are real. 所有值都是真实的。

I came up with following test but for some reason website on which I'm testing the code is not working for all examples. 我提出了以下测试,但是由于某种原因,我正在测试的网站上的代码不适用于所有示例。 Could someone point me in the right direction. 有人可以指出我正确的方向。

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    long x,y,x0,y0,r,h,w;
    scanf("%ld",&x);
    scanf("%ld",&y);
    scanf("%ld",&x0);
    scanf("%ld",&y0);
    scanf("%ld",&h);
    scanf("%ld",&w);
    if((x0+w/2.0>=x)&&(x0-w/2.0<=x)&&(y0+h/2.0>=y)&&(y0-h/2.0<=y))
        printf("inside a rectangle");
    else
        printf("outside a rectangle");
}

Thanks in advance. 提前致谢。

After OP's Edit: OP修改后:

The rectangle's side are parallel to x axis and y-axis. 矩形的边平行于x轴和y轴。 Then also it is possible to get the co-ordinates and apply the below mentioned algorithm. 然后,也有可能获得坐标并应用以下提到的算法。

Centre -- (x0,y0)
A -- (x0-w/2,y0-h/2)
B -- (x0-w/2.y0+h/2)
C -- (x0+w/2,y0+h/2)
D -- (x0+w/2,y0-h/2)

So all you have to do is, Apply the algorithms provided below. 因此,您所要做的就是,应用下面提供的算法。

More simply we can do this, 更简单地说,我们可以做到这一点,

if( 2*x <= 2*x0+w && 2*x >= 2*x0-w && 2*y <= 2*y0+h && 2*y >= 2*y0-h)
// it's inside

Before OP's edit 在OP编辑之前

Your logic is wrong. 你的逻辑是错误的。 It may say a point inside rectangle to be outside of it. 可以说矩形内的一个点在矩形外。 (For any rectangle this is wrong - OP didn't mention the condition of being sides parallel to xy axes) (对于任何矩形,这都是错误的-OP没有提到边平行于xy轴的条件)

There is a simple way and cleaner way to do this for rectangle. 对于矩形,有一种简单且更简洁的方法。 Find the Area of the rectangle. 找到矩形的面积。

Suppose it's A . 假设它是A

Now if the point P lies inside ABCD then 现在,如果点P位于ABCD

area of PAB+PBC+PCD+PDA = A

For better thing do this with , 为了更好的事情做到这一点,

AB.Bc+BC.CD+CD.DA+DA.AB = 2*AB*BC

or even better make a square of both side 甚至更好地使双方成为正方形

LHS^2 = 4*AB^2*BC^2

Now you will just multiply and check it. 现在,您将乘以进行检查。 One drawback of this solution is for large values of side length you have a chance of overflow. 该解决方案的一个缺点是,对于较大的side length值,您可能会发生溢出。

Another method would be to consider the projections. 另一种方法是考虑预测。

If point is inside of the rectangle then the projection of the corner of rectangle to point, on two of it's side must be less than the corresponding sides. 如果point在矩形的内部,则指向矩形的角的投影在其两个侧面上必须小于相应的侧面。 You can check the projection length using dot products. 您可以使用点积检查投影长度。

For example if P is the point and ABCD is rectangle check, 例如,如果P是点, ABCD是矩形检查,

if AP 's projection on AB has greater than zero length but less than the length of AB . 如果APAB上的投影长度大于零但小于AB的长度。 Check the same with BC and BP and check if length is greater than zero and less than BC or not. BCBP进行检查,并检查长度是否大于零且小于BC

This two condition makes sure that your point lies inside the rectangle. 这两个条件确保您的点位于矩形内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM