HDU 2436 - Collision Detection

转载 2015年11月17日 19:35:05

Collision Detection


记圆心为(x0, y0, z0)。
容易想到,计算长方体上离圆心最近一点(x', y', z')到圆心的距离dismin就可以判断出YES和NO。
由于“长方体的每条边和坐标轴平行”(这个条件非常重要),可以知道长方体上任意一点左边(x, y, z)满足xmin<=x<=xmax, y,z相同。而xmin, xmax等可以直接由长方体的八个顶点坐标得到。
从dismin^2=(x0-x)^2+(y0-y)^2+(z0-z)^2可以知道,要找到(x', y', z')这一点,其实xyz三个那种歌方向上是完全独立的,分别在xmin<=x<=xmax, ymin<=y<=ymax, zmin<=z<=zmax中间选取合适的x, y, z使(x0-x)^2、(y0-y)^2和(z0-z)^2都最小就可以了。


Description

      In physical simulations, video games and computational geometry, collision detection involves algorithms for checking for collision, i.e. intersection, of two given objects. Collision detection algorithms are a basic component of 3D video games. Without them, characters could go through walls and other obstacles. 
      Here comes an interesting problem, given a ball and a cuboid, you need to detect whether they collide. We say that two objects collide if and only if they share at least one point. 
 

Input

      The first line of input is the number of test cases. 
      Each test case contains two lines, the first line contains 24 integers X1, Y1, Z1, …, X8, Y8, Z8, representing the 8 vertexes of the cuboid. Vertexes are given in random order and you can make sure that all edges of the cuboid are parallel to coordinate axes; the second line contains 4 integers X,Y,Z,R representing the central point of the ball and its radius. All integers given are non-negative and will be less than 100000. 
 

Output

      For each case output "Yes" Or "No" on a single line.
 

Sample Input

2 0 0 0 0 0 1 0 1 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 1 2 2 2 2 0 0 0 0 0 1 0 1 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 1 2 2 2 1
 

Sample Output

Yes No
 


#include<iostream> #include<algorithm> using namespace std; const int INF = 1 << 30; struct Point { int x, y, z; } P[8]; struct Ball { int x, y, z, r; } B; int maxx, maxy, maxz, minx, miny, minz; inline void rebuild() { maxx = maxy = maxz = -INF; minx = miny = minz = INF; for(int i = 0; i < 8; i++) { maxx = max(P[i].x, maxx); maxy = max(P[i].y, maxy); maxz = max(P[i].z, maxz); minx = min(P[i].x, minx); miny = min(P[i].y, miny); minz = min(P[i].z, minz); } } //intersect in every aspect inline bool judge() { long long dx = 0; long long dy = 0; long long dz = 0; long long R = (long long)B.r * B.r; if(maxx >= B.x && minx <= B.x) dx = 0; else dx = min((long long)(maxx - B.x) * (maxx - B.x), (long long)(minx - B.x) * (minx - B.x)); if(maxy >= B.y && miny <= B.y) dy = 0; else dy = min((long long)(maxy - B.y) * (maxy - B.y), (long long)(miny - B.y) * (miny - B.y)); if(maxz >= B.z && minz <= B.z) dz = 0; else dz = min((long long)(maxz - B.z) * (maxz - B.z), (long long)(minz - B.z) * (minz - B.z)); return dx + dy + dz <= R; } int main() { int T; cin>>T; while( T-- ) { for(int i = 0; i < 8; i++) cin>>P[i].x>>P[i].y>>P[i].z; cin>>B.x>>B.y>>B.z>>B.r; rebuild(); if(judge()) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0; }

【计算几何】 HDOJ 2436 Collision Detection

问一个长方形和一个圆有没有接触。。。。 对每一维独立考虑,每一维都取最接近的值就行了。。。。 #include #include #include #include #include...

ACM Collision detection

Collision detection Time Limit:1000MS  Memory Limit:65536K Total Submit:44 Accepted:17 Descriptio...

How To Use Box2D For Just Collision Detection with Cocos2D iPhone

How To Use Box2D For Just Collision Detection with Cocos2D iPhoneLike this post? Follow me on Twitte...

Sicily 1907. Collision Detection

1907. Collision Detection Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description...

Create a Brick Breaker Game with the Corona SDK: Collision Detection

原创文章,转载请注明: 转载自All-iPad.net 本文链接地址: Create a Brick Breaker Game with the Corona SDK: Collision Detec...
  • lfhfut
  • lfhfut
  • 2011年05月15日 00:31
  • 825

Real-Time Rendering (9) - 碰撞检测(Collision Detection)

提要       碰撞检测(CD)在很多图形应用中都有着极其重要的角色,CAD/CAM,游戏动画,基于物理的建模,基本上所有的虚拟现实模拟都会用到,      我们常说的CD其实指的是Collisio...

How To Use Box2D For Just Collision Detection with cocos2d-iphone

原文地址:http://www.raywenderlich.com/606/box2d-tutorial-for-ios-how-to-use-box2d-for-just-collision-det...
  • cubesky
  • cubesky
  • 2014年08月22日 11:52
  • 890

空间划分(real time collision detection chapter 7)

本章介绍三种空间划分方法:均匀网格划分、分层网格划分、树划分 7.1 均匀网格划分 7.1.1 链表存储 定义一个数组,然后将网格单元(grid cell)与数组元素一一对应。当一个单元中...

Real-Time Collision Detection 英文版

  • 2017年03月10日 18:08
  • 249KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HDU 2436 - Collision Detection
举报原因:
原因补充:

(最多只允许输入30个字)