UVa 11800 Determine the Shape(几何)

题目链接:UVa 11800  Determine the Shape

几何,考察各个图形的判定定理。

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

const double eps = 1e-10;

struct Point
{
	double x, y;
	Point(double x=0, double y=0):x(x),y(y) { }
};

typedef Point Vector;

Vector operator + (const Vector& A, const Vector& B)
{
    return Vector(A.x+B.x, A.y+B.y);
}

Vector operator - (const Point& A, const Point& B)
{
    return Vector(A.x-B.x, A.y-B.y);
}

Vector operator * (const 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);
}

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(const Vector& A, const Vector& B)
{
    return A.x*B.x + A.y*B.y;
}

double Length(const Vector& A)
{
    return sqrt(Dot(A, A));
}

double Angle(const Vector& A, const Vector& B)
{
    return acos(Dot(A, B) / Length(A) / Length(B));
}

double Cross(const Vector& A, const 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);
}

bool SegmentProperIntersection(const Point& a1, const Point& a2, const Point& b1, const Point& b2)
{
	double c1 = Cross(a2-a1,b1-a1), c2 = Cross(a2-a1,b2-a1),
		c3 = Cross(b2-b1,a1-b1), c4=Cross(b2-b1,a2-b1);
	return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}

bool OnSegment(const Point& p, const Point& a1, const Point& a2)
{
	return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0;
}

//求两直线交点,先确保两直线有唯一交点,当且仅当Cross(v, w)非0
Point GetLineIntersection(const Point& P, const Point& v, const Point& Q, const Point& w)
{
	Vector u = P-Q;
	double t = Cross(w, u) / Cross(v, w);
	return P+v*t;
}

Vector Rotate(const Vector& A, double rad)
{
	return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));
}
double DistanceToLine(Point P, Point A, Point B)
{
    Vector v1 = B - A, v2 = P - A;
    return fabs(Cross(v1, v2)) / Length(v1); // 如果不取绝对值,得到的是有向距离
}

//判断是否是凸四边形
bool is_Convex(Point A, Point B, Point C, Point D)
{
    if(SegmentProperIntersection(A, B, C, D) || SegmentProperIntersection(A, C, B, D) || SegmentProperIntersection(A, D, B, C))
        return 1;
    else
        return 0;
}
//判断是否是正方形
bool is_Square(Point A, Point B, Point C, Point D)
{
    Point p1, p2, p3, p4;
    bool flag = false;
    if(SegmentProperIntersection(A, B, C, D))
        p1 = A, p2 = B, p3 = C, p4 = D, flag = true;
    else if(SegmentProperIntersection(A, C, B, D))
        p1 = A, p2 = C, p3 = B, p4 = D, flag = true;
    else if(SegmentProperIntersection(A, D, B, C))
        p1 = A, p2 = D, p3 = B, p4 = C, flag = true;
    if(flag && dcmp(Cross(p1 - p3, p2 - p4)) == 0 && dcmp(Cross(p1 - p4, p2 - p3)) == 0 &&
       dcmp(Dot(p1 - p2, p3 - p4)) == 0 && dcmp(Dot(p1 - p3, p1 - p4)) == 0)
       return true;
    return false;
}
//判断是否是平行四边形
bool is_Parallelogram(Point A, Point B, Point C, Point D)
{
    Point p1, p2, p3, p4;
    bool flag = false;
    if(SegmentProperIntersection(A, B, C, D))
        p1 = A, p2 = B, p3 = C, p4 = D, flag = true;
    else if(SegmentProperIntersection(A, C, B, D))
        p1 = A, p2 = C, p3 = B, p4 = D, flag = true;
    else if(SegmentProperIntersection(A, D, B, C))
        p1 = A, p2 = D, p3 = B, p4 = C, flag = true;
    if(flag && dcmp(Cross(p1 - p3, p2 - p4)) == 0 && dcmp(Cross(p1 - p4, p2 - p3)) == 0)
       return true;
    return false;
}
//判断是否是矩形
bool is_Rectangle(Point A, Point B, Point C, Point D)
{
    Point p1, p2, p3, p4;
    bool flag = false;
    if(SegmentProperIntersection(A, B, C, D))
        p1 = A, p2 = B, p3 = C, p4 = D, flag = true;
    else if(SegmentProperIntersection(A, C, B, D))
        p1 = A, p2 = C, p3 = B, p4 = D, flag = true;
    else if(SegmentProperIntersection(A, D, B, C))
        p1 = A, p2 = D, p3 = B, p4 = C, flag = true;
    if(flag && dcmp(Cross(p1 - p3, p2 - p4)) == 0 && dcmp(Cross(p1 - p4, p2 - p3)) == 0 &&
       dcmp(Dot(p1 - p3, p1 - p4)) == 0)
       return true;
    return false;
}
//判断是否是菱形
bool is_Rhombus(Point A, Point B, Point C, Point D)
{
    Point p1, p2, p3, p4;
    bool flag = false;
    if(SegmentProperIntersection(A, B, C, D))
        p1 = A, p2 = B, p3 = C, p4 = D, flag = true;
    else if(SegmentProperIntersection(A, C, B, D))
        p1 = A, p2 = C, p3 = B, p4 = D, flag = true;
    else if(SegmentProperIntersection(A, D, B, C))
        p1 = A, p2 = D, p3 = B, p4 = C, flag = true;
    if(flag && dcmp(Cross(p1 - p3, p2 - p4)) == 0 && dcmp(Cross(p1 - p4, p2 - p3)) == 0 &&
       dcmp(Dot(p1 - p2, p3 - p4)) == 0)
       return true;
    return false;
}
//判断是否是梯形
bool is_Trapezium(Point A, Point B, Point C, Point D)
{
    Point p1, p2, p3, p4;
    bool flag = false;
    if(SegmentProperIntersection(A, B, C, D))
        p1 = A, p2 = B, p3 = C, p4 = D, flag = true;
    else if(SegmentProperIntersection(A, C, B, D))
        p1 = A, p2 = C, p3 = B, p4 = D, flag = true;
    else if(SegmentProperIntersection(A, D, B, C))
        p1 = A, p2 = D, p3 = B, p4 = C, flag = true;
    if(flag && dcmp(Cross(p1 - p3, p2 - p4)) == 0 && dcmp(Cross(p1 - p4, p2 - p3)) != 0)
       return true;
    if(flag && dcmp(Cross(p1 - p3, p2 - p4)) != 0 && dcmp(Cross(p1 - p4, p2 - p3)) == 0)
       return true;
    return false;
}

int main()
{
    //freopen("in.txt", "r", stdin);
    int T, cnt;
    cnt = 0;
    scanf("%d", &T);
    Point a, b, c, d;
    while(T--)
    {
        scanf("%lf%lf", &a.x, &a.y);
        scanf("%lf%lf", &b.x, &b.y);
        scanf("%lf%lf", &c.x, &c.y);
        scanf("%lf%lf", &d.x, &d.y);
        printf("Case %d: ", ++cnt);
        if(is_Square(a, b, c, d))
        {
            printf("Square\n");
            continue;
        }
        if(is_Rectangle(a, b, c, d))
        {
            printf("Rectangle\n");
            continue;
        }
        if(is_Rhombus(a, b, c, d))
        {
            printf("Rhombus\n");
            continue;
        }
        if(is_Parallelogram(a, b, c, d))
        {
            printf("Parallelogram\n");
            continue;
        }
        if(is_Trapezium(a, b, c, d))
        {
            printf("Trapezium\n");
            continue;
        }
        printf("Ordinary Quadrilateral\n");

    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值