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

作者: 希望每天涨粉

计算几何(判断四边形形状) - 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; }

原文创作:希望每天涨粉

原文链接:https://www.cnblogs.com/BlairGrowing/p/13926424.html

更多推荐

更多
  • Go编程秘籍-三、数据转换与组合 本章将展示一些在数据类型之间转换、使用非常大的数字、使用货币、使用不同类型的编码和解码(包括 Base64 和gob)以及使用闭包创建自定义集合的示例。转换数据类型和接口转换,使用 math 和 math/big ...
  • Go编程秘籍-一、I/O 和文件系统 Go 为基本和复杂 I/O 提供了极好的支持。本章中的方法将探索用于处理 I/O 的常见 Go 接口,并向您展示如何使用它们。Go 标准库经常使用这些接口,本书中的食谱都将使用这些接口。本章将介绍以下配方:使用公共 I/O ...
  • Go编程秘籍-二、命令行工具 命令行应用是处理用户输入和输出的最简单方法之一。本章将重点介绍基于命令行的交互,如命令行参数、配置和环境变量。最后,我们将介绍一个在 Unix 和 Bash for Windows 中为文本输出着色的库。
  • Go编程秘籍-零、前言 要使用本书,您需要以下内容:Unix 编程环境。Go 1.x 系列的最新版本。互联网连接。如各章所述,允许安装其他软件包。各章节的技术要求部分中提到了各配方的先决条件和其他安装要求。
  • Go编程秘籍-十一、分布式系统 本章将探讨管理分布式数据、编排、容器化、度量和监视的方法。这些将成为编写和维护微服务和大型分布式应用工具箱的一部分。在本章中,我们将介绍以下配方:与 concur 一起使用服务发现,利用 Raft 实现基本共识,与 Docker ...
  • Go编程秘籍-十、并行与并发 Go 提供了使并行应用成为可能的原语。Goroutines 允许任何函数变成异步和并发的。通道允许应用设置与 Goroutines 的通信。在本章中,我们将介绍以下配方:使用通道和 select 语句,使用 sync.WaitGroup ...
  • Go编程秘籍-十二、反应式编程和数据流 本章还将探讨与卡夫卡联系的各种方式,并使用它来处理信息。最后,本章将演示如何在 Go 中创建一个基本的graphql服务器。在本章中,我们将介绍以下配方:使用 Goflow 进行数据流编程,与 Kafka 一起使用异步生产者,将卡夫卡连接到...
  • Go编程秘籍-九、测试 Go 代码 本章将分享一些测试 Go 代码的方法。在本章中,我们将介绍以下配方:使用标准库进行模拟,使用 Mockgen 包模拟接口,使用表驱动测试提高覆盖率,使用第三方测试工具,使用 Go 进行行为测试,在 Go ...
  • Go编程秘籍-十三、无服务器编程 本章将重点介绍无服务器架构,并将其与 Go 语言结合使用。无服务器架构是开发人员不管理后端服务器的架构。这包括亚马逊 Lambda、谷歌应用引擎和 Firebase 等服务。这些服务允许您在 web 上快速部署应用和存储数据。Apex ...
  • Go编程秘籍-五、网络编程 Go 标准库为网络操作提供了大量支持。它包括允许您使用 HTTP 管理 TCP/IP、UDP、DNS、邮件和 RPC 的包。包括gorilla/websockets,用于可在正常 HTTP 处理程序中使用的 WebSocket ...
  • 近期文章

    更多
    文章目录

      推荐作者

      更多