📄 collision.cpp
字号:
#include "collision.h"
//--------------------2LinesIntersection2D-------------------------
//
// Given 2 lines in 2D space AB, CD this returns true if an
// intersection occurs and sets dist to the distance the intersection
// occurs along AB
//
//-----------------------------------------------------------------
bool LineIntersection2D(const SPoint A,
const SPoint B,
const SPoint C,
const SPoint D,
double &dist)
{
double rTop = (A.y-C.y)*(D.x-C.x)-(A.x-C.x)*(D.y-C.y);
double rBot = (B.x-A.x)*(D.y-C.y)-(B.y-A.y)*(D.x-C.x);
double sTop = (A.y-C.y)*(B.x-A.x)-(A.x-C.x)*(B.y-A.y);
double sBot = (B.x-A.x)*(D.y-C.y)-(B.y-A.y)*(D.x-C.x);
if ( (rBot == 0) || (sBot == 0))
{
//lines are parallel
return false;
}
double r = rTop/rBot;
double s = sTop/sBot;
if( (r > 0) && (r < 1.0f) && (s > 0) && (s < 1.0f) )
{
dist = r;
return true;
}
else
{
dist = 0;
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -