码迷,mamicode.com
首页 > 其他好文 > 详细

uva 11800 Determine the Shape

时间:2015-08-01 17:07:05      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:

  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 }
View Code

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

uva 11800 Determine the Shape

标签:

原文地址:http://www.cnblogs.com/Newdawn/p/4694308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!