c# - 通过LiangBarsky算法裁剪线C#

标签 c# algorithm math graphics clipping

我有一个用C实现的Liang Barsky算法,但是有点不对劲,因为它有时返回矩形边界以外的点,就像在这种情况下:
p1=(516546)和p2=(0,0)
r=0;
L=511;
T=511;
B=0;
另外,我怎样才能表明这条线完全在矩形之外

    private void LiangBarsky( PointF P1,PointF P2,float R,float L,float T,float B)
    {
        try
        {

            float Tmin = 0;
            float Tmax = 1;
            float Tl, Tr, Tt, Tb;
            Tl = (L - P1.X) / (P2.X - P1.X);
            Tr = (R - P1.X) / (P2.X - P1.X);
            Tt = (T - P1.Y) / (P2.Y - P1.Y);
            Tb = (B - P1.Y) / (P2.Y - P1.Y);
            if (0 < Tl && Tl < 1.0)
            {
                if (InnerProduct(P1, P2, new Point(0, 1)) == EnteringPoint)
                    Tmin = Tl;
                else
                    Tmax = Tl;
                test = true;
            }
            if (0 < Tr && Tr < 1)
            {
                if (InnerProduct(P1, P2, new Point(0, -1)) == EnteringPoint)
                    Tmin = Tr;
                else
                    Tmax = Tr;
                test = true;
            }
            if (0 < Tt && Tt < 1)
            {
                if (InnerProduct(P1, P2, new Point(1, 0)) == EnteringPoint)
                    Tmin = Tt;
                else
                    Tmax = Tt;
                test = true;
            }
            if (0 < Tb && Tb < 1)
            {
                if (InnerProduct(P1, P2, new Point(-1, 0)) == EnteringPoint)
                    Tmin = Tb;
                else
                    Tmax = Tb;
                test = true;
            }
            if ((Tmin < Tmax) /*&& (Tmin!=0 )&&(Tmax!=1)*/)
            {

                P1 = new PointF((P1.X + (P2.X - P1.X) * Tmin), (P1.Y + (P2.Y - P1.Y) * Tmin));
                P2 = new PointF((P1.X + (P2.X - P1.X) * Tmax), (P1.Y + (P2.Y - P1.Y) * Tmax));
                Indtxt.BackColor = Color.Blue;
            }
            else
            {

                if(P1.Y>T  && P1.X>L && P2.X>L && P2.Y>T)
                    Indtxt.BackColor = Color.Red;
            }
        }
        catch (Exception ex)
        {
        }


    }
    private int InnerProduct(PointF LineP1, PointF LineP2, Point NormalVector)
    {
        PointF P1_P2 = new PointF(LineP2.X - LineP1.X, LineP2.Y - LineP1.Y);
        if (((P1_P2.X * NormalVector.X) + (P1_P2.Y * NormalVector.Y)) <= 0)
            return EnteringPoint;//0 as entering point ans 1 as exiting point
        else
            return ExitingPoint;
    }

我把algorith翻译成前面的代码,这样可能会遗漏一些概念

最佳答案

请看这个链接,它显示了所有代码为C的算法#
http://pastebin.com/NA01gacf

关于c# - 通过LiangBarsky算法裁剪线C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6695085/

相关文章:

python - 为什么我在 Python 中的 RSA 实现不起作用?

javascript - JSON 字符串中的数学方程式

Python 和 Powers 数学

c++ - 计算 3d 中线段之间的垂直距离和角度距离

c# - 禁用 GridViewColumn 调整大小

arrays - 排序数组以找到前 20 个最小的数字

c# - try catch 异常

python - 索引错误 : index 666 is out of bounds for axis 1 with size 501

c# - 使用 Linq to SQL SELECT @@DBTS

c# - 如何在 C# 中使用 Unity UNET 获取本地网络中所有设备的 IP 地址?