void clipline int x1 int y1 int x2 int y2 int X1 int Y1 int X2 int Y2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
void clipline(int x1, int y1, int x2, int y2, int X1, int Y1, int X2, int Y2)
{
Graphics g = Graphics.FromHwnd(this.Handle);
int code1=outCode(x1,y1,X1,Y1,X2,Y2);
int code2=outCode(x2,y2,X1,Y1,X2,Y2);
bool outside=(code1|code2)==0;
bool inside = (code1&code2)!=0;
while(!outside && !inside){
if(code1==0){
swap(x1,x2);
swap(y1,y2);
swap(code1,code2);
}
if((code1 & 0x01)!=0)
{
y1+=(y2-y1)*(X1-x1)/(x2-x1);
x1=X1;
}
if((code1&0x02)!=0)
{
x1+=(x2-x1)*(Y1-y1)/(y2-y1);
y1=Y1;
}
if((code1&0x04)!=0)
{
y1+=(y2-y1)*(X2-x1)/(x2-x1);
x1=X2;
}
if((code1&0x08)!=0)
{
x1+=(x2-x1)*(Y2-y1)/(y2-y1);
y1=Y2;
}
code1=outCode(x1,y1,X1,Y1,X2,Y2);
code2=outCode(x2,y2,X1,Y1,X2,Y2);
outside=(code1|code2)==0;
inside = (code1&code2)!=0;
}
g.DrawLine(redBrush,x1,y1,x2,y2);
}