LA 4413 Triangle Hazard 梅涅劳斯定理的应用

题目地址:pdf版本

比如说求A,我们知道了向量PR ,只需要求出PA/PR的值,就知道向量PA,这样加上P就可以求出A。

关于比例的计算,用到梅涅劳斯定理  纯平面几何内容。

然后简化计算时,用到了轮换对称性,减少代码量。

代码:

#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
const double eps=1e-10;
const double PI=acos(-1);

using namespace std;


struct Point{
    double x;
    double y;
    Point(double x=0,double y=0):x(x),y(y){}
    void operator<<(Point &A) {cout<<A.x<<' '<<A.y<<endl;}
};

int dcmp(double x)  {return (x>eps)-(x<-eps); }

typedef  Point  Vector;

Vector  operator +(Vector A,Vector B) { return Vector(A.x+B.x,A.y+B.y);}

Vector  operator -(Vector A,Vector B) { return Vector(A.x-B.x,A.y-B.y); }

Vector  operator *(Vector A,double p) { return Vector(A.x*p,A.y*p);  }

Vector  operator /(Vector A,double p) {return Vector(A.x/p,A.y/p);}

// ps  cout

ostream &operator<<(ostream & out,Point & P) { out<<P.x<<' '<<P.y<<endl; return out;}

bool  operator< (const Point &A,const Point &B) { return A.x<B.x||(A.x==B.x&&A.y<B.y); }

bool  operator== ( const Point &A,const Point &B) { return dcmp(A.x-B.x)==0&&dcmp(A.y-B.y)==0;}


double  Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;}

double  Cross(Vector A,Vector B)  {return A.x*B.y-B.x*A.y; }

double  Length(Vector A)  { return sqrt(Dot(A, A));}


double  Angle(Vector A,Vector B) {return acos(Dot(A,B)/Length(A)/Length(B));}

double  Area2(Point A,Point B,Point C ) {return Cross(B-A, C-A);}

Vector Rotate(Vector A,double rad) { return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));}
Vector Normal(Vector A) {double L=Length(A);return Vector(-A.y/L,A.x/L);}

Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)
{
    Vector u=P-Q;
    double t=Cross(w, u)/Cross(v,w);
    return P+v*t;
    
}

double DistanceToLine(Point P,Point A,Point B)
{
    Vector v1=P-A; Vector v2=B-A;
    return fabs(Cross(v1,v2))/Length(v2);
    
}

double DistanceToSegment(Point P,Point A,Point B)
{
    if(A==B)  return Length(P-A);
    
    Vector v1=B-A;
    Vector v2=P-A;
    Vector v3=P-B;
    
    if(dcmp(Dot(v1,v2))==-1)    return  Length(v2);
    else if(Dot(v1,v3)>0)    return Length(v3);
    
    else return DistanceToLine(P, A, B);
    
}

Point GetLineProjection(Point P,Point A,Point B)
{
    Vector v=B-A;
    Vector v1=P-A;
    double t=Dot(v,v1)/Dot(v,v);
    
    return  A+v*t;
}

bool  SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
{
    double c1=Cross(b1-a1, a2-a1);
    double c2=Cross(b2-a1, a2-a1);
    double c3=Cross(a1-b1, b2-b1);
    double c4=Cross(a2-b1, b2-b1);
    
    //cout<<c1<<c2<<c3<<c4<<endl;
    return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0 ;
    
}

bool  OnSegment(Point P,Point A,Point B)
{
    return dcmp(Cross(P-A, P-B))==0&&dcmp(Dot(P-A,P-B))<0;
}

double PolygonArea(Point *p,int n)
{
    double area=0;
    
    for(int i=1;i<n-1;i++)
    {
        area+=Cross(p[i]-p[0], p[i+1]-p[0]);
        
    }
    return area/2;
    
}

Point  read_point()
{
    Point P;
    scanf("%lf%lf",&P.x,&P.y);
    return  P;
}

Point getA(Point P,Point Q,Point R,double a,double b,double c)
{
    double r1=a*b/(a*b+1+a);
    double r2=(c*(a+1))/(1+c*(a+1));
    
    double r=(1-r1)/(1-r1-r2);
    Vector PA=(R-P)*r;
    return PA+P;
}


int main()
{
    int T;
    cin>>T;
    Point A,B,C,P,Q,R;
    double m[6];
    double a,b,c;
    while(T--)
    {
        P=read_point();
        Q=read_point();
        R=read_point();
        for(int i=0;i<6;i++)
        {
            cin>>m[i];
        }
        
        a=m[0]/m[1];
        b=m[2]/m[3];
        c=m[4]/m[5];
    
        A=getA(P, Q, R, a, b, c);
        B=getA(Q,R,P,b,c,a);
        C=getA(R,P,Q,c,a,b);
        
        printf("%.8lf %.8lf %.8lf %.8lf %.8lf %.8lf\n",A.x,A.y,B.x,B.y,C.x,C.y);
        
    }
   
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想了解关于接口的应用,以及如何使用接口来实现一个Shape类的Triangle子类,是吗?我可以为您解答这个问题。 首先,接口是一种定义了一组方法的抽象类型,它们没有任何实现,只是规定了一些方法的签名。在Java中,我们可以使用interface关键字来定义一个接口。接口的作用是让不同的类可以实现相同的方法,从而实现不同的功能。 现在,我们来看一下如何使用接口来实现一个Shape类的Triangle子类。首先,我们定义一个Shape接口,其中包含一个计算面积的方法和一个计算周长的方法: ``` public interface Shape { double getArea(); double getPerimeter(); } ``` 接下来,我们定义一个Triangle类,它实现了Shape接口,并且重写了getArea()和getPerimeter()方法。Triangle类的实现如下: ``` public class Triangle implements Shape { private double side1; private double side2; private double side3; public Triangle(double side1, double side2, double side3) { this.side1 = side1; this.side2 = side2; this.side3 = side3; } public double getArea() { double s = (side1 + side2 + side3) / 2; return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3)); } public double getPerimeter() { return side1 + side2 + side3; } } ``` 在Triangle类中,我们使用了三个私有变量来表示三角形的三条边,然后重写了Shape接口中的getArea()和getPerimeter()方法,计算出三角形的面积和周长。 现在,我们可以使用Triangle类来创建一个三角形对象,并调用它的getArea()和getPerimeter()方法来获取它的面积和周长。例如: ``` Triangle t = new Triangle(3, 4, 5); double area = t.getArea(); double perimeter = t.getPerimeter(); System.out.println("Area: " + area); System.out.println("Perimeter: " + perimeter); ``` 这段代码将创建一个边长分别为3、4、5的三角形对象,并打印出它的面积和周长。 希望我的解答对您有所帮助。如果您还有其他问题,请随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值