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 ...
随机推荐
- Welcome Phalcon
Welcome! 欢迎来到 Phalcon 框架, 一种崭新的 PHP 框架.我们的使命是给开发者一个开发 web 站点和应用的高级工具,让开发者不用担心框架的性能问题. Phalcon 是什么? P ...
- ionic + cordova 使用 cordova-plugin-crosswalk-webview 中的一些个坑
1) 在使用Web Audio API 时,无法使用 AudioContext.decodeAudioData() 对MP3文件进行解码 2)使用Cordova-plugin-weibosdk 插件时 ...
- Flex Viewer(三)——Config的原理
一.概述 在上文<深入浅出Flex Viewer(二)——体系结构>中,笔者详细介绍了到Flex Viewer框架,使得读者能够对该框架源代码的关键目录和文件结构和这些文件中所包含或涉及到 ...
- ArcGisServer根据最大最小坐标换算瓦片行列号
1.前言 在上一节中我们知道了屏幕上一像素等于实际中多少单位长度(米或经纬度)的换算方法,而知道这个原理后,接下来我们要怎么用它呢?它和我们前端显示地图有什么关联呢?这一节,我会尽量详细的将这两个问题 ...
- Android 在C代码中调用logcat
本文给<Android java传递int类型数组给C>中添加C代码中调用logcat的功能 Android.mk文件增加以下内容 LOCAL_LDLIBS += -llog C代码中增加 ...
- 干货之UIButton的title和image自定义布局
当需要实现一个自定义布局图片和标题的按钮时候,不知道有多少少年直接布局了UIButton,亦或是自定义一个UIView,然后以空白UIButton.UILabel.UIImageVew作为subVie ...
- OC中的面向对象语法4
一. 继承 1. 继承的基本用法 l 设计两个类Bird.Dog // Bird的声明 @interface Bird : NSObject { @public int weight; } - (vo ...
- Asp.Net MVC 自定义的MVC框架(非EF操作数据库)
一些废话:在北京辞职回家不知不觉中已经半年多了,这半年中有过很多的彷徨,困惑,还有些小小难受.半年时间算是我人生以来遇到过的最困苦的时候.理想的工作跟我擦肩而过,驾照也没有考过,年后这一改革...,毕 ...
- 17、文案人员 - IT软件人员书籍系列文章
这里说的文案人员是软件项目中的一个角色.其主要负责相关文档的整理,用户使用说明书的编写等等,在项目中是一个辅助的角色. 文案人员所做的事情不错,但是她能够辅助软件配置管理工程师进行工作,更好的为维护文 ...
- 【转发】揭秘Facebook 的系统架构
揭底Facebook 的系统架构 www.MyException.Cn 发布于:2012-08-28 12:37:01 浏览:0次 0 揭秘Facebook 的系统架构 www.MyExcep ...