K - Triangle 计蒜客 - 42405
生活随笔
收集整理的這篇文章主要介紹了
K - Triangle 计蒜客 - 42405
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
K - Triangle 計蒜客 - 42405
題意:
給你一個三角形的三點,再給你三角形邊上一個點,讓你求另一個點(也要在三角形上),使得平分三角形的面積
題解:
計算幾何
三角形的三邊ab,ac,bc
如果點p在ab上,離a點更近的話,另一個點在bc上,反之在ac上,如果在bc上,就開始二分bc上的點,二分個1000來遍差不多就能得到另一個點
計算幾何要求熟練使用模板,整個代碼基本上都是模板,就看你會不會用
在計算幾何這方面我還需要多練練
代碼:
#include <cstdio> #include <vector> #include <queue> #include <cstring> #include <cmath> #include <map> #include <set> #include <string> #include <iostream> #include <algorithm> #include <iomanip> using namespace std; #define sd(n) scanf("%d",&n) #define sdd(n,m) scanf("%d%d",&n,&m) #define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k) #define pd(n) printf("%d\n", n) #define pc(n) printf("%c", n) #define pdd(n,m) printf("%d %d", n, m) #define pld(n) printf("%lld\n", n) #define pldd(n,m) printf("%lld %lld\n", n, m) #define sld(n) scanf("%lld",&n) #define sldd(n,m) scanf("%lld%lld",&n,&m) #define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k) #define sf(n) scanf("%lf",&n) #define sc(n) scanf("%c",&n) #define sff(n,m) scanf("%lf%lf",&n,&m) #define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k) #define ss(str) scanf("%s",str) #define rep(i,a,n) for(int i=a;i<=n;i++) #define per(i,a,n) for(int i=n;i>=a;i--) #define mem(a,n) memset(a, n, sizeof(a)) #define debug(x) cout << #x << ": " << x << endl #define pb push_back #define all(x) (x).begin(),(x).end() #define fi first #define se second #define mod(x) ((x)%MOD) #define gcd(a,b) __gcd(a,b) #define lowbit(x) (x&-x) #define pii map<int,int> #define mk make_pair #define rtl rt<<1 #define rtr rt<<1|1typedef pair<int,int> PII; typedef long long ll; typedef unsigned long long ull; typedef long double ld; const int MOD = 1e9 + 7; //const double eps = 1e-9; const ll INF = 0x3f3f3f3f3f3f3f3fll; //const int inf = 0x3f3f3f3f; inline int read() {int ret = 0, sgn = 1;char ch = getchar();while(ch < '0' || ch > '9'){if(ch == '-')sgn = -1;ch = getchar();}while (ch >= '0' && ch <= '9'){ret = ret*10 + ch - '0';ch = getchar();}return ret*sgn; } inline void Out(int a) {if(a>9) Out(a/10);putchar(a%10+'0'); } int qpow(int m, int k, int mod) {int res = 1, t = m;while (k){if (k&1)res = res * t % mod;t = t * t % mod;k >>= 1;}return res; } ll gcd(ll a,ll b){return b==0?a : gcd(b,a%b);} ll lcm(ll a,ll b){return a*b/gcd(a,b);} ll inv(ll x,ll m){return qpow(x,m-2,m)%m;}const int N = 5e5+10; int n,m; typedef double db; const db eps = 1e-8; const db inf = 1e20; const db pi = acos(-1.0);int sgn(db x){if(fabs(x) < eps)return 0;if(x < 0)return -1;else return 1; }struct Point{db x, y;Point(){}Point(db _x, db _y){x = _x;y = _y;}void input(){scanf("%lf%lf", &x, &y);}bool operator == (Point b)const{return sgn(x-b.x) == 0 && sgn(y-b.y) == 0;}Point operator -(const Point &b)const{return Point(x-b.x, y-b.y);}//叉積db operator ^(const Point &b)const{return x*b.y - y*b.x;}//點積db operator *(const Point &b)const{return x*b.x + y*b.y;}//返回兩點的距離db dis(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 db &k)const{return Point(x*k, y*k);}Point operator /(const db &k)const{return Point(x/k, y/k);} };struct Line{Point s,e;Line(){}Line(Point _s,Point _e){s = _s;e = _e;}// 點在線段上的判斷bool pointonseg(Point p){return sgn((p-s)^(e-s)) == 0 && sgn((p-s) * (p-e)) <= 0;}// 求兩直線的交點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));} };// 求點a和點b的中點 Point get_mid(Point a, Point b) {return (a + b) * 0.5; }// 根據三個點計算三角形面積 double area(Point a,Point b,Point c) {return fabs((b - a) ^ (c - a) * 0.5); }signed main() {int t = 1;Point a,b,c,p;Line ab,ac,bc;sd(t);while(t--){a.input(),b.input(),c.input(),p.input();ab = Line(a,b),ac = Line(a,c),bc = Line(b,c);db s2 = area(a,b,c)/2;if(!ab.pointonseg(p) && !ac.pointonseg(p) && !bc.pointonseg(p)){cout<<-1<<endl;continue;}if(ab.pointonseg(p))//如果p在ab上 {// 離a點更近 那么另一點在bc上 反之在ac上。if(a.dis(p) < b.dis(p))//說明另一點在bc上 {Point l = b,r = c;Point mid = get_mid(l,r);int time = 1000;while(time--){mid = get_mid(l,r);db s = area(mid,p,b);int flag = sgn(s-s2);//判斷s和s2的面積 if(flag == 0)break;if(flag == 1)//s>s2,當S的面積更大,此時要縮小面積,說明終點在左側 r = mid;elsel = mid;//s<s2}printf("%.10lf %.10lf\n",mid.x,mid.y);}else//另一個點在ac上 {Point l = a,r = c;Point mid = get_mid(l,r);int time = 1000;while(time--){mid = get_mid(l,r);db s = area(mid,p,a);int flag = sgn(s-s2);if(flag == 0)break;if(flag == 1)r = mid;elsel = mid;}printf("%.10lf %.10lf\n",mid.x,mid.y);}}else if(ac.pointonseg(p)){if(a.dis(p) < c.dis(p)){Point l = c,r = b;Point mid = get_mid(l,r);int time = 1000;while(time--){mid = get_mid(l,r);db s = area(mid,p,c);int flag = sgn(s-s2);if(flag == 0)break;if(flag == 1)r = mid;elsel = mid;}printf("%.10lf %.10lf\n",mid.x,mid.y);}else{Point l = a,r = b;Point mid = get_mid(l,r);int time = 1000;while(time--){mid = get_mid(l,r);db s = area(mid,p,a);int flag = sgn(s-s2);if(flag == 0)break;if(flag == 1)r = mid;elsel = mid;}printf("%.10lf %.10lf\n",mid.x,mid.y);}}else{if(b.dis(p) < c.dis(p)){Point l = c,r = a;Point mid = get_mid(l,r);int time = 1000;while(time--){mid = get_mid(l,r);db s = area(mid,p,c);int flag = sgn(s-s2);if(flag == 0)break;if(flag == 1)r = mid;elsel = mid;}printf("%.10lf %.10lf\n",mid.x,mid.y);}else{Point l = b,r = a;Point mid = get_mid(l,r);int time = 1000;while(time--){mid = get_mid(l,r);db s = area(mid,p,b);int flag = sgn(s-s2);if(flag == 0)break;if(flag == 1)r = mid;elsel = mid;}printf("%.10lf %.10lf\n",mid.x,mid.y);}}}return 0; }總結
以上是生活随笔為你收集整理的K - Triangle 计蒜客 - 42405的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: H - Prince and Princ
- 下一篇: 路由器登陆界面的用户名和密码怎么重置无线