zoukankan      html  css  js  c++  java
  • C++判断四个点能否构成正方形/矩阵

    判断正方形方法:三个条件同时满足(1:四条边相等,2:边不为0,3:有一个直角)

    判断矩形的话就是条件1变为有2对边相等

    #include<iostream>  
    #include<algorithm>  
    #include<cmath>  
    using namespace std;
    double eps=1e-6;
    struct point
    {
    	double x, y;
    } a[4];
    
    bool cmp(point a, point b)
    {
    	if (a.x != b.x)
    		return a.x < b.x; //如果,横坐标不相等,所有点按横坐标升序排列
    	return a.y < b.y;//如果横坐标相等,所有点按纵坐标升序排列
    }
    
    double TwoPointDiatance(point a, point b)//计算两点之间的距离
    {
    	return sqrt(pow((a.x - b.x), 2) + pow((a.y - b.y), 2));
    }
    
    bool IsRightAngle(point a, point b, point c)//判断是否为直角
    {
    	double x;
    	x = (a.x - b.x)* (a.x - c.x) + (a.y - b.y)*(a.y - c.y);
    	if (fabs(x)<eps)
    		return 1;
    	else
    		return 0;
    }
    
    int main()
    {
    	int t, k;
    	double s1, s2, s3, s4;
    	cout << "请输入您想要玩的次数: ";
    	cin >> t;
    	cout << "输入4个点的坐标:" << endl;
    	while (t--)
    	{
    		for (int i = 0; i < 4; i++)
    			cin >> a[i].x >> a[i].y;
    
    		//确定点,排序,给点确定标号
    		sort(a, a + 4, cmp);
    
    		//确定边
    		s1 = TwoPointDiatance(a[0], a[2]);
    		s2 = TwoPointDiatance(a[0], a[1]);
    		s3 = TwoPointDiatance(a[3], a[1]);
    		s4 = TwoPointDiatance(a[2], a[3]);
    
    		//分析是否为正方形
    		if (s1 == s2&&s3 == s4&&s1 == s3&&s1 != 0 && IsRightAngle(a[0], a[1], a[2]))//三个条件同时满足(1:四条边相等,2:边不为0,3:有一个直角)
    			cout << "Yes" << endl;
    		else
    			cout << "No" << endl;
    		cout << "还剩 " << t << " 次。" << endl;
    		cout << "输入4个点的坐标:" << endl;
    	}
    
    	return 0;
    }
    /*
    几组正方形测试坐标:
    0 1 1 1 1 0 0 0
    
    0 2 3 -2 -1 -5 -4 -1
    
    0 4 4 7 7 3 3 0
    
    0 1 1 6 5 0 6 5
    */
    
    •  
  • 相关阅读:
    bzoj1045: [HAOI2008] 糖果传递(数论)
    bzoj1083: [SCOI2005]繁忙的都市(最小生成树)
    bzoj1079: [SCOI2008]着色方案(DP)
    BZOJ2467 [中山市选2010]生成树
    BZOJ4766 文艺计算姬
    BZOJ4894 天赋
    BZOJ2560 串珠子
    [SDOI2014]重建
    BZOJ3622 已经没有什么好害怕的了
    [SDOI2016]储能表
  • 原文地址:https://www.cnblogs.com/eason9906/p/11754734.html
Copyright © 2011-2022 走看看