标题:Sutherland-Hodgman 算法(多边形裁剪算法)
只看楼主
john_163
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2005-10-24
 问题点数:0 回复次数:3 
Sutherland-Hodgman 算法(多边形裁剪算法)

我下面这个程序在tc调试有警告,请大家帮我改下,谢谢
/* Sutherland-Hodgman 算法 */
#define LEN sizeof(struct node)
#include <math.h>
#include<stdio.h>
#include<graphics.h>
struct node {
int dx,dy;
struct node *next;
};
struct node *creat()
{ struct node *h,*q,*r;
int p[8][2]={100,120,160,50,180,100,200,80,240,160,210,220,170,160,140,190};
int i;
setcolor(12);
for (i=0;i<7;i++) line(p[i][0],p[i][1],p[i+1][0],p[i+1][1]);
line(p[0][0],p[0][1],p[7][0],p[7][1]);
rectangle(120,200,230,70);
h=NULL;
for (i=0;i<8;i++)
{ q=(struct node *)malloc(LEN);
q->dx=p[i][0]; q->dy=p[i][1];
if (h==NULL) h=q;
else r->next=q;
r=q;
}
r->next=NULL;
return(h);
}
struct node *builx(h,x)
struct node *h;
int x;
{int s[2],j[2];
struct node *hh,*p,*r,*q;
int max,min;
p=h; hh=NULL;
s[0]=p->dx; s[1]=p->dy;
p=p->next;
while (p!=NULL)
{ j[0]=x;
j[1]=s[1]+(p->dy-s[1])*(x-s[0])/(p->dx-s[0]);
max=s[0]; min=p->dx;
if (s[0]<p->dx) { max=p->dx; min=s[0]; }
if ((j[0]>=min)&&(j[0]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0]; q->dy=j[1];
if (hh==NULL) hh=q;
else r->next=q;
r=q;
}
if (p->dx>=x)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx; q->dy=p->dy;
if (hh==NULL) hh=q;
else r->next=q;
r=q;
}
s[0]=p->dx; s[1]=p->dy;
p=p->next;
}
p=h;
j[0]=x; j[1]=s[1]+(p->dy-s[1])*(x-s[0])/(p->dx-s[0]);
max=s[0]; min=p->dx;
if (s[0]<p->dx) { max=p->dx; min=s[0]; }
if ((j[0]>=min)&&(j[0]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0]; q->dy=j[1];
if (hh==NULL) hh=q;
else r->next=q;
r=q;
}
if (p->dx>=x)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx; q->dy=p->dy;
if (hh==NULL) hh=q;
else r->next=q;
r=q;
}
r->next=NULL;
return(hh);
}

struct node *builxx(h,x)
struct node *h;
int x;
{int s[2],j[2];
struct node *hh,*p,*r,*q;
int max,min;
p=h;
hh=NULL;
s[0]=p->dx;
s[1]=p->dy;
p=p->next;
while (p!=NULL)
{
j[0]=x;
j[1]=s[1]+(p->dy-s[1])*(x-s[0])/(p->dx-s[0]+0.1);
max=s[0];
min=p->dx;
if (s[0]<p->dx) { max=p->dx;
min=s[0];
}
if ((j[0]>=min)&&(j[0]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0];
q->dy=j[1];
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
if (p->dx<=x)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx;
q->dy=p->dy;
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
s[0]=p->dx;
s[1]=p->dy;
p=p->next;
}
p=h;
j[0]=x;
j[1]=s[1]+(p->dy-s[1])*(x-s[0])/(p->dx-s[0]+0.1);
max=s[0];
min=p->dx;
if (s[0]<p->dx) { max=p->dx;
min=s[0];
}
if ((j[0]>=min)&&(j[0]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0];
q->dy=j[1];
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
if (p->dx<=x)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx;
q->dy=p->dy;
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
r->next=NULL;
return(hh);
}

struct node *buily(h,y)
struct node *h;
int y;
{int s[2],j[2];
struct node *hh,*p,*r,*q;
int max,min;
p=h;
hh=NULL;
s[0]=p->dx;
s[1]=p->dy;
p=p->next;
while (p!=NULL)
{
j[1]=y;
j[0]=s[0]+(p->dx-s[0])*(y-s[1])/(p->dy-s[1]+0.1);
max=s[1];
min=p->dy;
if (s[1]<p->dy) { max=p->dy;
min=s[1];
}
if ((j[1]>=min)&&(j[1]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0];
q->dy=j[1];
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
if (p->dy>=y)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx;
q->dy=p->dy;
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
s[0]=p->dx;
s[1]=p->dy;
p=p->next;
}
p=h;
j[1]=y;
j[0]=s[0]+(p->dx-s[0])*(y-s[1])/(p->dy-s[1]+0.1);
max=s[1];
min=p->dy;
if (s[1]<p->dy) { max=p->dy;
min=s[1];
}
if ((j[1]>=min)&&(j[1]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0];
q->dy=j[1];
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
if (p->dy>=y)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx;
q->dy=p->dy;
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
r->next=NULL;
return(hh);
}

struct node *builyy(h,y)
struct node *h;
int y;
{int s[2],j[2];
struct node *hh,*p,*r,*q;
int max,min;
p=h;
hh=NULL;
s[0]=p->dx;
s[1]=p->dy;
p=p->next;
while (p!=NULL)
{
j[1]=y;
j[0]=s[0]+(p->dx-s[0])*(y-s[1])/(p->dy-s[1]+0.1);
max=s[1];
min=p->dy;
if (s[1]<p->dy) { max=p->dy;
min=s[1];
}
if ((j[1]>=min)&&(j[1]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0];
q->dy=j[1];
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
if (p->dy<=y)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx;
q->dy=p->dy;
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
s[0]=p->dx;
s[1]=p->dy;
p=p->next;
}
p=h;
j[1]=y;
j[0]=s[0]+(p->dx-s[0])*(y-s[1])/(p->dy-s[1]+0.1);
max=s[1];
min=p->dy;
if (s[1]<p->dy) { max=p->dy;
min=s[1];
}
if ((j[1]>=min)&&(j[1]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0];
q->dy=j[1];
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
if (p->dy<=y)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx;
q->dy=p->dy;
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
r->next=NULL;
return(hh);
}
void Initialize(void)
{
int gdriver=DETECT, gmode;
initgraph(&gdriver, &gmode," ");
setbkcolor(WHITE);
}
main()
{ int max,min;
struct node *head,*r,*q;
int i;
int s[2];
Initialize();
head=creat();
q=head;
while (q->next!=NULL)
{ putpixel(q->dx,q->dy,14);
q=q->next;
}
putpixel(q->dx,q->dy,14);
q=builx(head,120);
head=q;
while (q->next!=NULL)
{ putpixel(q->dx,q->dy,15);
q=q->next;
}
putpixel(q->dx,q->dy,15);
q=buily(head,70);
head=q;
while (q->next!=NULL)
{ putpixel(q->dx,q->dy,2);
q=q->next;
}
putpixel(q->dx,q->dy,2);
q=builxx(head,230);
head=q;
while (q->next!=NULL)
{ putpixel(q->dx,q->dy,1);
q=q->next;
}
putpixel(q->dx,q->dy,1);
q=builyy(head,200);
head=q;
s[0]=q->dx;
s[1]=q->dy;
q=q->next;
setcolor(14);
while (q!=NULL)
{ line(s[0],s[1],q->dx,q->dy);
s[0]=q->dx;
s[1]=q->dy;
q=q->next;
}
q=head;
line(s[0],s[1],q->dx,q->dy);
getch();
}

搜索更多相关主题的帖子: 多边形 算法 裁剪 
2006-04-07 09:42
john_163
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2005-10-24
得分:0 
怎么没人回我的啊

小路工作室:http:///
2006-04-08 09:40
soft_wind
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:1430
专家分:0
注 册:2006-4-5
得分:0 
吓死我了,我的妈呀,这么长

对不礼貌的女生收钱......
2006-04-08 09:59
seuwwe
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-5-16
得分:0 
回复:(john_163)Sutherland-Hodgman 算法(多边形裁...

/* Sutherland-Hodgman 算法 */
#define LEN sizeof(struct node)
#include <math.h>
#include<stdio.h>
#include<graphics.h>

struct node
{
int dx,dy;
struct node *next;
};
struct node *h,*q,*r;
struct node *creat()
{
int p[8][2]={100,120,160,50,180,100,200,80,240,160,210,220,170,160,140,190};
int i;
setcolor(12);
for (i=0;i<7;i++) line(p[i][0],p[i][1],p[i+1][0],p[i+1][1]);
line(p[0][0],p[0][1],p[7][0],p[7][1]);
rectangle(120,200,230,70);
h=NULL;
for (i=0;i<8;i++)
{
q=(struct node *)malloc(LEN);
q->dx=p[i][0];
q->dy=p[i][1];
if (h==NULL)
h=q;
else r->next=q;
r=q;
}
r->next=NULL;
return(h);
}


int x;

struct node *builx(h,x)
struct node *h;

{
int s[2],j[2];
struct node *hh,*p,*q;

int max,min;
p=h; hh=NULL;
s[0]=p->dx; s[1]=p->dy;
p=p->next;
while (p!=NULL)
{
j[0]=x;
j[1]=s[1]+(p->dy-s[1])*(x-s[0])/(p->dx-s[0]);
max=s[0]; min=p->dx;
if (s[0]<p->dx)
{
max=p->dx;
min=s[0];
}
if ((j[0]>=min)&&(j[0]<=max))
{
q=(struct node *)malloc(LEN);
q->dx=j[0]; q->dy=j[1];
if (hh==NULL)
hh=q;
else
r->next=q;
r=q;
}
if (p->dx>=x)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx; q->dy=p->dy;
if (hh==NULL) hh=q;
else r->next=q;
r=q;
}
s[0]=p->dx; s[1]=p->dy;
p=p->next;
}
p=h;
j[0]=x; j[1]=s[1]+(p->dy-s[1])*(x-s[0])/(p->dx-s[0]);
max=s[0]; min=p->dx;
if (s[0]<p->dx) { max=p->dx; min=s[0]; }
if ((j[0]>=min)&&(j[0]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0]; q->dy=j[1];
if (hh==NULL) hh=q;
else r->next=q;
r=q;
}
if (p->dx>=x)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx; q->dy=p->dy;
if (hh==NULL) hh=q;
else r->next=q;
r=q;
}
r->next=NULL;
return(hh);
}

struct node *builxx(h,x)
struct node *h;
int x;
{int s[2],j[2];
struct node *hh,*p,*q;
int max,min;
p=h;
hh=NULL;
s[0]=p->dx;
s[1]=p->dy;
p=p->next;
while (p!=NULL)
{
j[0]=x;
j[1]=s[1]+(p->dy-s[1])*(x-s[0])/(p->dx-s[0]+0.1);
max=s[0];
min=p->dx;
if (s[0]<p->dx) { max=p->dx;
min=s[0];
}
if ((j[0]>=min)&&(j[0]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0];
q->dy=j[1];
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
if (p->dx<=x)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx;
q->dy=p->dy;
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
s[0]=p->dx;
s[1]=p->dy;
p=p->next;
}
p=h;
j[0]=x;
j[1]=s[1]+(p->dy-s[1])*(x-s[0])/(p->dx-s[0]+0.1);
max=s[0];
min=p->dx;
if (s[0]<p->dx) { max=p->dx;
min=s[0];
}
if ((j[0]>=min)&&(j[0]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0];
q->dy=j[1];
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
if (p->dx<=x)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx;
q->dy=p->dy;
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
r->next=NULL;
return(hh);
}

struct node *buily(h,y)
struct node *h;
int y;
{int s[2],j[2];
struct node *hh,*p,*q;
int max,min;
p=h;
hh=NULL;
s[0]=p->dx;
s[1]=p->dy;
p=p->next;
while (p!=NULL)
{
j[1]=y;
j[0]=s[0]+(p->dx-s[0])*(y-s[1])/(p->dy-s[1]+0.1);
max=s[1];
min=p->dy;
if (s[1]<p->dy) { max=p->dy;
min=s[1];
}
if ((j[1]>=min)&&(j[1]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0];
q->dy=j[1];
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
if (p->dy>=y)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx;
q->dy=p->dy;
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
s[0]=p->dx;
s[1]=p->dy;
p=p->next;
}
p=h;
j[1]=y;
j[0]=s[0]+(p->dx-s[0])*(y-s[1])/(p->dy-s[1]+0.1);
max=s[1];
min=p->dy;
if (s[1]<p->dy) { max=p->dy;
min=s[1];
}
if ((j[1]>=min)&&(j[1]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0];
q->dy=j[1];
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
if (p->dy>=y)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx;
q->dy=p->dy;
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
r->next=NULL;
return(hh);
}

struct node *builyy(h,y)
struct node *h;
int y;
{int s[2],j[2];
struct node *hh,*p,*q;
int max,min;
p=h;
hh=NULL;
s[0]=p->dx;
s[1]=p->dy;
p=p->next;
while (p!=NULL)
{
j[1]=y;
j[0]=s[0]+(p->dx-s[0])*(y-s[1])/(p->dy-s[1]+0.1);
max=s[1];
min=p->dy;
if (s[1]<p->dy) { max=p->dy;
min=s[1];
}
if ((j[1]>=min)&&(j[1]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0];
q->dy=j[1];
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
if (p->dy<=y)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx;
q->dy=p->dy;
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
s[0]=p->dx;
s[1]=p->dy;
p=p->next;
}
p=h;
j[1]=y;
j[0]=s[0]+(p->dx-s[0])*(y-s[1])/(p->dy-s[1]+0.1);
max=s[1];
min=p->dy;
if (s[1]<p->dy) { max=p->dy;
min=s[1];
}
if ((j[1]>=min)&&(j[1]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0];
q->dy=j[1];
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
if (p->dy<=y)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx;
q->dy=p->dy;
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
r->next=NULL;
return(hh);
}
void Initialize(void)
{
int gdriver=DETECT, gmode;
initgraph(&gdriver, &gmode," ");
setbkcolor(WHITE);
}
main()
{ int max,min;
struct node *head,*r,*q;
int i;
int s[2];
Initialize();
head=creat();
q=head;
while (q->next!=NULL)
{ putpixel(q->dx,q->dy,14);
q=q->next;
}
putpixel(q->dx,q->dy,14);
q=builx(head,120);
head=q;
while (q->next!=NULL)
{ putpixel(q->dx,q->dy,15);
q=q->next;
}
putpixel(q->dx,q->dy,15);
q=buily(head,70);
head=q;
while (q->next!=NULL)
{ putpixel(q->dx,q->dy,2);
q=q->next;
}
putpixel(q->dx,q->dy,2);
q=builxx(head,230);
head=q;
while (q->next!=NULL)
{ putpixel(q->dx,q->dy,1);
q=q->next;
}
putpixel(q->dx,q->dy,1);
q=builyy(head,200);
head=q;
s[0]=q->dx;
s[1]=q->dy;
q=q->next;
setcolor(14);
while (q!=NULL)
{ line(s[0],s[1],q->dx,q->dy);
s[0]=q->dx;
s[1]=q->dy;
q=q->next;
}
q=head;
line(s[0],s[1],q->dx,q->dy);
getch();
}

2007-05-16 12:15



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-55713-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.286098 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved