今天,以3D为主,
仍然一步步地进行。
首先加了个3D参数化直线,类似于参数化2D直线。
//参数化D直线
typedef struct PARMLINE3D_TYP
{
POINT3D p0; //起点
POINT3D p1; //终点
VECTOR3D v; //方向向量p0->p1
}PARMLINE3D, * PARMLINE3D_PTR;
3D点积
float ddraw_math::VECTOR3D_DOT( VECTOR3D_PTR va, VECTOR3D_PTR vb )
{
return ( ( va->x * vb->x ) + ( va->y * vb->y ) + ( va->z + vb->z ) );
}
增加一个可能:线在平面上
#define PARM_LINE_INTERSECT_EVERYWHERE 3
int ddraw_math::Intersect_Parm_Lines3D_Plane3D(PARMLINE3D_PTR pline, PLANE3D_PTR plane, float * t, POINT3D_PTR pt )
{
float plane_dot_line = VECTOR3D_DOT( &pline->v, &plane->n );
if ( fabs(plane_dot_line) <= EPSILON_E5 )
{
if( fabs( Compute_Point_In_Plane3D( & pline->p0, plane ) ) <= EPSILON_E5 )
return PARM_LINE_INTERSECT_EVERYWHERE;
else
return PARM_LINE_NO_INTERSECT;
}
*t = - ( plane->n.x * pline->p0.x +plane->n.y * pline->p0.y +plane->n.z * pline->p0.z - plane->n.x * plane->n.x - plane->n.y * plane->n.y - plane->n.z * plane->n.z ) / plane_dot_line;
pt->x = pline->p0.x + pline->v.x * ( *t );
pt->y = pline->p0.y + pline->v.y * ( *t );
pt->z = pline->p0.z + pline->v.z * ( *t );
if ( ( * t >= 0 ) && ( * t <= 1 ) )
{
return ( PARM_LINE_INTERSECT_IN_SEGMENT );
}
else
{
return (PARM_LINE_INTERSECT_OUT_SEGMENT );
}
}
计算参数形式3D直线
void ddraw_math::Compute_Parm_Line3D( PARMLINE3D_PTR p, float t, POINT3D_PTR pt )
{
pt->x = p->p0.x+ p->v.x * t;
pt->y = p->p0.y+ p->v.y * t;
pt->z = p->p0.z+ p->v.z * t;
}
void ddraw_math ::Init_Parm_Line3D( POINT3D_PTR p_init, POINT3D_PTR p_term, PARMLINE3D_PTR p )
{
VECTOR3D_INIT( & ( p->p0 ), p_init );
VECTOR3D_INIT( & ( p->p1), p_term );
VECTOR3D_Build( p_init, p_term, & ( p->v));
}
void ddraw_math::VECTOR3D_Build( VECTOR3D_PTR init, VECTOR3D_PTR term, VECTOR3D_PTR result )
{
result->x = term->x -init->x;
result->y = term->y - init->y;
result->z = term->z - init->z;
}