hrbustoj 1142:围困(计算几何基础题,判断点是否在三角形内)
围困
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 360(138 users) Total Accepted: 157(129 users) Rating: Special Judge: No
Description
Leyni是一名猎人,一天他在草原中散步,遇到了三只老狼,三只老狼围成了一个三角形,如果Leyni在这个三角形中,那么他必死无疑,否则他还有一线生机。
现在已知三只老狼的坐标和Leyni的坐标,请问,Leyni还有活下去的机会吗?
Input
本题有多组测试数据,对于每组测试数据:
第一行,含有4对坐标,x1, y1, x2, y2, x3, y3, xLeyni, yLeyni。前3对坐标表示三只老狼的位置,最后一对表示Leyni的坐标。坐标的范围在[-10000, 10000],且都是整数。
注:输入数据保证Leyni不会在三角形的边上。
Output
对于每组测试数据:
第一行,如果Leyni还有一线生机,那么请输出Live,否则请输出Die
Sample Input
-1 -1 1 -1 0 1 0 0
-1 -1 1 -1 0 1 10 10
Sample Output
Die
Live
Author
齐达拉图
计算几何,基础题,判断点是否在三角形内。
这道题可以用来练习叉积的应用,思路是如果Leyni位置的点与三个顶点构成的向量与三个边代表的向量(必须是一个顺序,都是逆时针,或都是顺时针)的叉积都大于0或者都小于0,则Leyni在三角形内。需要注意的是不能只考虑一种情况,例如只考虑>0的情况,而不考虑<0的情况,这样会WA。
复杂代码:
#include <stdio.h>
#define eps 1e-10
typedef struct{ //定义点
double x,y;
}Point;
typedef Point Vector; //定义向量
double Cross(Vector a,Vector b) //叉积
{
return a.x*b.y - a.y*b.x;
}
bool isn(Point a,Point b,Point c,Point p) //判断点p是否在三角形abc内
{
Vector ab,bc,ca; //创建向量
Vector ap,bp,cp;
ab.x = b.x - a.x;ab.y = b.y - a.y; //向量赋值
bc.x = c.x - b.x;bc.y = c.y - b.y;
ca.x = a.x - c.x;ca.y = a.y - c.y;
ap.x = p.x - a.x;ap.y = p.y - a.y;
bp.x = p.x - b.x;bp.y = p.y - b.y;
cp.x = p.x - c.x;cp.y = p.y - c.y; if( Cross(ab,ap)>eps && Cross(bc,bp)>eps && Cross(ca,cp)>eps ||
Cross(ab,ap)<eps && Cross(bc,bp)<eps && Cross(ca,cp)<eps) //核心代码。注意不能单考虑一种情况(>0 或 <0)
return true;
else
return false;
}
int main()
{
int x1,y1,x2,y2,x3,y3,xLeyni,yLeyni;
while(scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&xLeyni,&yLeyni)!=EOF){ //输入四对坐标
if( (xLeyni==x1 && yLeyni==y1) ||
(xLeyni==x2 && yLeyni==y2) ||
(xLeyni==x3 && yLeyni==y3) ){
printf("Die\n"); //如果Leyni正好在三角形顶点上,则直接死掉
continue;
}
Point a,b,c,p; //将输入的坐标赋给点
a.x = double(x1),a.y = double(y1);
b.x = double(x2),b.y = double(y2);
c.x = double(x3),c.y = double(y3);
p.x = double(xLeyni),p.y = double(yLeyni); if(isn(a,b,c,p)) //判断是否在三角形内
printf("Die\n");
else
printf("Live\n");
}
return ;
}
优化代码:
#include <stdio.h>
typedef struct { //定义点
double x,y;
}Point;
double Cross(Point a,Point b,Point c) //三点求叉积
{
return (b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x);
}
int main()
{
Point a,b,c,p; //定义四点
while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&p.x,&p.y)!=EOF){ //输入四对坐标
if( (Cross(a,b,p)> && Cross(b,c,p)> && Cross(c,a,p)>) ||
(Cross(a,b,p)< && Cross(b,c,p)< && Cross(c,a,p)<) ) //判断点p是否在三角形abc内。注意不能只考虑一种情况(<0 或 >0)
printf("Die\n");
else
printf("Live\n");
}
return ;
}
Freecode : www.cnblogs.com/yym2013
hrbustoj 1142:围困(计算几何基础题,判断点是否在三角形内)的更多相关文章
- hrbustoj 1429:凸多边形(计算几何,判断点是否在多边形内,二分法)
凸多边形 Time Limit: 2000 MS Memory Limit: 65536 K Total Submit: 130(24 users) Total Accepted: 40(1 ...
- 2D空间中判断一点是否在三角形内
要注意如果是XY坐标轴的2D空间,要取差乘分量z而不是y. 实现原理是,将三角形ABC三个边(AB,BC,CA)分别与比较点判断差乘,如果这3个差乘结果表示的方向一致,说明就在三角形内. 效果: 代码 ...
- 【TOJ 3005】Triangle(判断点是否在三角形内+卡精度)
描述 Given the coordinates of the vertices of a triangle,And a point. You just need to judge whether t ...
- B - Toy Storage(POJ - 2398) 计算几何基础题,比TOYS多了个线段排序
Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing ...
- POJ 2398 - Toy Storage - [计算几何基础题][同POJ2318]
题目链接:http://poj.org/problem?id=2398 Time Limit: 1000MS Memory Limit: 65536K Description Mom and dad ...
- POJ 2318 - TOYS - [计算几何基础题]
题目链接:http://poj.org/problem?id=2318 Time Limit: 2000MS Memory Limit: 65536K Description Calculate th ...
- 算法复习——计算几何基础(zoj1081)
题目: Statement of the Problem Several drawing applications allow us to draw polygons and almost all o ...
- hrbustoj 1306:再遇攻击(计算几何,判断点是否在多边形内,水题)
再遇攻击 Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 253(37 users) Total Accepted: 56(2 ...
- nyis oj 68 三点顺序 (计算几何基础)
三点顺序 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 如今给你不共线的三个点A,B,C的坐标,它们一定能组成一个三角形,如今让你推断A,B,C是顺时针给出的还是逆 ...
随机推荐
- 根据sys.database_mirroring查询镜像数据库同步状态
SELECT * FROM sys.database_mirroring WHERE database_id =DB_ID('dbname') 主要查看mirroring_state字段和值和mirr ...
- alitomcat maven以及Autoconfig
maven概述 Maven的核心是POM(Project Object Model),即项目对象模型.最直观的,maven对项目依赖进行统一的管理,让开发者从纷杂错乱的jar包世界摆脱出来,更加专注于 ...
- phpredis中文手册——《redis中文手册》 php版(转)
redis中文手册:http://readthedocs.org/docs/redis/en/latest/ 本文是参考<redis中文手册>,将示例代码用php来实现,注意php-red ...
- [hdu 4959]Poor Akagi 数论(卢卡斯数,二次域运算,等比数列求和)
Poor Akagi Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- MySQL中group_concat函数
本文通过实例介绍了MySQL中的group_concat函数的使用方法,比如select group_concat(name) .MySQL中group_concat函数完整的语法如下:group_c ...
- python基础之Event对象、队列和多进程基础
Event对象 用于线程间通信,即程序中的其一个线程需要通过判断某个线程的状态来确定自己下一步的操作,就用到了event对象 event对象默认为假(Flase),即遇到event对象在等待就阻塞线程 ...
- C#:复选框操作类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- php扩展swoole的安装
这个明星php安装是要装php-pear yum install php-pear 然后通过pear命名安装swoole pecl install swoole 配置php.ini 添加 extens ...
- 获取http内容的php函数
实现获取http内容的php函数. 代码如下: <?php function http_open($url, $data, $cookie = null, $method = "GET ...
- Xilinx问题查找
1.登录https://www.xilinx.com/ 2.在All下拉菜单选择Support选项查找技术问题,All选项会查找全部关键词,但是大多数我们只需要技术相关的内容