three points 1(牛客多校) (计算几何, 三角形)

题意:

t组样例,每组样例输入w, h, a, b, c.在坐标系中,0 <= x <= w,  0<=y<=h,

求出三个点X, Y, Z, 并且|XY| = a, |XZ| = b, |YZ| = c,求这三点坐标并依次输出

 

题解:

  • 一个三角形在矩形中是合法的,那么就一定可以平移到矩阵的某个角, 
  • 然后让三角形的某个边和这个矩形的边重叠,
  • 具体处理利用三角函数(不要用沟谷定理,减少误差)
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
double w, h;
struct node{
    double x, y;
}no[3];
bool check(double a, double b, double c, int x, int y, int z){
    no[x].x = 0.0;
    no[x].y = 0.0;
    if(a <= w){
        no[y].x = a;
        no[y].y = 0.0;
    }
    else{
        no[y].x = w;
        no[y].y = sqrt(a*a - w*w);
    }
    double ang = acos((a*a + b*b - c*c )*1.0/ (2*a*b));
    ang = ang + atan(no[y].y*1.0 / no[y].x);
    no[z].x = b * cos(ang);
    no[z].y = b * sin(ang);
    if(no[z].x >= -eps && no[z].x <= w+eps && no[z].y >= -eps && no[z].y <= h+eps){
        printf("%.12lf %.12lf %.12lf %.12lf %.12lf %.12lf\n", no[0].x, no[0].y, no[1].x, no[1].y, no[2].x, no[2].y);
        return true;
    }
    
    return false;
}
int main(){
    int t;
    cin >> t;
    while(t--){
        double a, b, c;
        scanf("%lf%lf%lf%lf%lf", &w, &h, &a, &b, &c);
        if(check(a, b, c, 0, 1, 2)) continue;
        if(check(a, c, b, 1, 0, 2)) continue;
        if(check(b, a, c, 0, 2, 1)) continue;
        if(check(b, c, a, 2, 0, 1)) continue;
        if(check(c, a, b, 1, 2, 0)) continue;
        if(check(c, b, a, 2, 1, 0)) continue;    
    }
    return 0;
} 
随便偷的代码

 

posted @ 2023-05-24 11:10  VxiaohuanV  阅读(12)  评论(0编辑  收藏  举报