簡體   English   中英

求平面上的 4 個點是否構成一個矩形?

[英]find if 4 points on a plane form a rectangle?

有人可以用 C 風格的偽代碼告訴我如何編寫一個函數(代表你喜歡的點),如果 4 個點(函數的參數)形成一個矩形,則返回 true,否則返回 false?

我想出了一個解決方案,首先嘗試找到具有相等 x 值的 2 對不同的點,然后對 y 軸執行此操作。 但是代碼比較長。 只是好奇看看其他人想出了什么。

  • 求角點的質心:cx=(x1+x2+x3+x4)/4, cy=(y1+y2+y3+y4)/4
  • 測試從質心到所有 4 個角的距離的平方是否相等
bool isRectangle(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { double cx,cy; double dd1,dd2,dd3,dd4; cx=(x1+x2+x3+x4)/4; cy=(y1+y2+y3+y4)/4; dd1=sqr(cx-x1)+sqr(cy-y1); dd2=sqr(cx-x2)+sqr(cy-y2); dd3=sqr(cx-x3)+sqr(cy-y3); dd4=sqr(cx-x4)+sqr(cy-y4); return dd1==dd2 && dd1==dd3 && dd1==dd4; }

(當然,在實踐中測試兩個浮點數 a 和 b 的相等性應該以有限的精度完成:例如 abs(ab) < 1E-6)

struct point
{
    int x, y;
}

// tests if angle abc is a right angle
int IsOrthogonal(point a, point b, point c)
{
    return (b.x - a.x) * (b.x - c.x) + (b.y - a.y) * (b.y - c.y) == 0;
}

int IsRectangle(point a, point b, point c, point d)
{
    return
        IsOrthogonal(a, b, c) &&
        IsOrthogonal(b, c, d) &&
        IsOrthogonal(c, d, a);
}

如果訂單事先不知道,我們需要稍微復雜一點的檢查:

int IsRectangleAnyOrder(point a, point b, point c, point d)
{
    return IsRectangle(a, b, c, d) ||
           IsRectangle(b, c, a, d) ||
           IsRectangle(c, a, b, d);
}
  • 平移四邊形,使其頂點之一現在位於原點
  • 剩下的三個點從原點形成三個向量
  • 其中之一必須代表對角線
  • 另外兩個必須代表雙方
  • 根據平行四邊形規則,如果邊形成對角線,我們有一個平行四邊形
  • 如果邊成直角,它是一個有直角的平行四邊形
  • 平行四邊形的對角相等
  • 平行四邊形的連續角是互補的
  • 所以所有的角都是直角
  • 它是一個矩形
  • 不過,它在代碼中更加簡潔:-)

     static bool IsRectangle( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int 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); }
  • (如果你想讓它使用浮點值,請不要盲目地替換標頭中的int聲明。這是不好的做法。它們存在是有原因的。人們應該始終使用錯誤的一些上限比較浮點結果時。)

1. Find all possible distances between given 4 points. (we will have 6 distances)
2. XOR all distances found in step #1
3. If the result after XORing is 0 then given 4 points are definitely vertices of a square or a rectangle otherwise, return false (given 4 points do not form a rectangle).
4. Now, to differentiate between square and rectangle 
   a. Find the largest distance out of 4 distances found in step #1. 
   b. Check if the largest distance / Math.sqrt (2) is equal to any other distance.
   c. If answer is No, then given four points form a rectangle otherwise they form a square.

在這里,我們使用了rectangle/squareBit Magic 的幾何屬性。

正在播放的矩形屬性

  1. 長方形的對邊和對角線的長度相等。
  2. 如果矩形的對角線長度是 sqrt(2) 乘以它的任何長度,則該矩形是正方形。

位魔法

  1. 對等值數進行異或運算返回 0。

由於矩形的 4 個角之間的距離將始終形成 3 對,一對用於對角線,每對不同長度的邊各兩對,因此對矩形的所有值進行異或運算將返回 0。

如果點是 A、B、C 和 D,並且您知道順序,那么您可以計算向量:

x=BA, y=CB, z=DC 和 w=AD

然后取點積 (x dot y)、(y dot z)、(z dot w) 和 (w dot x)。 如果它們都為零,那么你有一個矩形。

從一個點到另一個 3 點的距離應形成一個直角三角形:

|   /      /|
|  /      / |
| /      /  |
|/___   /___|
d1 = sqrt( (x2-x1)^2 + (y2-y1)^2 ) 
d2 = sqrt( (x3-x1)^2 + (y3-y1)^2 ) 
d3 = sqrt( (x4-x1)^2 + (y4-y1)^2 ) 
if d1^2 == d2^2 + d3^2 then it's a rectangle

簡化:

d1 = (x2-x1)^2 + (y2-y1)^2
d2 = (x3-x1)^2 + (y3-y1)^2
d3 = (x4-x1)^2 + (y4-y1)^2
if d1 == d2+d3 or d2 == d1+d3 or d3 == d1+d2 then return true

我們知道如果兩條直線的斜率乘積為-1,那么兩條直線是垂直的,因為給定了一個平面,我們可以找到三個連續直線的斜率,然后將它們相乘以檢查它們是否真的垂直。 假設我們有線 L1、L2、L3。 現在如果 L1 垂直於 L2,L2 垂直於 L3,那么它是一個矩形,斜率 m(L1)*m(L2)=-1 和 m(L2)*m(L3)=-1,那么它暗示它是一個矩形。 代碼如下

bool isRectangle(double x1,double y1,
        double x2,double y2,
        double x3,double y3,
        double x4,double y4){
    double m1,m2,m3;
    m1 = (y2-y1)/(x2-x1);
    m2 = (y2-y3)/(x2-x3);
    m3 = (y4-y3)/(x4-x3);

    if((m1*m2)==-1 && (m2*m3)==-1)
        return true;
    else
        return false;
}

將點積建議更進一步,檢查點的任意 3 個點構成的兩個向量是否垂直,然后查看 x 和 y 是否與第四個點匹配。

如果你有點 [Ax,Ay] [Bx,By] [Cx,Cy] [Dx,Dy]

向量 v = BA 向量 u = CA

v(點)u/|v||u| == cos(θ)

所以如果 (vu == 0) 那里有幾條垂直線。

我實際上不知道 C 編程,但這里有一些“元”編程給你:P

if (v==[0,0] || u==[0,0] || u==v || D==A) {not a rectangle, not even a quadrilateral}

var dot = (v1*u1 + v2*u2); //computes the "top half" of (v.u/|v||u|)
if (dot == 0) { //potentially a rectangle if true

    if (Dy==By && Dx==Cx){
     is a rectangle
    }

    else if (Dx==Bx && Dy==Cy){
     is a rectangle
    }
}
else {not a rectangle}

這沒有平方根,也沒有被零除的可能性。 我注意到人們在之前的帖子中提到了這些問題,所以我想我會提供一個替代方案。

因此,在計算上,您需要四次減法才能得到 v 和 u、兩次乘法、一次加法,並且您必須檢查 1 到 7 個等式之間的某個位置。

也許我是在編造這個,但我依稀記得在某處讀到減法和乘法是“更快”的計算。 我認為聲明變量/數組並設置它們的值也很快?

抱歉,我對這種事情很陌生,所以我很想對我剛剛寫的內容提供一些反饋。

編輯:根據我下面的評論試試這個:

A = [a1,a2];
B = [b1,b2];
C = [c1,c2];
D = [d1,d2];

u = (b1-a1,b2-a2);
v = (c1-a1,c2-a2);

if ( u==0 || v==0 || A==D || u==v)
    {!rectangle} // get the obvious out of the way

var dot = u1*v1 + u2*v2;
var pgram = [a1+u1+v1,a2+u2+v2]
if (dot == 0 && pgram == D) {rectangle} // will be true 50% of the time if rectangle
else if (pgram == D) {
    w = [d1-a1,d2-a2];

    if (w1*u1 + w2*u2 == 0) {rectangle} //25% chance
    else if (w1*v1 + w2*v2 == 0) {rectangle} //25% chance

    else {!rectangle}
}
else {!rectangle}

我最近遇到了類似的挑戰,但是在 Python 中,這是我在 Python 中提出的,也許這種方法可能很有價值。 這個想法是有 6 條線,如果創建成一個集合,應該剩下 3 個唯一的線距 - 長度、寬度和對角線。

def con_rec(a,b,c,d): 
        d1 = a.distanceFromPoint(b)
        d2 = b.distanceFromPoint(c)
        d3 = c.distanceFromPoint(d)
        d4 = d.distanceFromPoint(a)
        d5 = d.distanceFromPoint(b)
        d6 = a.distanceFromPoint(c)
        lst = [d1,d2,d3,d4,d5,d6] # list of all combinations 
        of point to point distances
        if min(lst) * math.sqrt(2) == max(lst): # this confirms a square, not a rectangle
            return False
        z = set(lst) # set of unique values in ck
        if len(lst) == 3: # there should be three values, length, width, diagonal, if a 
        4th, it's not a rectangle
            return True
        else: # not a rectangle
            return False

先驗證這4個點能不能形成平行四邊形,再看是否存在一個直角
1.驗證平行四邊形

input 4 points A, B, C, D;

if(A, B, C, D are the same points), exit;// not a rectangle;

else form 3 vectors, AB, AC, AD, verify(AB=AC+AD || AC=AB+AD || AD=AB+AC), \\\\if one of them satisfied, this is a parallelogram;

2.驗證一個直角

through the last step, we could find which two points are the adjacent points of A;

We need to find out if angle A is a right angle, if it is, then rectangle.

我不知道是否存在錯誤。 請問有沒有。

這是我的算法建議,用於軸對齊矩形測試,但在 Python 中。

這個想法是抓住第一個點作為樞軸,所有其他點必須符合相同的寬度和高度,並通過一組檢查所有點是否不同,以解決諸如 (1, 2) 之類的情況, (1, 2), (10, 30), (10, 30)。

from collections import namedtuple

Point = namedtuple('Point', ('x', 'y'))

def is_rectangle(p1, p2, p3, p4) -> bool:
    width = None
    height = None

    # All must be distinct
    if (len(set((p1, p2, p3, p4))) < 4):
        return False

    pivot = p1

    for point in (p2, p3, p4):
        candidate_width = point.x - pivot.x
        candidate_height = point.y - pivot.y

        if (candidate_width != 0):
            if (width is None):
                width = candidate_width
            elif (width != candidate_width):
                return False

        if (candidate_height != 0):
            if (height is None):
                height = candidate_height
            elif (height != candidate_height):
                return False

    return width is not None and height is not None

# Some Examples
print(is_rectangle(Point(10, 50), Point(20, 50), Point(10, 40), Point(20, 40)))
print(is_rectangle(Point(100, 50), Point(20, 50), Point(10, 40), Point(20, 40)))
print(is_rectangle(Point(10, 10), Point(20, 50), Point(10, 40), Point(20, 40)))
print(is_rectangle(Point(10, 30), Point(20, 30), Point(10, 30), Point(20, 30)))
print(is_rectangle(Point(10, 30), Point(10, 30), Point(10, 30), Point(10, 30)))
print(is_rectangle(Point(1, 2), Point(10, 30), Point(1, 2), Point(10, 30)))
print(is_rectangle(Point(10, 50), Point(80, 50), Point(10, 40), Point(80, 40)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM