www.pudn.com > 2.rar > 2.c
#define LEN sizeof(struct node) #include#include #include 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] 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] 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] 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] 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] 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] 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] 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] 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(); }