Docsity

Structures-Computer Graphics-Lab Mannual, Lecture notes of Computer Graphics

Toggle sidebar
bg1
#include <stdio.h>
#include <stdlib.h>
typedef struct tEdge {
int yUpper;
float xIntersect, dxPerScan;
struct tEdge *next;
} Edge;
typedef struct tdcPt {
int x;
int y;
} dcPt;
#define WINDOW_HEIGHT 480
void insertEdge(Edge *list, Edge *edge)
{
Edge *p, *q = list;
p = q->next;
while (p != NULL )
{
if ( edge->xIntersect < p->xIntersect ) p = NULL;
else
{
q = p;
p = p->next;
}
}
edge->next = q->next;
q->next = edge;
}
int yNext( int k, int cnt, dcPt *pts)
{
int j;
if ( (k+1) > (cnt-1) ) j = 0;
else j = k+1;
while( pts[k].y == pts[j].y )
{
if ( (j+1) > (cnt-1) ) j = 0;
else j++;
}
return(pts[j].y);
}
void makeEdgeRec( dcPt lower, dcPt upper, int yComp, Edge *edge, Edge
*edges[])
{
docsity.com
pf3
pf4

Partial preview of the text

Download Structures-Computer Graphics-Lab Mannual and more Lecture notes Computer Graphics in PDF only on Docsity!

#include <stdio.h> #include <stdlib.h>

typedef struct tEdge { int yUpper; float xIntersect, dxPerScan; struct tEdge *next; } Edge;

typedef struct tdcPt { int x; int y; } dcPt;

#define WINDOW_HEIGHT 480

void insertEdge(Edge *list, Edge *edge) { Edge *p, *q = list;

p = q->next; while (p != NULL ) { if ( edge->xIntersect < p->xIntersect ) p = NULL; else { q = p; p = p->next; } }

edge->next = q->next; q->next = edge; }

int yNext( int k, int cnt, dcPt *pts) { int j;

if ( (k+1) > (cnt-1) ) j = 0; else j = k+1;

while( pts[k].y == pts[j].y ) { if ( (j+1) > (cnt-1) ) j = 0; else j++; }

return(pts[j].y); }

void makeEdgeRec( dcPt lower, dcPt upper, int yComp, Edge *edge, Edge *edges[]) {

edge->dxPerScan = (float)(upper.x - lower.x)/(upper.y - lower.y); edge->xIntersect = lower.x; if ( upper.y < yComp ) edge->yUpper = upper.y-1; else edge->yUpper = upper.y; insertEdge(edges[lower.y],edge);

}

void buildEdgeList( int cnt, dcPt *pts, Edge *edges[]) { Edge *edge; dcPt v1, v2; int i, yPrev = pts[cnt-2].y;

v1.x = pts[cnt-1].x; v1.y = pts[cnt-1].y;

for ( i = 0; i < cnt; ++i) { v2 = pts[i]; if ( v1.y != v2.y ) { edge = (Edge *)calloc(1,sizeof(Edge)); if ( v1.y < v2.y ) makeEdgeRec(v1,v2,yNext(i,cnt,pts),edge,edges); else makeEdgeRec(v2,v1,yPrev,edge,edges); } yPrev = v1.y; v1 = v2; }

}

void buildActiveList(int scan, Edge *active, Edge *edges[]) { Edge *p, *q;

p = edges[scan]->next; while (p) { q = p->next; insertEdge(active,p); p = q; }

}

void fillScan(int scan, Edge *active) { Edge *p1, *p2; int i;

p1 = active->next; while (p1)

Edge *edges[WINDOW_HEIGHT]; Edge *active; int i, scan; active = (Edge *) malloc (sizeof (Edge)); for (i = 0; i < WINDOW_HEIGHT; i++) { edges[i] = (Edge *) malloc (sizeof (Edge)); edges[i]->next = NULL; } active->next = NULL; buildEdgeList (cnt, pts, edges); for (scan = 0; scan < WINDOW_HEIGHT; scan++) { buildActiveList (scan, active, edges); if (active->next) { fillScan (scan, active); updateActiveList (scan, active); resortActiveList (active); } } }

int main(int argc, char *argv[]) { int cnt; dcPt *PolyVertices;

cnt = 6; PolyVertices = (dcPt *)calloc(cnt, sizeof(dcPt));

PolyVertices[0].x = 2; PolyVertices[0].y = 2; PolyVertices[1].x = 3; PolyVertices[1].y = 8; PolyVertices[2].x = 11; PolyVertices[2].y = 10; PolyVertices[3].x = 11; PolyVertices[3].y = 2; PolyVertices[4].x = 9; PolyVertices[4].y = 5; PolyVertices[5].x = 5; PolyVertices[5].y = 5;

printf("Polygon vertices in device coordinates:\n (%d,%d)", PolyVertices[0].x, PolyVertices[0].y); for ( int i = 1 ; i < cnt ; i++ ) printf(", (%d,%d)", PolyVertices[i].x, PolyVertices[i].y); printf("\n\n");

printf("Scan-line pixels in pixel coordinates:\n");

scanFill(cnt, PolyVertices);

return(0); }