uva11178(二维几何计算模板)

题目链接:http://vjudge.net/problem/viewProblem.action?id=18543

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const double PI = acos(-1.0);
struct Point{//点
    double x, y;
    Point(double _x = 0, double _y = 0):x(_x),y(_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);
}
bool operator < (const Point &a, const Point &b){
    return a.x < b.x || (a.x == b.x && a.y < b.y);
}
const double eps = 1e-10;
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 Length(Vector A){
    return sqrt(Dot(A, A));
}
//夹角大小a.b = |a||b|cosθ
double Angle(Vector A, Vector B){
    return acos(Dot(A, B) / Length(A) / Length(B));
}

/*叉积  右手定则
    1、等于两向量构成四边形的面积
    2、判断两向量顺逆时针关系,P*Q>0,Q在P左边
        等于0(共线)
*/
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));
}
//直线交点(P点和它的方向,Q点和它的方向)
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){
    Vector u = P-Q;
    double t = Cross(w, u) / Cross(v,w);
    return P + v*t;
}
//点到直线的距离,用平行四边形面积除以底
double DistanceToLine(Point P, Point A, Point B){
    Vector v1 = B - A, v2 = P - A;
    return fabs(Cross(v1, v2) / Length(v1));//如果不去绝对值,得有向距离
}
//点到线段距离
double DistanceToSegment(Point P, Point A, Point B){
    if(A == B)return Length(P-A);
    Vector v1 = B - A, v2 = P - A, v3 = P - B;//向量AB、AP、BP
    if(dcmp(Dot(v1, v2)) < 0)return Length(v2);//投影在线段AB左边
    else if(dcmp(Dot(v1, v3)) > 0)return Length(v3);//投影在线段AB右边
    else return fabs(Cross(v1, v2)) / Length(v1);//P点投影在线段上,距离变为求点到直线距离
}
//求点在直线上的投影Q
Point GetLineProjection(Point P, Point A, Point B){
    Vector v = B - A;
    return A + v*(Dot(v, P-A) / Dot(v, v));
}
//线段相交判定
bool OnSegment(Point p, Point a1, Point a2){//先判断一个点是否自一条线段上
    return dcmp(Cross(a1-p, a2-p))==0 && dcmp(Dot(a1-p, a2-p))<0;
}
//规范相交,恰有一个交点,每条线段两个端点都在另一个线段的两侧,即叉积符号不同
bool SegmentProperIntersection(Point a1, Point a2, Point b1, 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;
}
Point read_point()
{
    Point p;
    scanf("%lf%lf",&p.x,&p.y);
    return p;
}
int main()
{
    int i, j, n, t;
    double LA, LB, LC;
    Point A, B, C, E, D, F;
    Vector BD,CD,CE,AE,AF,BF;
    cin>>t;
    while(t--)
    {
        A = read_point();
        B = read_point();
        C = read_point();//点

        LA = Angle(C-A, B-A);
        LB = Angle(A-B, C-B);
        LC = Angle(B-C, A-C);//角度

        BD = Rotate(C-B, LB/3.0);
        CD = Rotate(B-C, -LC/3.0);
        D = GetLineIntersection(B, BD, C, CD);

        CE = Rotate(A-C, LC/3.0);
        AE = Rotate(C-A, -LA/3.0);
        E = GetLineIntersection(C, CE, A, AE);

        AF = Rotate(B-A, LA/3.0);
        BF = Rotate(A-B, -LB/3.0);
        F = GetLineIntersection(B, BF, A, AF);

        printf("%.6lf %.6lf %.6lf %.6lf %.6lf %.6lf\n",
               D.x,D.y,E.x,E.y,F.x,F.y);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值