简体   繁体   English

C点形成矩形

[英]C points forming rectangle

I have this piece of code for finding out whether 4 x,y coordinates make a rectangle which works perfectly well, but, I need to make it so that it only works if the points are entered in the correct order, ie: Point #1: 0 0 Point #2: 20 0 Point #3: 20 50 Point #4: 0 50 would say "Is a rectangle" but : Point #1: 0 0 Point #2: 20 0 Point #3: 0 50 Point #4: 20 50 Would say "Is not a rectangle" (because the points arent in the right order) 我有这段代码来找出4 x,y坐标是否使矩形完美地工作,但是,我需要使它只有在以正确的顺序输入点时才起作用,即:Point#1 :0 0点#2:20 0点#3:20 50点#4:0 50会说“是一个矩形”, :点#1:0 0点#2:20 0点#3:0 50点# 4:20 50会说“不是矩形”(因为点arent的顺序正确)

This is what i have: 这就是我所拥有的:

 static bool IsRectangle(float x1, float y1, float x2, float y2,
                       float x3, float y3, float x4, float y4)
    {
    x2 -= x1; x3 -= x1; x4 -= x1; y2 -= y1; y3 -= y1; y4 -= y1;
    return
    (x2 + x3 == x4 && y2 + y3 == y4 && x2 * x3 == -y2 * y3) ||
    (x2 + x4 == x3 && y2 + y4 == y3 && x2 * x4 == -y2 * y4) ||
    (x3 + x4 == x2 && y3 + y4 == y2 && x3 * x4 == -y3 * y4);
    }

Im totally unsure as to how to limit it to only work in the correct order... :( Thank you 我完全不确定如何将其限制为仅以正确的顺序工作... :(谢谢

Note: This method only works for rectangles aligned with the axes. 注意:此方法仅适用于与轴对齐的矩形。

Think about the delta X/Y between adjacent points. 考虑相邻点之间的增量X / Y。 In the first case you have: 在第一种情况下,您具有:

  • ( 20, 0) (20,0)
  • ( 0, 50) (0,50)
  • (-20, 0) (-20,0)

and in the second case you have: 在第二种情况下,您有:

  • ( 20, 0) (20,0)
  • (-20, 50) <=== (-20,50)<===
  • ( 20, 0) (20,0)

The highlighted line shows you what to look for if the points are out of order...check for delta X/Y between adjacent points where both are non-zero. 突出显示的行显示了如果点不正常时要查找的内容...检查两个都不为零的相邻点之间的增量X / Y。

In terms of code something like the following should work (not tested): 就代码而言,类似以下内容的代码应该起作用(未经测试):

static bool IsRectangle(float x1, float y1, float x2, float y2,
                   float x3, float y3, float x4, float y4)
{
    int dx1 = x2 - x1;
    int dx2 = x3 - x2;
    int dx3 = x4 - x3;
    int dy1 = y2 - y1;
    int dy2 = y3 - y2;
    int dy3 = y4 - y3;

    x2 -= x1; x3 -= x1; x4 -= x1; y2 -= y1; y3 -= y1; y4 -= y1;

    return
        (dx1 == 0 || dy1 == 0) && (dx2 == 0 || dy2 == 0) && (dx3 == 0 || dy3 == 0) &&
        ((x2 + x3 == x4 && y2 + y3 == y4 && x2 * x3 == -y2 * y3) ||
        (x2 + x4 == x3 && y2 + y4 == y3 && x2 * x4 == -y2 * y4) ||
        (x3 + x4 == x2 && y3 + y4 == y2 && x3 * x4 == -y3 * y4));
}

Check each pair of consecutive segments to ensure they're perpendicular: (x1,y1) --> (x2,y2) perpendicular to (x2,y2) --> (x3,y3), (x2,y2) --> (x3,y3) perpendicular to (x3,y3) --> (x4,y4), etc., ending with (x4,y4) --> (x1,y1). 检查每对连续的分段以确保它们垂直:(x1,y1)->(x2,y2)垂直于(x2,y2)->(x3,y3),(x2,y2)-> (x3,y3)垂直于(x3,y3)->(x4,y4)等,以(x4,y4)->(x1,y1)结尾。 If so, you have a rectangle. 如果是这样,则您有一个矩形。

So for each segment, you need to subtract the endpoints to get a vector for the segment... then for each (consecutive) pair of these vectors check that the sum of product of the X components and the product of the Y components is zero, indicating that they're orthogonal. 因此,对于每个段,您需要减去端点以获得该段的向量...然后对于这些向量的每对(连续),检查X分量与Y分量的乘积之和为零,表示它们是正交的。

For example: 例如:

static bool IsRectangle(float x1, float y1, float x2, float y2, 
                        float x3, float y3, float x4, float y4)
{
  return !( ((x2-x1)*(x3-x2) + (y2-y1)*(y3-y2)) ||
            ((x3-x2)*(x4-x3) + (y3-y2)*(y4-y3)) ||
            ((x4-x3)*(x1-x4) + (y4-y3)*(y1-y4)) ||
            ((x1-x4)*(x2-x1) + (y1-y4)*(y2-y1)) );
}

The example above may need to be adjusted to allow for rounding errors if very large or non-integer coordinates are used, though. 但是,如果使用非常大或非整数的坐标,则可能需要调整上述示例以允许舍入误差。

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

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