【计算几何】URAL - 2101 - Knight's Shield
Input
Output
Example
| input | output |
|---|---|
0 0 |
48.0000000000 |
-3 0 |
9.0697674419 |
Notes
把三角形按锐角、直角、钝角分类讨论,看点p是否在三条高上。锐角三角形的答案在3-6之间,直角在3-4之间,钝角在1-2之间。
需要求点在直线上的射影,然后再相似三角形啦,正切函数啥的啦搞一下面积就出来了。
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define EPS 0.00000001
struct 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的更多相关文章
- Ural 1298 Knight 题解
目录 Ural 1298 Knight 题解 题意 题解 程序 Ural 1298 Knight 题解 题意 给定一个\(n\times n(1\le n\le8)\)的国际象棋棋盘和一个骑士(基本上 ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- Ural 1197 - Lonesome Knight
The statement of this problem is very simple: you are to determine how many squares of the chessboar ...
- Ural 2036. Intersect Until You're Sick of It 计算几何
2036. Intersect Until You're Sick of It 题目连接: http://acm.timus.ru/problem.aspx?space=1&num=2036 ...
- URAL 1775 B - Space Bowling 计算几何
B - Space BowlingTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...
- Ural 1046 Geometrical Dreams(解方程+计算几何)
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1046 参考博客:http://hi.baidu.com/cloudygoose/item ...
- URAL 2099 Space Invader题解 (计算几何)
啥也不说了,直接看图吧…… 代码如下: #include<stdio.h> #include<iostream> #include<math.h> using na ...
- URAL 1966 Cycling Roads 计算几何
Cycling Roads 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/F Description When Vova was ...
随机推荐
- 用原生JavaScript做个简单的回到顶部
很多网页在下方都会放置一个“返回顶部”按钮,尤其是页面底部没有导航的网页,这样可以帮助访客重新找到导航或者重温一遍广告(想得真美).随着近几年来 JavaScript 的应用日渐广泛,滑动效果无处不在 ...
- at用法小记
By francis_hao Aug 22,2017 at – 设置稍后执行的作业. 概要 at [-V] [-f file] [-mMlv] timespec...at [-V] [-f ...
- 从零开始学习MXnet(三)之Model和Module
在我们在MXnet中定义好symbol.写好dataiter并且准备好data之后,就可以开开心的去训练了.一般训练一个网络有两种常用的策略,基于model的和基于module的.今天,我想谈一谈他们 ...
- 获得edittext的图片大小
1.在布局文件中编写控件,有2张图片 <EditText android:id="@+id/edit" android:background="@drawable/ ...
- Spring Boot(一)
1.注解 @EnableAutoConfiguration 官方文档:The @EnableAutoConfiguration annotation is often placed on your ...
- PowerShell官方文档
PowerShell PowerShell 在 .NET Framework 基础之上构建,是一种基于任务的命令行 Shell 脚本语言:专门面向系统管理员和高级用户,可快速自动化多个操作系统(Lin ...
- input 单选按钮radio 取消选中(转载)
input单选按钮: 在radio按钮中添加属性tag 0代表未被选中 HTML代码: <input name="rdo1" value="AA" ty ...
- [Leetcode Week8]Triangle
Triangle 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/triangle/description/ Description Given a t ...
- Linux上使用程序相对路径访问文件【转】
转自:http://blog.csdn.net/yinxusen/article/details/7444249 今天一个朋友问我这个问题,说为什么在Windows上跑得很好的应用程序,移植到Linu ...
- bootstrap,ECMA
前端UI(布局)框架 bootstrap Amaze UI BootStrap 全局css样式 栅格系统 container 容器 超小屏幕 手机 vw <768px 宽度 100% 小屏幕 平 ...