gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:53:28 24 4
gpt4 key购买 nike

我有一个用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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com