ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

计算几何(判断四边形形状)

2021-09-08 17:01:21  阅读:218  来源: 互联网

标签:ang return Length 形状 Vector dcmp 几何 四边形 Line


计算几何(判断四边形形状) - Determine the Shape - UVA 11800

题意:

给 定 四 个 点 坐 标 , 判 断 四 边 形 形 状 。 给定四个点坐标,判断四边形形状。给定四个点坐标,判断四边形形状。

输入:

T 组 测 试 数 据 , T组测试数据,T组测试数据,

每 组 包 括 四 个 点 的 坐 标 。 每组包括四个点的坐标。每组包括四个点的坐标。

输出:

四 边 形 形 状 。 四边形形状。四边形形状。

Sample Input

6
0 0
2 0
2 2
0 2
0 0
3 0
3 2
0 2
0 0
8 4
5 0
3 4
0 0
2 0
3 2
1 2
0 0
5 0
4 3
1 3
0 0
5 0
4 3
1 4

Sample Output

Case 1: Square
Case 2: Rectangle
Case 3: Rhombus
Case 4: Parallelogram
Case 5: Trapezium
Case 6: Ordinary Quadrilateral

代码:

#include<iostream>
#include<cmath>
#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

const double eps=1e-10;
const double pi=acos(-1.0);

struct Point 
{
    double x,y;
    Point(double x=0,double y=0) : x(x), y(y) {}
};

//点与向量
typedef Point Vector;

Vector operator + (Vector A,Vector B) { return Vector(A.x+B.x,A.y+B.y); }

Vector operator - (Point A,Point B) { return Vector(A.x-B.x,A.y-B.y); }

Vector operator * (Vector A,double p) { return Vector(A.x*p,A.y*p); }

Vector operator / (Vector A,double p) { return Vector(A.x/p,A.y/p); }

bool operator < (const Point &a,const Point &b)
{
    return a.x<b.x || (a.x==b.x && a.y<b.y);
}

int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    else return x<0 ? -1 : 1;
}

bool operator == (const Point &a, const Point &b)
{
    return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0;
}

double Dot(Vector A,Vector B) { return A.x*B.x + A.y*B.y; }

double Length(Vector A) { return sqrt(Dot(A,A)); }

double Angle(Vector A, Vector B) { return acos(Dot(A,B) / Length(A) / Length(B)); } //A和B夹角

double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }  //若A到B逆时针则为正,否则为负

double Area2(Point A,Point B,Point C) { return Cross(B-A,C-A); }    //三角形ABC的面积的两倍(有方向)

//点和直线
struct Line
{//直线定义
    Point v, p;
    Vector dir;
    double ang;
    
    Line() { }  //构造函数  
    Line(const Line& L): p(L.p), dir(L.dir), ang(L.ang) { }  
    Line(Point v, Point p):v(v), p(p){ dir=p-v; ang=atan2(dir.y,dir.x); }
    bool operator < (const Line& L) const //极角排序 
    {  
        return ang < L.ang;  
    } 
    Point point(double t)
    {//返回点P = v + (p - v)*t
        return v + dir*t;
    }
};

typedef vector<Point> Polygon;  

//平行四边形的判定(保证四边形顶点按顺序给出)  
bool isParallelogram(Polygon p) {  
    if (dcmp(Length(p[0]-p[1]) - Length(p[2]-p[3])) || dcmp(Length(p[0]-p[3]) - Length(p[2]-p[1]))) return false;  
    Line a = Line(p[0], p[1]);  
    Line b = Line(p[1], p[2]);  
    Line c = Line(p[3], p[2]);  
    Line d = Line(p[0], p[3]);  
    return dcmp(a.ang - c.ang) == 0 && dcmp(b.ang - d.ang) == 0;  
}  
  
//梯形的判定  
bool isTrapezium(Polygon p) {  
    Line a = Line(p[0], p[1]);  
    Line b = Line(p[1], p[2]);  
    Line c = Line(p[3], p[2]);  
    Line d = Line(p[0], p[3]);  
    return (dcmp(a.ang - c.ang) == 0 && dcmp(b.ang - d.ang)) || (dcmp(a.ang - c.ang) && dcmp(b.ang - d.ang) == 0);  
}  
  
//菱形的判定  
bool isRhombus(Polygon p) {  
    if (!isParallelogram(p)) return false;  
    return dcmp(Length(p[1]-p[0]) - Length(p[2]-p[1])) == 0;  
}  
  
//矩形的判定  
bool isRectangle(Polygon p) {  
    if (!isParallelogram(p)) return false;  
    return dcmp(Length(p[2]-p[0]) - Length(p[3]-p[1])) == 0;  
}  
  
//正方形的判定  
bool isSquare(Polygon p) {  
    return isRectangle(p) && isRhombus(p);  
} 

int check(Polygon p)
{
    if(isSquare(p)) return 1;
    else if(isRectangle(p)) return 2;
    else if(isRhombus(p)) return 3;
    else if(isParallelogram(p)) return 4;
    else if(isTrapezium(p)) return 5;
    else return 6;
}

int main()
{
    int T;
    scanf("%d",&T);
    for(int C=1;C<=T;C++)
    {
        Polygon p;
        Point tmp;
        for(int i=0;i<4;i++) {scanf("%lf%lf",&tmp.x,&tmp.y); p.push_back(tmp);}
        int t=check(p);//V[0],V[1],V[2],V[3];
        swap(p[1],p[2]);
        t=min(t,check(p));//V[0],V[2],V[1],V[3];
        swap(p[1],p[2]);
        swap(p[2],p[3]);
        t=min(t,check(p));//V[0],V[1],V[3],V[2];
        
        printf("Case %d: ",C);
        if(t==1) puts("Square");
        else if(t==2) puts("Rectangle");
        else if(t==3) puts("Rhombus");
        else if(t==4) puts("Parallelogram");
        else if(t==5) puts("Trapezium");
        else puts("Ordinary Quadrilateral");
    }
    return 0;
}

 

标签:ang,return,Length,形状,Vector,dcmp,几何,四边形,Line
来源: https://www.cnblogs.com/BlairGrowing/p/13926424.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有