C Sutherland-Hodgman多边形裁剪算法

C Sutherland-Hodgman多边形裁剪算法,c,graphics,C,Graphics,Sutherland-hodgeman多边形裁剪算法是我们感兴趣的裁剪或只获取给定多边形的某些特定部分的算法。我知道剪辑的概念,我在网上看到了以下代码: #include <stdio.h> #include <graphics.h> #include <conio.h> #include <math.h> #include <process.h> #define TRUE 1 #define FALSE 0 typedef unsig

Sutherland-hodgeman多边形裁剪算法是我们感兴趣的裁剪或只获取给定多边形的某些特定部分的算法。我知道剪辑的概念,我在网上看到了以下代码:

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y);
enum  {  TOP = 0x1,
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
float xmin,xmax,ymin,ymax;
void clip(float x0,float y0,float x1,float y1)
{
outcode outcode0,outcode1,outcodeOut;
int accept = FALSE,done = FALSE;
outcode0 = CompOutCode(x0,y0);
outcode1 = CompOutCode(x1,y1);
do
{
    if(!(outcode0|outcode1))
    {
        accept = TRUE;
        done = TRUE;
    }
    else
    if(outcode0 & outcode1)
        done = TRUE;
    else
    {
        float x,y;
        outcodeOut = outcode0?outcode0:outcode1;
        if(outcodeOut & TOP)
        {
            x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
            y = ymax;
        }
        else if(outcodeOut & BOTTOM)
        {
            x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
            y = ymin;
        }
        else if(outcodeOut & RIGHT)
        {
            y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
            x = xmax;
        }
        else
        {
            y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
            x = xmin;
        }
        if(outcodeOut==outcode0)
        {
            x0 = x;
            y0 = y;
            outcode0 = CompOutCode(x0,y0);
        }
        else
        {
            x1 = x;
            y1 = y;
            outcode1 = CompOutCode(x1,y1);
          }
    }
}while(done==FALSE);
if(accept)
    line(x0,y0,x1,y1);
outtextxy(150,20,"POLYGON AFTER CLIPPING");
rectangle(xmin,ymin,xmax,ymax);
}
outcode CompOutCode(float x,float y)
{
    outcode code = 0;
    if(y>ymax)
        code|=TOP;
    else if(y<ymin)
            code|=BOTTOM;
    if(x>xmax)
        code|=RIGHT;
    else if(x<xmin)
        code|=LEFT;
    return code;
}
void main( )
{
float x1,y1,x2,y2;
/* request auto detection */
int gdriver = DETECT, gmode, n,poly[14],i;
clrscr( );
printf("Enter the no of sides of polygon:");
scanf("%d",&n);
printf("\nEnter the coordinates of polygon\n");
for(i=0;i<2*n;i++)
{
    scanf("%d",&poly[i]);
}
poly[2*n]=poly[0];
poly[2*n+1]=poly[1];
printf("Enter the rectangular coordinates of clipping window\n");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

outtextxy(150,20,"POLYGON BEFORE CLIPPING");
drawpoly(n+1,poly);
rectangle(xmin,ymin,xmax,ymax);
getch( );
cleardevice( );
for(i=0;i<n;i++)
clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]);
getch( );
restorecrtmode( );
}
#包括
#包括
#包括
#包括
#包括
#定义真1
#定义FALSE 0
typedef无符号整数输出码;
输出代码复合代码(浮点x,浮点y);
枚举{TOP=0x1,
底部=0x2,
右=0x4,
左=0x8
};
浮点xmin、xmax、ymin、ymax;
空心夹(浮子x0、浮子y0、浮子x1、浮子y1)
{
outcode outcode0、outcode1、outcodeOut;
int accept=FALSE,done=FALSE;
outcode0=复合代码(x0,y0);
outcode1=复合代码(x1,y1);
做
{
如果(!(输出代码0 |输出代码1))
{
接受=真;
完成=正确;
}
其他的
如果(输出代码0和输出代码1)
完成=正确;
其他的
{
浮动x,y;
outcodeOut=outcode0?outcode0:outcode1;
如果(输出代码输出和顶部)
{
x=x0+(x1-x0)*(ymax-y0)/(y1-y0);
y=ymax;
}
else if(输出代码输出和底部)
{
x=x0+(x1-x0)*(ymin-y0)/(y1-y0);
y=ymin;
}
else if(输出代码输出&右侧)
{
y=y0+(y1-y0)*(xmax-x0)/(x1-x0);
x=xmax;
}
其他的
{
y=y0+(y1-y0)*(xmin-x0)/(x1-x0);
x=xmin;
}
if(outcodeOut==outcode0)
{
x0=x;
y0=y;
outcode0=复合代码(x0,y0);
}
其他的
{
x1=x;
y1=y;
outcode1=复合代码(x1,y1);
}
}
}while(done==FALSE);
如果(接受)
线(x0,y0,x1,y1);
outtextxy(150,20,“剪裁后的多边形”);
矩形(xmin,ymin,xmax,ymax);
}
输出代码复合输出代码(浮点x、浮点y)
{
输出代码=0;
如果(y>ymax)
代码|=顶部;
else if(yxmax)
代码|=右;

否则如果(x您介绍的算法不是针对任意多边形进行剪裁的Sutherland-Hodgman算法,而是针对矩形视口进行剪裁的Cohen-Sutherland算法。代码片段似乎直接取自相应的。这篇文章解释了算法,并逐步解释了代码示例,因为他们的版本包含许多有用的注释。主要思想是使用4位代码对线端点相对于视口的位置进行分类,这使得实际剪裁可以基于简单的按位操作实现

假设您了解基本编程结构(如循环、if…)、基本算术和算法的基本工作原理(可能借助Wikipedia),那么理解代码示例的关键是要记住C运算符
&
实现位运算


如果你真的在寻找Sutherland Hodgman算法来裁剪任意多边形,那么就完全忘记这个代码示例。你从中获得的源代码要么错误地将其归因于Sutherland Hodgman算法,要么由于两个算法都归因于Ivan Sutherland,而se是根本不同的算法。

一步一步地向我解释这一点
不适合这个网站。试着找出一些特定的问题,让我们发布有针对性的答案。应该从哪里开始回答你?解释什么是
浮动
吗?解释
包含
的作用?…解释什么
clip
可以。这只是
clip
的一个算法。事实上,这是一个很好的注释,我编辑了我的问题。检查这里的算法: