空间范围内剪切盒的生成

效果图

#define ZERO_VALUE 0
#define PI   3.14159265358979

//点pos绕中心点CenterPos旋转angle
float2 RotateCenterPoint(float2 CenterPos,float angle,float2 pos)
{
    return float2(  
                    (pos.x - CenterPos.x) * cos(angle) - (pos.y - CenterPos.y) * sin(angle) + CenterPos.x, 
                    (pos.x - CenterPos.x) * sin(angle) + (pos.y - CenterPos.y) * cos(angle) + CenterPos.y
                  );
}
//判断空间一点CurPos是否在以CenterPos为中心,尺寸为Size的Box内
//angle为Box为中心点旋转的角度
bool IsCurPosInBoxEx(float3 CurPos, float3 CenterPos ,float3 Size ,float angle)
{
    //控制Box的尺寸
    float3 Min = CenterPos - Size.xyz /4.0f;
    float3 Max = CenterPos + Size.xyz / 4.0f;
    //先判断z方向上
    if (CurPos.z < Min.z || CurPos.z > Max.z)
        return false;
    
    float r = angle * PI / 180.0f;
    //计算旋转后的平面四个顶点的坐标,顺时针方向
    float2 A = RotateCenterPoint(CenterPos.xy, r, Min.xy);
    float2 B = RotateCenterPoint(CenterPos.xy, r, float2(Min.x, Max.y));
    float2 C = RotateCenterPoint(CenterPos.xy, r, Max.xy);
    float2 D = RotateCenterPoint(CenterPos.xy, r, float2(Max.x, Min.y));
   // 四边形内的点都在顺时针( 逆时针)向量的同一边,向量积同向。
    float a = (B.x - A.x) * (CurPos.y - A.y) - (B.y - A.y) * (CurPos.x - A.x);
    float b = (C.x - B.x) * (CurPos.y - B.y) - (C.y - B.y) * (CurPos.x - B.x);
    float c = (D.x - C.x) * (CurPos.y - C.y) - (D.y - C.y) * (CurPos.x - C.x);
    float d = (A.x - D.x) * (CurPos.y - D.y) - (A.y - D.y) * (CurPos.x - D.x);
    
    if ((a > ZERO_VALUE && b > ZERO_VALUE && c > ZERO_VALUE && d > ZERO_VALUE) ||
        (a < ZERO_VALUE && b < ZERO_VALUE && c < ZERO_VALUE && d < ZERO_VALUE))
    {
        return true;
    }
    return false;
}

参考1

发布了40 篇原创文章 · 获赞 6 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/mrbaolong/article/details/103417270