阅读4点坐标。他们做一个正方形?正方形、做一个、坐标

2023-09-11 03:53:30 作者:回头望不见你

余计算点之间的距离,并且如果距离相等的点作出一个正方形,否则没有。我的code只有当我按以下顺序A(X,Y),B(X,Y),C(X,Y),D(X,Y)或反向读的坐标。但是,如果我这样写,例如A(X,Y),B(X,Y),D(X,Y),C(X,Y),它不会工作,因为测距方法计算正方形的对角线长度。我该如何解决这个问题呢?

 的#include<的iostream>
使用名字空间std;

结构{
    INT X;
    诠释Ÿ;
}一[10];

INT DIST(INT X1,Y1 INT,INT×2,INT Y2)
{
    INT C1,C2;
    C1 = X2-X1;
    C2 = Y2,Y1;
    返回(C1 * C1)+(C2 * c2)的;
}

诠释的main()
{
    INT D1,D2,D3,D4;
    的for(int i = 1; I< = 4;我++)
    {
        COUT<< X&其中;&其中; I<< =; CIN>> A [1] .X;
        COUT<< 'Y'<< I<< =; CIN>> A [1] .Y;
    }
    D1 = DIST(一个[1] .X,一个[1] .Y,一个[2] .X,一个[2] .Y);
    D2 = DIST(一个[2] .X,一个[2] .Y,一个[3] .X,一个[3] .Y);
    D3 = DIST(一个[3] .X,一个[3] .Y,一个[4] .X,一个[4] .Y);
    D4 = DIST(一个[4] .X,一个[4] .Y,一个[1] .X,一个[1] .Y);
    如果(D1 == D2及放大器;&安培; D1 = = D3和放大器;&安培; D1 = = D4)
        COUT<< 是一个正方形;
    其他
        COUT<< 是不是方;
    返回0;
}
 

解决方案

检查的距离是不够的,你需要检查至少一个角度,作为形状可以是菱形。

检查只角度也不够,因为你可以用一个长方形结束了。

共有的点之间的距离6是。计算所有的人。出于这些6,四应该是平等的(调用它们的长度 X )。 - 这保证了菱形

另外两个应该是平等的相互之间(称之为其长度)。 这保证了矩形

将一个菱形,矩形一起和 BAM - !方

I calculate the distance between the points and if the distances are equal the point make a square, else no. My code works only if I read the coordinates in the following order A(x, y), B(x, y), C(x, y), D(x, y) or reverse. But if I read like this for example A(x, y), B(x, y), D(x, y), C(x, y) it won't work because the dist method will calculate the square's diagonal length. How can I solve this problem?

#include <iostream>
using namespace std;

struct {
    int x;
    int y;
}a[10];

int dist(int x1, int y1, int x2, int y2)
{
    int c1, c2;
    c1 = x2-x1;
    c2 = y2-y1;
    return (c1*c1)+(c2*c2);
}

int main()
{
    int d1, d2, d3, d4;
    for (int i=1; i<=4; i++)
    {
        cout << 'X' << i << '='; cin >> a[i].x;
        cout << 'Y' << i << '='; cin >> a[i].y;
    }
    d1 = dist(a[1].x, a[1].y, a[2].x, a[2].y);
    d2 = dist(a[2].x, a[2].y, a[3].x, a[3].y);
    d3 = dist(a[3].x, a[3].y, a[4].x, a[4].y);
    d4 = dist(a[4].x, a[4].y, a[1].x, a[1].y);
    if(d1==d2 && d1==d3 && d1==d4)
        cout << "Is a square";
    else
        cout << "Is not a square";
    return 0;
}
如图.在正方形网格中.每个小正方形的边长都为1.点A.点B在网格中的位置如图所示. 1 建立适当的平面直角坐标系.使点A.点B的坐标分别为,.在平面直角坐标系中标出点

解决方案

Checking the distances is not enough, you'll need to check at least an angle, as the shape could be a rhombus.

Checking only angles is also not enough, because you could end up with a rectangle.

There are a total of 6 distances between the points. Calculate all of them. Out of those 6, four should be equal (call their length x). - this guarantees a rhombus

The other two should be equal between themselves (call their length y). this guarantees a rectangle

Put a rhombus and a rectangle together and BAM! - square.