欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2019 ICPC 南京 K.Triangle(二分+几何)

程序员文章站 2022-03-30 17:07:42
...

题意:

给一个三角形,以及一个点(某条线段的端点),让求出另一点,使得这个线段平分这个三角形即两部分面积相等。

思路:

读完题就觉得是二分,奈何没有板子敲了半天,面积还是算不出来,最后抄了一手板子一发过。
首先如果p点不在三角形上面,直接输出-1
如果在三角形上面,那么可以判断出另一点是在另外两条边的哪条边上,比如p点在ab上且离a更近,那么 必然在bc上,反之在ac上。

代码:

#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))
        {
            // 离a点更近 那么另一点在bc上 反之在ac上。
            if(a.dis(p) < b.dis(p))
            {
                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);
                    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 = 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,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;
                    else
                        l = 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;
                    else
                        l = mid;
                }
                printf("%.10lf %.10lf\n",mid.x,mid.y);
            }
        }
    }
    return 0;
}