1:求两个平面相交所形成的直线:
bool GetIntersectionLine (D3DXPLANE& plane01,D3DXPLANE& plane02,CRayLine& line)
{
D3DXPlaneNormalize(&plane01,&plane01);
D3DXPlaneNormalize(&plane02,&plane02);
D3DXVECTOR3 plane01Normal=-D3DXVECTOR3(plane01.a,plane01.b,plane01.c);
D3DXVECTOR3 plane02Normal=-D3DXVECTOR3(plane02.a,plane02.b,plane02.c);
D3DXVECTOR3 tempD;
D3DXVec3Cross(&tempD,&plane01Normal,&plane02Normal);
if (D3DXVec3Length(&tempD)<0.0001)return false;
line.setDirection(tempD);
float s1,s2,a,b;
s1=plane01.d;s2=plane02.d;
float n1n2dot=D3DXVec3Dot(&plane01Normal,&plane02Normal);
float n1normsqr=D3DXVec3Dot(&plane01Normal,&plane01Normal);
float n2normsqr=D3DXVec3Dot(&plane02Normal,&plane02Normal);
a=(s2*n1n2dot-s1*n2normsqr)/(n1n2dot*n1n2dot-n1normsqr*n2normsqr);
b=(s1*n1n2dot-s2*n2normsqr)/(n1n2dot*n1n2dot-n1normsqr*n2normsqr);
line.setOrigin(a*plane01Normal+b*plane02Normal);
return true;
}
2:求直线与平面的交点
bool Tool::LineIntersectPlane(const CRayLine& line,const D3DXPLANE& plane,float& t,D3DXVECTOR3& intersection)
{
float s1=D3DXVec3Dot(&line.getDirection(),&D3DXVECTOR3(plane.a,plane.b,plane.c));
float s2=line.getOrigin().x*plane.a +line.getOrigin().y*plane.b+line.getOrigin().z*plane.c +plane.d;
if(abs(s1)<0.00001)
{
if(abs(s2)<0.00001)
{
t=0;
return true;
}
else
{return false;}
}
t=-s2;
t=t/s1;
intersection=line.getOrigin()+t*line.getDirection();
return true;
}
|
利用DirectX写的二个常用函数
最新推荐文章于 2021-10-23 11:31:54 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)