uva 11800 Determine the Shape

  vjudge上题目链接:Determine the Shape

  第二道独自 A 出的计算几何水题,题意就是给你四个点,让你判断它是哪种四边形:正方形、矩形、菱形、平行四边形、梯形 or 普通四边形。

  按照各个四边形的特征层层判断就行,只是这题四个点的相对位置不确定(点与点之间是相邻还是相对并不确定),所以需要枚举各种情况,幸好 4 个点一共只有 3 种不同的情况:abcd、abdc、acbd 画个图就能一目了然,接下来就是编码了,无奈因为一些细节问题我还是调试了还一会 o(╯□╰)o

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 struct Vector {
 8     double x,y;
 9     Vector(double x = 0, double y = 0): x(x), y(y) {}
10     void readint() {
11         int x,y;
12         scanf("%d %d",&x,&y);
13         this->x = x;
14         this->y = y;
15     }
16     Vector operator + (const Vector &b) const {
17         return Vector(x + b.x, y + b.y);
18     }
19     Vector operator - (const Vector &b) const {
20         return Vector(x - b.x, y - b.y);
21     }
22     Vector operator * (double p) const {
23         return Vector(x * p, y * p);
24     }
25     Vector operator / (double p) const {
26         return Vector(x / p, y / p);
27     }
28 };
29
30 typedef Vector point;
31 typedef const point& cpoint;
32 typedef const Vector& cvector;
33
34 Vector operator * (double p, const Vector &a) {
35     return a * p;
36 }
37
38 double dot(cvector a, cvector b) {
39     return a.x * b.x + a.y * b.y;
40 }
41
42 double length(cvector a) {
43     return sqrt(dot(a,a));
44 }
45
46 double cross(cvector a, cvector b) {
47     return a.x * b.y - a.y *b.x;
48 }
49
50 const double eps = 1e-10;
51 int dcmp(double x) {
52     return fabs(x) < eps ? 0: (x < 0 ? -1 : 1);
53 }
54
55 bool operator == (cpoint a, cpoint b) {
56     return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
57 }
58
59 const char s[8][30] = {"Ordinary Quadrilateral", "Trapezium", "Parallelogram",
60                 "Rhombus", "Rectangle", "Square" };
61
62 int judge(cpoint a, cpoint b, cpoint c, cpoint d) {
63     if(dcmp(cross(a - b, c - d)) == 0) {
64         if(dcmp(cross(b - c, a - d)) == 0) {
65             if(dcmp(length(b - a) - length(d - a)) == 0) {
66                 if(dcmp(dot(b - a, d - a)) == 0)    return 5;
67                 else    return 3;
68             }
69             else if(dcmp(dot(b - a, d - a)) == 0)   return 4;
70             else    return 2;
71         }
72         else   return 1;
73     }
74     else if(dcmp(cross(b - c, a - d)) == 0)    return 1;
75     else    return 0;
76 }
77
78 int main() {
79     int t,Case = 0;
80     point a,b,c,d;
81     scanf("%d",&t);
82     while(t--) {
83         a.readint();
84         b.readint();
85         c.readint();
86         d.readint();
87         printf("Case %d: ",++Case);
88         int ans = 0;
89         ans = max(ans, judge(a,b,c,d));
90         ans = max(ans, judge(a,b,d,c));
91         ans = max(ans, judge(a,c,b,d));
92         printf("%s\n",s[ans]);
93     }
94     return 0;
95 }

  计算几何,还是很有趣的。。。

时间: 2024-10-09 20:03:42

uva 11800 Determine the Shape的相关文章

UVA 11800 Determine the Shape --凸包第一题

题意: 给四个点,判断四边形的形状.可能是正方形,矩形,菱形,平行四边形,梯形或普通四边形. 解法: 开始还在纠结怎么将四个点按序排好,如果直接处理的话,有点麻烦,原来凸包就可搞,直接求个凸包,然后点就自动按逆时针排好了,然后就判断就可以了,判断依据题目下面有,主要是用到点积和叉积,判断垂直用点积,判断平行用叉积. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstd

UVA11800 - Determine the Shape

每次给4个点坐标,确保没有三点共线,判断构成的是个什么四边形就行了,并输出类型. 我的做法: 以一个点为极点进行极角排序使得点都是逆时针排列 用点积和叉积判断线与线的关系 我的代码: #include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #i

几何基础专题

UVA 11437 Triangle Fun UVA 11800 Determine the Shape 四边形判定 UVA 11646 Athletics Track UVA 11817 Tunnelling the Earth 球面距离 UVA 1473 Dome of Circus UVA 11524 InCircle UVA 11731 Ex-circles 旁切圆 UVA 12300 Smallest Regular Polygon UVA 10566 Crossed Ladders

[UVA]11800-Determine the Shape(计算几何)

这道题比较基础,方法也比较多,我的话是使用了向量法进行计算. 任意枚举3个点,看这3个点确定的3个向量和第四个点是否构成一个平行四边形,如果是平行四边形,再进行特殊图形的判断. 14118653 11800 Determine the Shape Accepted C++ 0.102 2014-08-30 13:31:48 #include <iostream> #include <cstdlib> #include <cstdio> #include <stri

ArcGIS Engine开发的ArcGIS 版本管理的功能

原文:ArcGIS Engine开发的ArcGIS 版本管理的功能 转自:http://blog.csdn.net/linghe301/article/details/7965901 这是以前的ArcGIS Engine开发成果,主要是Geodatabase方面的,模仿ArcGIS版本的流程系统环境: VS2010.ArcGIS Engine10.DevExpress721(第三方控件,比较常用易于下载) ---------------------------------------------

我们需要解决的机器学习问题

From:http://machinelearningmastery.com/practical-machine-learning-problems/ Practical Machine Learning Problems What is Machine Learning? We can read authoritative definitions of machine learning, but really, machine learning is defined by the proble

Polygon Collider 2D

The Polygon Collider 2D component is a collider for use with 2D physics. The collider's shape is defined by a freeform edge made of line segments, so you can adjust it to fit the shape of the Sprite graphic with great precision. Note that this collid

Video processing systems and methods

BACKGROUND The present invention relates to video processing systems. Advances in imaging technology have led to high resolution cameras for personal use as well as professional use. Personal uses include digital cameras and camcorders that can captu

使用贝塞尔曲线进行插值 一种非常简单的平滑多边形的方法

原文Interpolation with Bezier Curves A very simple method of smoothing polygons Initially, there was a question in comp.graphic.algorithms how to interpolate a polygon with a curve in such a way that the resulting curve would be smooth and hit all its