K - Triangle 计蒜客 - 42405
Posted Jozky86
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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|1
typedef 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;
else
l = 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;
else
l = 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;
else
l = 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;
else
l = mid;
}
printf("%.10lf %.10lf\\n",mid.x计蒜客-T1271 完美K倍子数组