?? 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;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -