Skip to main content

Scan Line Algorithm

Include:
stdio.h
conio.h
graphics.h



// Store Edges

struct edge
{
int x1;
int y1;
int x2;
int y2;
int flag;
};


void main()
{
int gd = DETECT, gm, n, i, j, k;

struct edge ed[10],temped;

float dx,dy,m[10],x_int[10],inter_x[10];

int x[10],y[10],ymax = 0, ymin = 480, yy,temp;
initgraph (&gd,&gm,"bgi");


printf("\n Enter Number Of Vertices : ");
scanf("%d", &n);

printf("\n Enter Vertices: \n");

for(i = 0; i < n; i++)
{
printf(" x[%d] : ", i);
scanf("%d", &x[i]);
printf(" y[%d] : ", i);
scanf("%d", &y[i]);

if(y[i] > ymax)
ymax = y[i];

if(y[i] < ymin)
ymin = y[i];

ed[i].x1 = x[i];
ed[i].y1 = y[i];
}



// Store the edge info.

for(i=0;i {
ed[i].x2 = ed[i+1].x1;
ed[i].y2 = ed[i+1].y1;
ed[i].flag=0;
}
ed[i].x2 = ed[0].x1;
ed[i].y2 = ed[0].y1;
ed[i].flag=0;

// Check for y1>y2, if not interchange y1 and y2 with
// corresponding x1 and x2

for(i=0;i{
if(ed[i].y1 < ed[i].y2)
{
temp = ed[i].x1; // x swapping
ed[i].x1=ed[i].x2;
ed[i].x2=temp;

temp = ed[i].y1; // y swapping
ed[i].y1=ed[i].y2;
ed[i].y2=temp;
}
}


// Draw the polygon

for(i=0;i line(ed[i].x1,ed[i].y1,ed[i].x2,ed[i].y2);


// Sorting of edges in the order of y1,y2,x1

for(i=0;i {
for(j=0;j {
if(ed[j].y1 {
temped = ed[j];
ed[j]=ed[j+1];
ed[j+1] = temped;
}

if(ed[j].y1==ed[j+1].y1)
{
if(ed[j].y2 {
temped = ed[j];
ed[j]=ed[j+1];
ed[j+1] = temped;
}

if(ed[j].y2==ed[j+1].y2)
{
if(ed[j].x1 {
temped = ed[j];
ed[j]=ed[j+1];
ed[j+1] = temped;
}
}
}

}
}


// Calculating 1/slope of each edge and storing top x
// coordinate of the edge

for(i=0;i {
dx = ed[i].x2 - ed[i].x1;
dy = ed[i].y2 - ed[i].y1;
if(dy==0)
m[i]=0;
else
m[i] = dx/dy;

inter_x[i]= ed[i].x1;
}

yy=ymax;
while(yy>ymin)
{

// Marking active egdes

for(i=0;i {
if(yy > ed[i].y2 && yy <= ed[i].y1 && ed[i].y1!=ed[i].y2)
ed[i].flag = 1;

else
ed[i].flag = 0;

}

// Finding the x intersections

j=0;
for(i=0;i {
if(ed[i].flag==1)
{
if(yy==ed[i].y1)
{
x_int[j]=ed[i].x1;
j++;
if(ed[i-1].y1==yy && ed[i-1].y1 {
x_int[j]=ed[i].x1;
j++;
}
if(ed[i+1].y1==yy && ed[i+1].y1 {
x_int[j]=ed[i].x1;
j++;
}
}
else
{
x_int[j] = inter_x[i]+(-m[i]);
inter_x[i]=x_int[j];
j++;
}
}
}

// Sorting the x intersections

for(i=0;i {
for(k=0;k {
if(x_int[k]>x_int[k+1])
{
temp =x_int[k];
x_int[k] = x_int[k+1];
x_int[k+1] = temp;
}
}
}

// Extracting pairs of x values to draw lines

for(i=0;i {
line(x_int[i],yy,x_int[i+1],yy);
}
yy--;
delay(50);
}
getch();
}

Comments

Popular posts from this blog

4. Lex and Yacc Program to detect errors in a 'C' Language Program

Lex and Yacc Program to detect errors in a 'C' Language Program   Lex Code : %{ #include"y.tab.h" #include<stdio.h> int LineNo = 1 ; %} identifier [ a - zA - Z ][ _a - zA - Z0 - 9 ]* number [ 0 - 9 ]+|([ 0 - 9 ]*\.[ 0 - 9 ]+) %% main \(\) return MAIN ; if return IF ; else return ELSE ; while return WHILE ; int | char | flaot return TYPE ; { identifier } return VAR ; { number } return NUM ; \> | \< | \<= | \>= | == return RELOP ; [\ t ] ; [\ n ] LineNo ++; . return yytext [ 0 ]; %% Yacc Code : %{ #include<string.h> #include<stdio.h> extern int LineNo ; int errno = 0 ; %} % token NUM VAR RELOP % token MAIN IF ELSE WHILE TYPE % left '-' '+' % left '*' '/' %% PROGRAM : MAIN BLOCK ; BLOCK : '{' CODE '}' ; CODE : BLOCK | STATEMENT CODE | STATEMENT ; STATEMENT : DECST ';' | DECST { printf ( "\nLine number %d...

Selenium + Python + UnexpectedAlertPresentException: Dealing with annoying alerts

Handling  UnexpectedAlertPresentException   Alerts who hates them? I Do!  Who doesn't hate an annoying alert causing your tests / scraping job to fail? I must say they are pretty much on point on the Unexpected part!  Fortunately, there are easy ways to mitigate the issue. 1. Disable alerts completely: driver . execute_script( 'window.alert = function(){};' ); execute this script just before where you anticipate the alert and you're golden. 2. You want to see the alert text but not disturb the execution flow. driver . execute_script( 'window.alert = console.info;' ); Now the alerts have been redirected to the console and you don't have to worry about them. (Unless you have to - then you'd have to monitor the console) 3. You know exactly when it comes and want to accept the alert and move on. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 from selenium import webdriver from selenium.webdriver.s...

2. Lex program that detects statement type i.e. Simple or Compound

Lex program that detects statement type i.e. Simple or Compound Note: Only AND | OR | BUT conjunctions are supported. Program: % option noyywrap %{ char test = 's' ; %} %% ( "" [ aA ][ nN ][ dD ] "" )|( "" [ oO ][ rR ] "" )|( "" [ bB ][ uU ][ tT ] "" ) { test = 'c' ;} . {;} \ n return 0 ; %% main () { yylex (); if ( test == 's' ) printf ( "\n Its a simple sentence" ); else if ( test == 'c' ) printf ( "\n This is compound sentence" ); }