现在的位置: 首页 > 综合 > 正文

UVA 11178 – Morley’s Theorem(计算几何)

2018年10月12日 ⁄ 综合 ⁄ 共 1796字 ⁄ 字号 评论关闭

这是一道基础的计算几何,基本自己推推就能推出来了,基本思路就是根据3点,求出角度,就可以知道要旋转的角度,然后求出两个旋转后的向量求交点输出即可

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

struct Point {
    double x, y;
    Point() {}
    Point(double x, double y) {
        this->x = x;
        this->y = y;
    }
    void read() {
        scanf("%lf%lf", &x, &y);
    }
};

typedef Point Vector;

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);
}

const double eps = 1e-8;

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));} //向量的模
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 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);
}

Point get(Point A, Point B, Point C) {
    Vector v1 = C - B;
    double a1 = Angle(A - B, v1);
    v1 = Rotate(v1, a1 / 3);
    Vector v2 = B - C;
    double a2 = Angle(A - C, v2);
    v2 = Rotate(v2, -a2 / 3);
    return GetLineIntersection(B, v1, C, v2);
}

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        Point A, B, C, D, E, F;
        A.read();
        B.read();
        C.read();
        D = get(A, B, C);
        E = get(B, C, A);
        F = get(C, A, B);
        printf("%.6f %.6f %.6f %.6f %.6f %.6f\n", D.x, D.y, E.x, E.y, F.x, F.y);
    }
    return 0;
}

抱歉!评论已关闭.