[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 <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
const double eps = 1e-10;
struct Point
{
	double x, y;
	Point(double x = 0, double y = 0) : x(x), y(y) { }
	bool operator < (const Point& a) const
	{
		if(a.x != x) return x < a.x;
		return y < a.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); }
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 Dist(Point A,Point B){return (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (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; }
double Area2(Point A, Point B, Point C) { return Cross(B-A, C-A); }
Vector Rotate(Vector A, double rad)
{
	return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad) + A.y*cos(rad));
}
Point GetIntersection(Point P, Vector v, Point Q, Vector w)
{
	Vector u = P-Q;
	double t = Cross(w, u) / Cross(v, w);
	return P+v*t;
}
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2)
{
	double c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1);
	double c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1);
	return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
}
double PolygonArea(Point* p, int n)
{
	double area = 0;
	for(int i = 1; i < n-1; i++)
		area += Cross(p[i]-p[0], p[i+1]-p[0]);
	return area;
}
bool OnSegment(Point p, Point a1, Point a2)
{
	return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0;
}
//Square                    正方形
//Rectangle                 长方形
//Rhombus                   菱形
//Parallelogram            平行四边形
//Trapezium                 梯形
//Ordinary Quadrilateral    一般四边形
Point P[5];
char result[10][30];
void init(){
    strcpy(result[1],"Square");
    strcpy(result[2],"Rectangle");
    strcpy(result[3],"Rhombus");
    strcpy(result[4],"Parallelogram");
    strcpy(result[5],"Trapezium");
    strcpy(result[6],"Ordinary Quadrilateral");
}
int Judge(Point a,Point b,Point c,Point d){
    Vector v1 = b - a;
    Vector v2 = c - a;
    Vector v3 = v1 + v2;
    if(v3 == (d - a)){   //确定是一个平行四边形
        double L1 = Dist(a,b);
        double L2 = Dist(a,c);
        double  k = Dot(v1,v2);
        if(!dcmp(L1 - L2) && !dcmp(k))
            return 1;
        if(!dcmp(k))
            return 2;
        if(!dcmp(L1 - L2))
            return 3;
        else
            return 4;
    }
    else
        return 0;
}
int Judge2(Point a,Point b,Point c,Point d){
    Vector ab = b - a;
    Vector bc = c - b;
    Vector cd = d - c;
    Vector da = a - d;
    double ans1 = Cross(ab,cd);
    double ans2 = Cross(bc,da);
    if(!dcmp(ans1) || !dcmp(ans2))
        return 1;
    else
        return 0;
}
int solve(){
    int ans;
    ans = Judge(P[1],P[2],P[3],P[4]);
    if(ans)
        return ans;
    ans = Judge(P[1],P[3],P[4],P[2]);
    if(ans)
        return ans;
    ans = Judge(P[1],P[2],P[4],P[3]);
    if(ans)
        return ans;
    else{
        int ok = 0;
        ok = Judge2(P[1],P[2],P[3],P[4]);
        if(ok) return 5;
        ok = Judge2(P[1],P[3],P[4],P[2]);
        if(ok) return 5;
        ok = Judge2(P[1],P[2],P[4],P[3]);
        if(ok) return 5;
    }
    return 6;
}
int main(){
    init();
    int T,Case = 1;
    scanf("%d",&T);
    while(T--){
        for(int i = 1 ; i <= 4 ; i++)
            scanf("%lf%lf",&P[i].x,&P[i].y);
        int ans = solve();
        printf("Case %d: %s\n",Case ++ ,result[ans]);
    }
    return 0;
}
时间: 2024-12-28 17:18:23

[UVA]11800-Determine the Shape(计算几何)的相关文章

uva 11800 Determine the Shape

vjudge上题目链接:Determine the Shape 第二道独自 A 出的计算几何水题,题意就是给你四个点,让你判断它是哪种四边形:正方形.矩形.菱形.平行四边形.梯形 or 普通四边形. 按照各个四边形的特征层层判断就行,只是这题四个点的相对位置不确定(点与点之间是相邻还是相对并不确定),所以需要枚举各种情况,幸好 4 个点一共只有 3 种不同的情况:abcd.abdc.acbd 画个图就能一目了然,接下来就是编码了,无奈因为一些细节问题我还是调试了还一会 o(╯□╰)o 1 #in

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

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

UVA 11178 Morley&#39;s Theorem 计算几何

计算几何: 最基本的计算几何,差积  旋转 Morley's Theorem Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem D Morley's Theorem Input: Standard Input Output: Standard Output Morley's theorem states that that the line

UVA11800 - Determine the Shape

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

uva 11796 Dog Distance (计算几何-点和直线)

C Dog Distance Input Standard Input Output Standard Output Two dogs, Ranga and Banga, are running randomly following two different paths. They both run for T seconds with different speeds. Ranga runs with a constant speed of R m/s, whereas Banga runs

uva 10652 Board Wrapping (计算几何-凸包)

Problem B Board Wrapping Input: standard input Output: standard output Time Limit: 2 seconds The small sawmill in Mission, British Columbia, has developed a brand new way of packaging boards for drying. By fixating the boards in special moulds, the b

UVA - 11437 - Triangle Fun (计算几何~)

UVA - 11437 Triangle Fun Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem A Triangle Fun Input: Standard Input Output: Standard Output In the picture below you can see a triangle ABC. Point D, E

UVA - 11796 - Dog Distance (计算几何~)

不得不感叹,计算几何真是太美丽了!! AC代码: #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <cmath> using namespace std; struct Point { double x, y; Point(double x = 0, double y = 0) : x(x) , y(y) { } }; t

UVA - 11646 - Athletics Track (计算几何~)

题目地址:点这里 思路:计算几何入门题,首先,两个圆弧是同一个圆的,所以这个圆是矩形的外接圆,那么矩形中心就是圆心,由长宽算出角度和半径(这时用单位长度表示),再算出一个单位长度的实际长度,从而得出长和宽 AC代码: #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <cmath> using namespace std;