Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

line clipping algo help

canatancanatan Member Posts: 1
i can not figure out where i'm going wrong with this algo. its supposed to clip lines using the cohen-sutherland line clipping technique..
i'm pasting the various functions and code.. please help...
abrl[] are arrays.
xmax, ymax, xmin, ymin are the coordinates of the window.
abrl[0]==1 if coordinate is left of window,
abrl[1]==1 if coordinate is right
abrl[2]==1 if below
abrl[3]==1 if above..
if both abrl of both the coordinates are 0 then we draw the line
otherwise if by and-ing the arbl bits we get a 1 at any bit we dont draw the line
in the third case we find the interesection of the line with max and min of the window till we get to condition 1 or 2
[CODE]void Clineclipping1View::intersection(int &x1, int &y1, double m, double c, bool abrl[])
{
if(abrl[0]==1)
{
y1=ymax;
x1=((ymax-c)/m);
}
else if(abrl[1]==1)
{
y1=ymin;
x1=((ymin-c)/m);
}
else if(abrl[2]==1)
{
x1=xmax;
y1=((m*xmax)+c);
}
else if(abrl[3]==1)
{
x1=xmin;
y1=((m*xmin)+c);
}

}

void Clineclipping1View::clipline(int x1, int y1, int x2, int y2,CDC *pDC)
{int count=0;
int px1=x1;
int px2=x2;
int py1=y1;
int py2=y2;
draw=false;
done=false;
float m=(y2-y1)/(x2-x1);
float c= (y1-(m*x1));

while(!done)
{
regioncode(px1,py1,abrl1);
regioncode(px2,py2,abrl2);
//line outside check
for(int i=0;i<4;i++)
{
if(abrl1[i]==1)
if(abrl2[i]==1)
count++;
}
if (count==4)
{
draw=false;
done=true;
}
//line inside check
count=0;
for(int i=0;i<4;i++)
{
if((abrl1[i]|abrl2[i])==0)
count++;
}
if(count==4)
{
draw=true;
done=true;
}
if(!done)
{intersection(px1,py1,m,c,abrl1);
intersection(px2,py2,m,c,abrl2);
}
}
if(done)
{
if(draw)
{
pDC->MoveTo(px1,py1);
pDC->LineTo(px2,py2);
}


}
}
[/CODE]
[CODE]void Clineclipping1View::intersection(int &x1, int &y1, double m, double c, bool abrl[])
{
if(abrl[0]==1)
{
y1=ymax;
x1=((ymax-c)/m);
}
else if(abrl[1]==1)
{
y1=ymin;
x1=((ymin-c)/m);
}
else if(abrl[2]==1)
{
x1=xmax;
y1=((m*xmax)+c);
}
else if(abrl[3]==1)
{
x1=xmin;
y1=((m*xmin)+c);
}

}[/CODE]
[CODE]void Clineclipping1View::regioncode(int x1, int x2, bool abrl[])
{
for(int i=0;i<4;i++)
abrl[i]=0;
if((x1-xmin)<0)
abrl[3]=1;
else if((xmax-x1)<0)
abrl[2]=1;
if((x2-ymin)<0)
abrl[1]=1;
else if((ymax-x2)<0)
abrl[0]=1;


}[/CODE]
Sign In or Register to comment.