c - Sutherland Hodgman多边形裁剪算法

标签 c bgi

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( );
}

代码写得很好,我在理解代码中定义的 clip 函数方面遇到了真正的问题。由于我是C的新手,我无法理解,谁能解释一下clip函数中涉及的算法?

最佳答案

您提供的算法不是针对任意多边形裁剪的 Sutherland-Hodgman 算法,而是针对矩形视口(viewport)裁剪的 Cohen-Sutherland 算法。代码片段好像直接取自对应的Wikipedia article .那篇文章解释了算法,还逐步解释了代码示例,因为它们的版本包含许多有用的注释。主要思想是使用 4 位代码对线端点相对于视口(viewport)的位置进行分类,这使得可以基于简单的按位运算实现实际裁剪。

假设您了解基本的编程结构(如循环、if、...)、基本算术和算法的基本工作原理(可能借助维基百科),那么理解代码示例的关键是保持请记住,C 运算符 |& 实现按位运算。

如果您真的在寻找 Sutherland-Hodgman-Algorithm 来裁剪任意多边形,那么请完全忘记此代码示例。您从中获得它的来源要么错误地将其归因于 Sutherland-Hodgman-Algorithm,要么您误解了它,因为这两种算法都归因于 Ivan Sutherland,但它们是根本不同的算法。

关于c - Sutherland Hodgman多边形裁剪算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7715040/

相关文章:

c++ - 为什么 nl_langinfo(CODESET) 与语言环境字符映射不同?

c++ - Codeblocks 中的图形程序

c - 哪里可以找到 'The Art and Science of C'源代码?

c - getch() 将回车等同于 esc 键

c++ - Wx Dev C++ 与 Graphics.h?

将 uint8_t 转换为十六进制字符串(2 位数字)

c - wprintf : %p with NULL pointer

转换为函数

编译器找不到 sstream?

c - 在 vim 中为 gtk+-2.0 设置 clang_complete