#include #include #include #include #include #define round(a) ((int)(a+0.5)) #define line(a,b,c,d) line(a,480-(b),c,480-(d)) int compare(const void *a,const void *b) { return *(int *)a - *(int *)b; } void swap(int &x1,int &y1,int &x2,int &y2) { int t; t=x1; x1=x2; x2=t; t=y1; y1=y2; y2=t; } struct xyz { int ymax; double x; double invsl; struct xyz * next; }; struct xyz * et[480]={0}; struct xyz * aet=0; //just a debugging function void printet() { int i=0; struct xyz * trav; printf("START : PrintET\n"); while(!et[i]) i++; while(i<480) { trav=et[i]; if(trav) { printf("%d: ",i); while(trav) { printf("ym:%d x:%lf 1/n:%lf >>>>",trav->ymax,trav->x,trav->invsl); trav=trav->next; } printf("%%\n"); } i++; } printf("END : PrintET"); } void makeet(int x1,int y1,int x2,int y2) { struct xyz * etn, *trav; if(y1==y2) return; etn=(struct xyz *)malloc(sizeof(xyz)); if(y1>y2) swap(x1,y1,x2,y2); etn->invsl=(1.0*x2-x1)/(y2-y1); etn->ymax=y2; etn->x=(double)x1; etn->next=0; if(!et[y1]) { et[y1]=etn; } else { trav=et[y1]; while(trav->next!=NULL) trav=trav->next; trav->next=etn; } } main() { int x[20],y[20],n,i,j,k,arr[20],gm,gd=DETECT; struct xyz * trav; printf("Enter the number of points:"); scanf("%d",&n); //n=3; printf("Enter the points:"); for(i=0;inext!=NULL) { trav=trav->next; } trav->next=et[i]; et[i]=0; } //move into arr the x's where iymax) arr[j++]=round(trav->x); trav=trav->next; } if(j==0) break; //end the algo //sort arr qsort(arr,j,sizeof(int),compare); //plot using pairs for(k=0;k+1x+=trav->invsl; trav=trav->next; } } getch(); return 0; }