UVA 11800 Determine the Shape --凸包第一题
题意: 给四个点,判断四边形的形状。可能是正方形,矩形,菱形,平行四边形,梯形或普通四边形。
解法: 开始还在纠结怎么将四个点按序排好,如果直接处理的话,有点麻烦,原来凸包就可搞,直接求个凸包,然后点就自动按逆时针排好了,然后就判断就可以了,判断依据题目下面有,主要是用到点积和叉积,判断垂直用点积,判断平行用叉积。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define eps 1e-8
using namespace std; struct Point{
double x,y;
Point(double x=, double y=):x(x),y(y) {}
void input() { scanf("%lf%lf",&x,&y); }
};
typedef Point Vector;
struct Circle{
Point c;
double r;
Circle(){}
Circle(Point c,double r):c(c),r(r) {}
Point point(double a) { return Point(c.x + cos(a)*r, c.y + sin(a)*r); }
void input() { scanf("%lf%lf%lf",&c.x,&c.y,&r); }
};
struct Line{
Point p;
Vector v;
double ang;
Line(){}
Line(Point p, Vector v):p(p),v(v) { ang = atan2(v.y,v.x); }
Point point(double t) { return Point(p.x + t*v.x, p.y + t*v.y); }
bool operator < (const Line &L)const { return ang < L.ang; }
};
int dcmp(double x) {
if(x < -eps) return -;
if(x > eps) return ;
return ;
}
template <class T> T sqr(T x) { return x * x;}
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Vector A, Vector 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); }
bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ; }
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)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
Vector VectorUnit(Vector x){ return x / Length(x);}
Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);}
double angle(Vector v) { return atan2(v.y, v.x); }
int ConvexHull(Point* p, int n, Point* ch)
{
sort(p,p+n);
int m = ;
for(int i=;i<n;i++) {
while(m > && Cross(ch[m-]-ch[m-], p[i]-ch[m-]) <= ) m--;
ch[m++] = p[i];
}
int k = m;
for(int i=n-;i>=;i--) {
while(m > k && Cross(ch[m-]-ch[m-], p[i]-ch[m-]) <= ) m--;
ch[m++] = p[i];
}
if(n > ) m--;
return m;
} //data segment
Point p[],ch[];
Point A,B,C,D;
//data ends int main()
{
int t,n,i,cs = ;
scanf("%d",&t);
while(t--)
{
for(i=;i<;i++) p[i].input();
printf("Case %d: ",cs++);
int m = ConvexHull(p,,ch);
if(m < ) { puts("Ordinary Quadrilateral"); continue; }
A = ch[], B = ch[], C = ch[], D = ch[]; if(dcmp(Dot(B-A,D-A)) == && dcmp(Dot(B-A,C-B)) == && dcmp(Dot(C-B,C-D)) == && dcmp(Dot(D-C,D-A)) ==
&& dcmp(Length(B-A)-Length(C-B)) == && dcmp(Length(C-B)-Length(D-C)) == && dcmp(Length(C-D)-Length(A-D)) == )
puts("Square");
else if(dcmp(Dot(B-A,D-A)) == && dcmp(Dot(B-A,C-B)) == && dcmp(Dot(C-B,C-D)) == && dcmp(Dot(D-C,D-A)) ==
&& dcmp(Length(A-D)-Length(C-B)) == && dcmp(Length(A-B)-Length(C-D)) == )
puts("Rectangle");
else if(dcmp(Length(B-A)-Length(C-B)) == && dcmp(Length(C-B)-Length(D-C)) == && dcmp(Length(C-D)-Length(A-D)) == )
puts("Rhombus");
else if(dcmp(Length(A-D)-Length(B-C)) == && dcmp(Length(A-B)-Length(C-D)) == )
puts("Parallelogram");
else if(dcmp(Cross(B-C,D-A)) == || dcmp(Cross(B-A,D-C)) == )
puts("Trapezium");
else
puts("Ordinary Quadrilateral");
}
return ;
}
UVA 11800 Determine the Shape --凸包第一题的更多相关文章
- uva 11800 Determine the Shape
vjudge上题目链接:Determine the Shape 第二道独自 A 出的计算几何水题,题意就是给你四个点,让你判断它是哪种四边形:正方形.矩形.菱形.平行四边形.梯形 or 普通四边形. ...
- 简单几何(四边形形状) UVA 11800 Determine the Shape
题目传送门 题意:给了四个点,判断能构成什么图形,有优先规则 分析:正方形和矩形按照点积为0和长度判断,菱形和平行四边形按向量相等和长度判断,梯形按照叉积为0判平行.因为四个点是任意给出的,首先要进行 ...
- UVA 11800 - Determine the Shape 几何
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- hdu 1348 Wall(凸包模板题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 Wall Time Limit: 2000/1000 MS (Java/Others) M ...
- [算法 笔记]2014年去哪儿网 开发笔试(续)第一题BUG修正
上一篇的blog地址为:http://www.cnblogs.com/life91/p/3313868.html 这几天又参加了一个家公司的笔试题,在最后的编程题中竟然出现了去哪儿网开发的第一题,也就 ...
- 《学习OpenCV》练习题第五章第一题ab
这道题是载入一幅带有有趣纹理的图像并用不同的模板(窗口,核)大小做高斯模糊(高斯平滑),然后比较用5*5大小的窗口平滑图像两次和用11*11大小的窗口平滑图像一次是否接近相同. 先说下我的做法,a部分 ...
- 《学习OpenCV》练习题第四章第一题b&c
#include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...
- 《学习OpenCV》练习题第四章第一题a
#include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...
- Google Code Jam 第一题
通过的第一题,留做纪念,呵呵,非常简单,Africa 2010, Qualification Round: Store Credit. #include <stdio.h> #includ ...
随机推荐
- WCF的三个名称/命名空间,你是否傻傻分不清楚?
在定义和寄宿WCF服务的时候会面临三个名称/命名空间,它们分别是ServiceContractAttribute.ServiceBehaviorAttribute和Binding的Name和Names ...
- 【服务器】CentOS下部署运行NodeJs Web App
NodeJs Web App测试完成后,要怎么部署呢?介绍两个不错的方案 已知以下情景: 我要为 「kenniu」这个项目做配置 它的入口文件在 「/path/to/entry.js」 运行的User ...
- AloneJs.msgbox() —— 弹出消息框
一.引用 <link href="https://cdn.suziyun.com/alonejs.min.css" rel="stylesheet" /& ...
- 分享一个我的JavaScript版GridView多功能表格
GridView是什么? GridView是由Mr.Co开发的一套开源的多功能表格插件,主要用于让页面开发者在开发中节省拼接Table表格和操作Table表格相关复杂操作的开发成本与时间.开发人员可以 ...
- Java虚拟机JVM学习06 自定义类加载器 父委托机制和命名空间的再讨论
Java虚拟机JVM学习06 自定义类加载器 父委托机制和命名空间的再讨论 创建用户自定义的类加载器 要创建用户自定义的类加载器,只需要扩展java.lang.ClassLoader类,然后覆盖它的f ...
- 百度地图SDK 遇到的问题及解决方案
目前项目工作中用到了百度地图sdk,遇到了不少问题,在此记录一下,顺便吐槽下希望百度能把这地图sdk做的更好用一点. 1,开发环境, Xcode6.0 (6A313) + 百度地图 iOS SDK v ...
- MySQL如何发型不乱的应对半年数十TB数据增量
➠更多技术干货请戳:听云博客 前段时间,Oracle官方发布了MySQL 5.7的GA版本.新版本中实现了真正意义的并行复制(基于Group Commit的Group Replication),而不 ...
- myIsEqualToString
BOOL myisEqualToString(NSString * str1 , NSString * str2){ //1.如果两个字符串,指针地址相等,就说明一定是相等 if(str1 == st ...
- 基于Metaweblog API 接口一键发布到国内外主流博客平台
之前的生活 之前一直使用evenote写博客和日志,其实还是挺方便的.但是我一直都希望能够同步到国内的博客和国外的blogspot等主流博客平台.而强大everote只提供了facebook.twit ...
- 什么是Java实例初始化块
在本篇文章,我将会使用一个例子展示什么是实例变量初始化块,实例初始化块和静态初始化块,然后说明在Java中实例初始化块是如何工作的. 执行顺序 查看下面的代码,你知道哪个先执行吗? package s ...