【計算幾何】URAL - 2101 - Knight's Shield

來源:互聯網
上載者:User

標籤:question   sid   lag   border   rom   面積   scribe   depend   find   

Little Peter Ivanov likes to play knights. Or musketeers. Or samurai. It depends on his mood. For parents, it is still always looks like “he again found a stick and peels the trees.” They cannot understand that it is a sword. Or epee. Or katana.Today Peter has found a shield. Actually, it is a board from the fence; fortunately, the nails from it have already been pulled. Peter knows that the family coat of arms should be depicted on the knight’s shield. The coat of arms of Ivanovs is a rectangle inscribed in a triangle (only grandfather supports Peter’s game, and he is, after all, a professor of mathematics). Peter has already drawn the triangle, and then noticed that there is a hole from a nail inside the triangle. It is not very good, so Peter wants to draw a rectangle in such a way that the hole will be on its border.Because of the rectangle in Peter’s family symbolizes the authority and power then Peter wants to draw a rectangle of maximum area.And due to the fact, that Peter is a grandson of grandfather-mathematician, he is also interested in purely theoretical question — how many different rectangles, satisfying the conditions, can be drawn in the triangle.Help Peter to find the answers to these questions.

Input

The four lines contain the coordinates of four points that are the vertices of the triangle and the hole, respectively. All coordinates are integers and do not exceed 10 4 in absolute value. It is guaranteed that the hole is strictly inside the triangle. Also it is guaranteed that the triangle vertices do not lie on one line.

Output

In the first line output the maximum area of a rectangle, which Peter can draw. The answer will be considered correct if a relative or absolute error of maximum area does not exceed 10 ?6.In the second line output the number of different rectangles that Peter can draw (these rectangles are not required to have the maximum area).

Example

input output
0 010 00 204 6
48.00000000004
-3 02 -15 70 1
9.06976744192
NotesThe rectangle is called inscribed in a triangle if all its vertices lie on the sides of the triangle.

把三角形按銳角、直角、鈍角分類討論,看點p是否在三條高上。銳角三角形的答案在3-6之間,直角在3-4之間,鈍角在1-2之間。

需要求點在直線上的射影,然後再相似三角形啦,正切函數啥的啦搞一下面積就出來了。

#include<cstdio>#include<cmath>#include<algorithm>using namespace std;#define EPS 0.00000001struct Point{double x,y;Point(const double &X,const double &Y)  {  x=X;  y=Y;  }Point(){}double Length()  {  return sqrt(x*x+y*y);  }}p,a[4];typedef Point Vector;double Dot(const Vector &a,const Vector &b){return a.x*b.x+a.y*b.y;}Vector operator - (const Vector &a,const Vector &b){return Vector(a.x-b.x,a.y-b.y);}Vector operator + (const Vector &a,const Vector &b){return Vector(a.x+b.x,a.y+b.y);}double Cross(const Vector &a,const Vector &b){return a.x*b.y-a.y*b.x;}double DisToLine(Point P,Point A,Point B){Vector v1=B-A,v2=P-A;return fabs(Cross(v1,v2))/v1.Length();}double tanget(Point a,Point b,Point c)//a是頂點{double COS=Dot(b-a,c-a)/(b-a).Length()/(c-a).Length();double SIN=sqrt((1.0-COS*COS));return SIN/COS;}Vector operator * (const double &x,const Vector &v){return Vector(x*v.x,x*v.y);}Point GetLineProjection(Point P,Point A,Point B){Vector v=B-A;return A+(Dot(v,P-A)/Dot(v,v))*v;}double area;int main(){//freopen("b.in","r",stdin);for(int i=1;i<=3;++i)  scanf("%lf%lf",&a[i].x,&a[i].y);scanf("%lf%lf",&p.x,&p.y);int flag=0,ans=0;//鈍角三角形if(Dot(a[2]-a[1],a[3]-a[1])<-EPS)  {  double dis=DisToLine(p,a[2],a[3]);  area=dis*((a[2]-a[3]).Length()-dis/tanget(a[2],a[1],a[3])-dis/tanget(a[3],a[1],a[2]));  if(fabs(Dot(a[1]-p,a[2]-a[3]))<EPS)    ans=1;  else    {    ans=2;    Point p1=GetLineProjection(a[1],a[2],a[3]);    Point p2=GetLineProjection(p,a[2],a[3]);    double h=(a[1]-p1).Length();    double h1;    if(Dot(p-p1,a[2]-p1)>EPS)      h1=(p2-a[2]).Length()/(p1-a[2]).Length()*h;    else      h1=(p2-a[3]).Length()/(p1-a[3]).Length()*h;    double l1=(h-h1)/h*(a[2]-a[3]).Length();    area=max(area,h1*l1);    }  printf("%.10lf\n%d\n",area,ans);  return 0;  }else if(Dot(a[1]-a[2],a[3]-a[2])<-EPS)  {  double dis=DisToLine(p,a[1],a[3]);  area=dis*((a[1]-a[3]).Length()-dis/tanget(a[1],a[2],a[3])-dis/tanget(a[3],a[1],a[2]));  if(fabs(Dot(a[2]-p,a[1]-a[3]))<EPS)    ans=1;  else    {    ans=2;    Point p1=GetLineProjection(a[2],a[1],a[3]);    Point p2=GetLineProjection(p,a[1],a[3]);    double h=(a[2]-p1).Length();    double h1;    if(Dot(p-p1,a[1]-p1)>EPS)      h1=(p2-a[1]).Length()/(p1-a[1]).Length()*h;    else      h1=(p2-a[3]).Length()/(p1-a[3]).Length()*h;    double l1=(h-h1)/h*(a[1]-a[3]).Length();    area=max(area,h1*l1);    }  printf("%.10lf\n%d\n",area,ans);  return 0;  }else if(Dot(a[1]-a[3],a[2]-a[3])<-EPS)  {  double dis=DisToLine(p,a[1],a[2]);  area=dis*((a[1]-a[2]).Length()-dis/tanget(a[1],a[2],a[3])-dis/tanget(a[2],a[1],a[3]));  if(fabs(Dot(a[3]-p,a[1]-a[2]))<EPS)    ans=1;  else    {    ans=2;    Point p1=GetLineProjection(a[3],a[1],a[2]);    Point p2=GetLineProjection(p,a[1],a[2]);    double h=(a[3]-p1).Length();    double h1;    if(Dot(p-p1,a[1]-p1)>EPS)      h1=(p2-a[1]).Length()/(p1-a[1]).Length()*h;    else      h1=(p2-a[2]).Length()/(p1-a[2]).Length()*h;    double l1=(h-h1)/h*(a[1]-a[2]).Length();    area=max(area,h1*l1);    }  printf("%.10lf\n%d\n",area,ans);  return 0;  }//直角三角形if(fabs(Dot(a[2]-a[1],a[3]-a[1]))<EPS)  {  double dis=DisToLine(p,a[2],a[3]);  area=dis*((a[2]-a[3]).Length()-dis/tanget(a[2],a[1],a[3])-dis/tanget(a[3],a[1],a[2]));  if(fabs(Dot(a[1]-p,a[2]-a[3]))<EPS)    ans=3;  else    {    ans=4;    Point p1=GetLineProjection(a[1],a[2],a[3]);    Point p2=GetLineProjection(p,a[2],a[3]);    double h=(a[1]-p1).Length();    double h1;    if(Dot(p-p1,a[2]-p1)>EPS)      h1=(p2-a[2]).Length()/(p1-a[2]).Length()*h;    else      h1=(p2-a[3]).Length()/(p1-a[3]).Length()*h;    double l1=(h-h1)/h*(a[2]-a[3]).Length();    area=max(area,h1*l1);        Point p3=GetLineProjection(p,a[1],a[3]);    double h2=(p3-a[3]).Length()/(a[1]-a[3]).Length()*(a[1]-a[2]).Length();    Point p4=GetLineProjection(p,a[1],a[2]);    double l2=(a[1]-a[3]).Length()-h2/tanget(a[3],a[1],a[2]);    area=max(area,h2*l2);        double h3=(p4-a[2]).Length()/(a[1]-a[2]).Length()*(a[1]-a[3]).Length();    double l3=(a[1]-a[2]).Length()-h3/tanget(a[2],a[1],a[3]);    area=max(area,h3*l3);    }  printf("%.10lf\n%d\n",area,ans);  return 0;  }else if(fabs(Dot(a[1]-a[2],a[3]-a[2]))<EPS)  {  double dis=DisToLine(p,a[1],a[3]);  area=dis*((a[1]-a[3]).Length()-dis/tanget(a[1],a[2],a[3])-dis/tanget(a[3],a[1],a[2]));  if(fabs(Dot(a[2]-p,a[1]-a[3]))<EPS)    ans=3;  else    {    ans=4;    Point p1=GetLineProjection(a[2],a[1],a[3]);    Point p2=GetLineProjection(p,a[1],a[3]);    double h=(a[2]-p1).Length();    double h1;    if(Dot(p-p1,a[1]-p1)>EPS)      h1=(p2-a[1]).Length()/(p1-a[1]).Length()*h;    else      h1=(p2-a[3]).Length()/(p1-a[3]).Length()*h;    double l1=(h-h1)/h*(a[1]-a[3]).Length();    area=max(area,h1*l1);        Point p3=GetLineProjection(p,a[2],a[3]);    double h2=(p3-a[3]).Length()/(a[2]-a[3]).Length()*(a[1]-a[2]).Length();    Point p4=GetLineProjection(p,a[1],a[2]);    double l2=(a[2]-a[3]).Length()-h2/tanget(a[3],a[1],a[2]);    area=max(area,h2*l2);        double h3=(p4-a[1]).Length()/(a[1]-a[2]).Length()*(a[2]-a[3]).Length();    double l3=(a[1]-a[2]).Length()-h3/tanget(a[1],a[2],a[3]);    area=max(area,h3*l3);    }  printf("%.10lf\n%d\n",area,ans);  return 0;  }else if(fabs(Dot(a[1]-a[3],a[2]-a[3]))<EPS)  {  double dis=DisToLine(p,a[1],a[2]);  area=dis*((a[1]-a[2]).Length()-dis/tanget(a[1],a[2],a[3])-dis/tanget(a[2],a[1],a[3]));  if(fabs(Dot(a[3]-p,a[1]-a[2]))<EPS)    ans=3;  else    {    ans=4;    Point p1=GetLineProjection(a[3],a[1],a[2]);    Point p2=GetLineProjection(p,a[1],a[2]);    double h=(a[3]-p1).Length();    double h1;    if(Dot(p-p1,a[1]-p1)>EPS)      h1=(p2-a[1]).Length()/(p1-a[1]).Length()*h;    else      h1=(p2-a[2]).Length()/(p1-a[2]).Length()*h;    double l1=(h-h1)/h*(a[1]-a[2]).Length();    area=max(area,h1*l1);        Point p3=GetLineProjection(p,a[2],a[3]);    double h2=(p3-a[2]).Length()/(a[2]-a[3]).Length()*(a[1]-a[3]).Length();    Point p4=GetLineProjection(p,a[1],a[3]);    double l2=(a[2]-a[3]).Length()-h2/tanget(a[2],a[1],a[3]);    area=max(area,h2*l2);        double h3=(p4-a[1]).Length()/(a[1]-a[3]).Length()*(a[2]-a[3]).Length();    double l3=(a[1]-a[3]).Length()-h3/tanget(a[1],a[2],a[3]);    area=max(area,h3*l3);    }  printf("%.10lf\n%d\n",area,ans);  return 0;  }//銳角三角形for(int i=1;i<=3;++i)//枚舉上頂點  {  int j,k;  if(i==1) j=2,k=3;  else if(i==2) j=3,k=1;  else j=1,k=2;  double dis=DisToLine(p,a[j],a[k]);  area=max(area,dis*((a[j]-a[k]).Length()-dis/tanget(a[j],a[i],a[k])-dis/tanget(a[k],a[i],a[j])));  if(fabs(Dot(a[i]-p,a[k]-a[j]))<EPS)    ++ans;  else    {    ans+=2;  Point p1=GetLineProjection(a[i],a[j],a[k]);    Point p2=GetLineProjection(p,a[j],a[k]);    double h=(a[i]-p1).Length();    double h1;    if(Dot(p-p1,a[j]-p1)>EPS)      h1=(p2-a[j]).Length()/(p1-a[j]).Length()*h;    else      h1=(p2-a[k]).Length()/(p1-a[k]).Length()*h;    double l1=(h-h1)/h*(a[j]-a[k]).Length();    area=max(area,h1*l1);    }  }printf("%.10lf\n%d\n",area,ans);return 0;}

【計算幾何】URAL - 2101 - Knight's Shield

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.