生活随笔
收集整理的這篇文章主要介紹了
牛客 - 四等分的角度(几何)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:輸入 A , O , B 三個點的坐標,輸出 K1 , K2 , K3 分別為角 AOB 四等分線上的點
題目分析:吐了吐了,這個題想到了一種解法,但是在實現的時候被高中的向量知識卡住了,要是這個題能過的話這次的紅包應該能領不少嗚嗚嗚
說回題目,借題目的圖一用:
為了方便講解,就將三條四等分線從左到右稱為 XP , XC , XQ ,其中點 O 被我替換成了點 X
一開始想過可以求出角 AOB 的大小,然后除以四,每次將直線 XA 沿著點 X 旋轉這么個角度就行了,不得不說思路很好,實現很難,放棄
接下來就想,既然是四等分點,求兩次角平分線不就行了,那么我們可以先求出 XA 和 XB 的角平分線 XC ,然后再求出 XC 和 XA 的角平分線就是 XP 了,同理求出 XQ ,有了這個三個四等分線的直線方程,再令其與直線 AB 求交點,求出來的就是答案了
求角平分線有一種非常簡單的方法,以 XA 和 XB 為例,可以先求出 XA 和 XB 代表的單位向量,根據向量的加法的特殊性質, XC 恰好就是 XA + XB ,這樣一來題目就變的很簡單了
好巧不巧,之前一直在用的 kuangbin 大牛的幾何模板中,恰好有一個函數就是可以直接求單位向量的
需要注意的地方是,一開始是直線 XA ,其向量就是 ( OA - OX ) ,這里的 O 就是坐標原點
還有一點就是,直接這樣求出來的 XC 是單位向量!!需要借助點 X 轉換回 XC 這條直線才能進行后續操作,這里需要借助的知識就是,知道單位向量和直線上的一個點的坐標后如何得到直線方程,因為點 X 肯定在直線上的,所以點 X + XC 肯定也是在直線上,兩點確定一條直線,這樣直線得方程就確定好了
剩下的就是調用模板里的函數了
代碼:
?
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<unordered_map>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e5+100;const double eps = 1e-10;int sgn(double x){if(fabs(x) < eps)return 0;if(x < 0)return -1;else return 1;
}struct Point{double x,y;Point(){}Point(double _x,double _y){x = _x;y = _y;}void input(){scanf("%lf%lf",&x,&y);}void output(){printf("%.10f %.10f\n",x,y);}bool operator == (Point b)const{return sgn(x-b.x) == 0 && sgn(y-b.y) == 0;}bool operator < (Point b)const{return sgn(x-b.x)== 0?sgn(y-b.y)<0:x<b.x;}Point operator -(const Point &b)const{return Point(x-b.x,y-b.y);}//叉積double operator ^(const Point &b)const{return x*b.y - y*b.x;}//點積double operator *(const Point &b)const{return x*b.x + y*b.y;}//返回長度double len(){return hypot(x,y);//庫函數}//返回兩點的距離double distance(Point p){return hypot(x-p.x,y-p.y);}Point operator +(const Point &b)const{return Point(x+b.x,y+b.y);}Point operator *(const double &k)const{return Point(x*k,y*k);}Point operator /(const double &k)const{return Point(x/k,y/k);}//`化為長度為r的向量`Point trunc(double r){double l = len();if(!sgn(l))return *this;r /= l;return Point(x*r,y*r);}
};struct Line{Point s,e;Line(){}Line(Point _s,Point _e){s = _s;e = _e;}bool operator ==(Line v){return (s == v.s)&&(e == v.e);}void adjust(){if(e < s)swap(s,e);}//求線段長度double length(){return s.distance(e);}//`點和直線關系`//`1 在左側`//`2 在右側`//`3 在直線上`int relation(Point p){int c = sgn((p-s)^(e-s));if(c < 0)return 1;else if(c > 0)return 2;else return 3;}// 點在線段上的判斷bool pointonseg(Point p){return sgn((p-s)^(e-s)) == 0 && sgn((p-s)*(p-e)) <= 0;}//`兩向量平行(對應直線平行或重合)`bool parallel(Line v){return sgn((e-s)^(v.e-v.s)) == 0;}//`兩線段相交判斷`//`2 規范相交`//`1 非規范相交`//`0 不相交`int segcrossseg(Line v){int d1 = sgn((e-s)^(v.s-s));int d2 = sgn((e-s)^(v.e-s));int d3 = sgn((v.e-v.s)^(s-v.s));int d4 = sgn((v.e-v.s)^(e-v.s));if( (d1^d2)==-2 && (d3^d4)==-2 )return 2;return (d1==0 && sgn((v.s-s)*(v.s-e))<=0) ||(d2==0 && sgn((v.e-s)*(v.e-e))<=0) ||(d3==0 && sgn((s-v.s)*(s-v.e))<=0) ||(d4==0 && sgn((e-v.s)*(e-v.e))<=0);}//`直線和線段相交判斷`//`-*this line -v seg`//`2 規范相交`//`1 非規范相交`//`0 不相交`int linecrossseg(Line v){int d1 = sgn((e-s)^(v.s-s));int d2 = sgn((e-s)^(v.e-s));if((d1^d2)==-2) return 2;return (d1==0||d2==0);}//`兩直線關系`//`0 平行`//`1 重合`//`2 相交`int linecrossline(Line v){if((*this).parallel(v))return v.relation(s)==3;return 2;}//`求兩直線的交點`//`要保證兩直線不平行或重合`Point crosspoint(Line v){double a1 = (v.e-v.s)^(s-v.s);double a2 = (v.e-v.s)^(e-v.s);return Point((s.x*a2-e.x*a1)/(a2-a1),(s.y*a2-e.y*a1)/(a2-a1));}//點到直線的距離double dispointtoline(Point p){return fabs((p-s)^(e-s))/length();}//點到線段的距離double dispointtoseg(Point p){if(sgn((p-s)*(e-s))<0 || sgn((p-e)*(s-e))<0)return min(p.distance(s),p.distance(e));return dispointtoline(p);}//`返回線段到線段的距離`//`前提是兩線段不相交,相交距離就是0了`double dissegtoseg(Line v){return min(min(dispointtoseg(v.s),dispointtoseg(v.e)),min(v.dispointtoseg(s),v.dispointtoseg(e)));}
};int main()
{
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);Point A,B,X;A.input(),X.input(),B.input();Point OA=(A-X).trunc(1);Point OB=(B-X).trunc(1);Point OC=(OA+OB).trunc(1);Point OP=OA+OC;Point OQ=OB+OC;Line AB(Line(A,B));AB.crosspoint(Line(OP+X,X)).output();AB.crosspoint(Line(OC+X,X)).output();AB.crosspoint(Line(OQ+X,X)).output();return 0;
}
?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生
總結
以上是生活随笔為你收集整理的牛客 - 四等分的角度(几何)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。