1. 程式人生 > 其它 >計算幾何Uva11178 - Morley's Theorem

計算幾何Uva11178 - Morley's Theorem

這裡模板是AcWing y總的和演算法競賽入門經典的

題目讓我們求三角形角的三等分角與其他三等分角的交點的笛卡爾座標(原pdf有圖很詳細,這裡可能表述錯誤)

但實際上就是模板操作

例如題目圖中∠D的求法,作法 1.求出三等分角,2.旋轉,3.求交點 都可用模板完成

∠E和∠F也是同樣的求法,並運用三角形的對稱性,只用寫一個函式。

//Uva  11178 - Morley's Theorem

#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include
<math.h> using namespace std; struct Point { double x,y; Point(double x=0,double y=0):x(x),y(y){ } }; typedef Point Vector; Vector operator -(Point A,Point 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 *(Point 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); } double dot(Point a,Point b) { return a.x*b.x+a.y*b.y; } double cross(Point a,Point b) { return a.x*b.y-b.x*a.y; } double get_length(Point a) { return
sqrt(dot(a,a)); } double get_angle(Vector a,Vector b) { return acos(dot(a,b)/get_length(a)/get_length(b)); } Point rotate(Point a,double angle) { return Point(a.x*cos(angle)-a.y*sin(angle),a.x*sin(angle)+a.y*cos(angle)); } Point get_line_intersection(Point p,Point v,Point q,Point w) { Vector u=p-q; double t=cross(w,u)/cross(v,w); return p+v*t; } Point getD(Point A,Point B,Point C) { Point v1=C-B; double ang1=get_angle(A-B,v1); v1=rotate(v1,ang1/3); Point v2=B-C; double ang2=get_angle(A-C,v2); v2=rotate(v2,-ang2/3); return get_line_intersection(B,v1,C,v2); } Point readpoint() { double x,y; cin>>x>>y; return Vector{x,y}; } int main() { int t; cin>>t; while(t--) { Point A,B,C,D,E,F; double a,b,c,d,e,f; A=readpoint(); B=readpoint(); C=readpoint(); D=getD(A,B,C); E=getD(B,C,A); F=getD(C,A,B); printf("%.6lf %.6lf %.6lf %.6lf %.6lf %.6lf\n",D.x,D.y,E.x,E.y,F.x,F.y); } return 0; }

本文來自部落格園,作者:magicat,轉載請註明原文連結:https://www.cnblogs.com/magicat/p/15526764.html