Wednesday, 9 November 2011

Boundary fill flood fill Algorithm in Computer Graphics

Flood Fill
Flood fill colors an entire area in an enclosed figure through interconnected pixels using a single color. It is an easy way to fill color in the graphics. One just takes the shape and starts flood fill. The algorithm works in a manner so as to give all the pixels inside the boundary the same color leaving the boundary and the pixels outside. Flood Fill is also sometimes referred to as Seed Fill as you plant a seed and more and more seeds are planted by the algorithm. Each seed takes the responsibility of giving the same color to the pixel at which it is positioned. There are many variations of Flood Fill algorithm that are used depending upon requirements.


#include
#include
#include
#include

void fill_right(x,y)
int x , y ;
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);
fill_right(++x,y);
x = x - 1 ;
fill_right(x,y-1);
fill_right(x,y+1);

}
}

void fill_left(x,y)
int x , y ;
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);

fill_left(--x,y);
x = x + 1 ;
fill_left(x,y-1);
fill_left(x,y+1);

}
}


void main()
{
int x , y ,a[10][10];
int gd, gm ,n,i;

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");

printf("\n\n\tEnter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");

for(i=0;i
#include
#include
#include



void fill_right(x,y)
int x , y ;
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_right(++x,y);
x = x - 1 ;
fill_right(x,y-1);
fill_right(x,y+1);
}
delay(1);
}


void fill_left(x,y)
int x , y ;
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);

fill_left(--x,y);
x = x + 1 ;
fill_left(x,y-1);
fill_left(x,y+1);
}
delay(1);
}


void main()
{
int x,y,n,i;
int gd=DETECT,gm;
clrscr();

initgraph(&gd,&gm,"c:\\tc\\bgi");



/*- draw object -*/

line (50,50,200,50);
line (200,50,200,300);
line (200,300,50,300);
line (50,300,50,50);

/*- set seed point -*/
x = 100; y = 100;

fill_right(x,y);
fill_left(x-1,y);

getch();
}
Buy me a Cup of Coffee








MONIKA YADAV (MCA),
Software Engineer,
www.NotesGuru.in, Indore

ROHIT KESHRIYA (MCA),
Software Engineer,
www.NotesGuru.in, Indore

For guest faculty Contact us on following E-mail ID:

monikay.aerosoft@gmail.com
monikay.aerosoft@rediffmail.com
monikay.aerosoft@yahoo.com
monikay.aerosoft@hotmail.com
rohit.aerosoft@gmail.com
rohit.aerosoft@rediffmail.com
rohit.aerosoft@yahoo.com
rohit.aerosoft@hotmail.com

Note: We have been used search engines for gathering content.



















Your Ad Here







free counters



Area Filling Algorithm in Computer Graphics



#include
#include
#include
#include

struct Node
{
int x;
int y;
struct Node* next;
};

void fill (int pt[][2], int clr);
void floodfill4 (int x, int y, int oldclr, int newclr);
void insert (int x, int y, struct Node** last);

void main()
{
int i, j;
int pt[3][2];
int clr;

printf ("This program demonstrates filling a polygon.\n");
printf ("Enter the x- and y-coordinates for three points:\n");
for (i=0; i<3; i++) for (j=0; j<2; j++) scanf ("%d", &pt[i][j]); printf ("Enter the fill-colour: (Any number from 1 to 14) "); scanf ("%d", &clr); fill (pt, clr); } void fill (int pt[][2], int clr) { int gd = DETECT, gm; int seedx, seedy; initgraph (&gd, &gm, "..\\bgi"); setcolor (WHITE); line (pt[0][0], pt[0][1], pt[1][0], pt[1][1]); line (pt[1][0], pt[1][1], pt[2][0], pt[2][1]); line (pt[2][0], pt[2][1], pt[0][0], pt[0][1]); getch(); seedx = (pt[0][0] + pt[1][0] + pt[2][0]) / 3; seedy = (pt[0][1] + pt[1][1] + pt[2][1]) / 3; floodfill4 (seedx, seedy, BLACK, clr); getch(); closegraph(); return; } void floodfill4 (int x, int y, int oldclr, int newclr) { struct Node* first, *last, *tmp; first = (struct Node*) malloc (sizeof (struct Node)); if (first == NULL) { closegraph(); fprintf (stderr, "floodfill4: Out of memory.\n"); exit (2); } if (oldclr == newclr) { free (first); return; } first->x = x;
first->y = y;
first->next = NULL;
last = first;

while (first != NULL)
{
putpixel (x, y, newclr);

if (getpixel (x, y-1) == oldclr)
{
putpixel (x, y-1, newclr);
insert (x, y-1, &last);
}


if (getpixel (x, y+1) == oldclr)
{
putpixel (x, y+1, newclr);
insert (x, y+1, &last);
}

if (getpixel (x-1, y) == oldclr)
{
putpixel (x-1, y, newclr);
insert (x-1, y, &last);
}

if (getpixel (x+1, y) == oldclr)
{
putpixel (x+1, y, newclr);
insert (x+1, y, &last);
}

tmp = first;
first = first->next;
x = first->x;
y = first->y;
free (tmp);
}
}

void insert (int x, int y, struct Node** last)
{
struct Node* p;
p = (struct Node*) malloc (sizeof (struct Node));
if (p == NULL)
{
closegraph();
fprintf (stderr, "\n insert: Out of memory.\n");
exit (2);
}

p->x = x;
p->y = y;
p->next = NULL;
(*last)->next = p;
*last = (*last)->next;
}

Buy me a Cup of Coffee








MONIKA YADAV (MCA),
Software Engineer,
www.NotesGuru.in, Indore

ROHIT KESHRIYA (MCA),
Software Engineer,
www.NotesGuru.in, Indore

For guest faculty Contact us on following E-mail ID:

monikay.aerosoft@gmail.com
monikay.aerosoft@rediffmail.com
monikay.aerosoft@yahoo.com
monikay.aerosoft@hotmail.com
rohit.aerosoft@gmail.com
rohit.aerosoft@rediffmail.com
rohit.aerosoft@yahoo.com
rohit.aerosoft@hotmail.com

Note: We have been used search engines for gathering content.



















Your Ad Here







free counters

Antialiasing Technique

Antialiasing methods were developed to combat the effects of aliasing.
There are three main classes of antialiasing algorithms.


As aliasing problem is due to low resolution, one easy solution is to increase the resolution , causing sample points to occur more frequently. This increases the cost of image production.


The image is created at high resolution and then digitally filtered. This method is called supersampling or postfiltering and eliminates high frequencies which are the source of aliases.


The image can be calculated by considering the intensities over a particular region. This is called prefiltering.

Prefiltering.


Prefiltering methods treat a pixel as an area, and compute pixel color based on the overlap of the scene's objects with a pixel's area. These techniques compute the shades of gray based on how much of a pixel's area is covered by a object.
For example, a modification to Bresenham's algorithm was developed by Pitteway and Watkinson. In this algorithm, each pixel is given an intensity depending on the area of overlap of the pixel and the line. So, due to the blurring effect along the line edges, the effect of antialiasing is not very prominent, although it still exists.
Prefiltering thus amounts to sampling the shape of the object very densely within a pixel region. For shapes other than polygons, this can be very computationally intensive.

Supersampling or postfiltering is the process by which aliasing effects in graphics are reduced by increasing the frequency of the sampling grid and then averaging the results down. This process means calculating a virtual image at a higher spatial resolution than the frame store resolution and then averaging down to the final resolution. It is called postfiltering as the filtering is carried out after sampling.

The drawback is that there is a technical and economic limit for increasing the resolution of the virtual image.


Since the frequency of images can extend to infinity, it just reduces aliasing by raising the Nyquist limit - shift the effect of the frequency spectrum.
Supersampling is basically a three stage process.

A continuous image I(x,y) is sampled at n times the final resolution. The image is calculated at n times the frame resolution. This is a virtual image.

The virtual image is then lowpass filtered

The filtered image is then resampled at the final frame resolution.
Buy me a Cup of Coffee








MONIKA YADAV (MCA),
Software Engineer,
www.NotesGuru.in, Indore

ROHIT KESHRIYA (MCA),
Software Engineer,
www.NotesGuru.in, Indore

For guest faculty Contact us on following E-mail ID:

monikay.aerosoft@gmail.com
monikay.aerosoft@rediffmail.com
monikay.aerosoft@yahoo.com
monikay.aerosoft@hotmail.com
rohit.aerosoft@gmail.com
rohit.aerosoft@rediffmail.com
rohit.aerosoft@yahoo.com
rohit.aerosoft@hotmail.com

Note: We have been used search engines for gathering content.

Two Dimensional Transformation

Transformation:

A graphics system should allow the programmer to define picturesthat include a variety of transformation. For example, he should be able tomagnify a picture so that detail appears more clearly, or reduce it so thatmore of the picture is visible. How should also be able to applytransformation to symbols. It is also useful to be able to change the scale of a symbol and to rotate it through some angle.Two aspects of the formulation of transformation should beemphasized ± (1)

A transformation is single mathematical entity and as such canbe denoted by single name or symbol.(2)

Two transformations can be combined or concatenated to yield asingle transformation with the same effect as the sequentialapplication of the original two. Thus, transformation A might betranslation and transformation B a scaling. The concatenationproperty allows us to determine a transformation C=AB whoseeffect is to translate and then scale.
G
eometric Transformation:

In all computer graphics system, the fundamental ability is tosimulate the manipulation of objects in space, which is referred to astransformation.In geometric transformation, the object is transformed relative to astationary coordinate system or background i.e. each and every point of itis displaced equally.There are basically four types of geometric transformation:

1. Translation
2. Scaling
3. Rotation
4. Reflection


Buy me a Cup of Coffee








MONIKA YADAV (MCA),
Software Engineer,
www.NotesGuru.in, Indore

ROHIT KESHRIYA (MCA),
Software Engineer,
www.NotesGuru.in, Indore

For guest faculty Contact us on following E-mail ID:

monikay.aerosoft@gmail.com
monikay.aerosoft@rediffmail.com
monikay.aerosoft@yahoo.com
monikay.aerosoft@hotmail.com
rohit.aerosoft@gmail.com
rohit.aerosoft@rediffmail.com
rohit.aerosoft@yahoo.com
rohit.aerosoft@hotmail.com

Note: We have been used search engines for gathering content.

Translation in Computer Graphics

ALGORITHM 3 Dimensional Transformation Source Code
1. Enter the choice for transformation.
2. Perform the translation, rotation, scaling of 3D object.
3. Get the needed parameters for the transformation from the user.
4. Increase of rotation, object can be rotated about x or y or z axis.
5. Display the transmitted object in the screen
Source Code Programming Algorithm
#include
#include
#include
#include
int maxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
{
int gd,gm,x,y,z,o,x1,x2,y1,y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"d:\\tc\\bgi");
setfillstyle(0,getmaxcolor());
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
axis();
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
printf("\Enter the translation factor");
scanf("%d%d",&x,&y);
axis();
printf("After translation");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+x+100,midy-(y+150),midx+x+60,midy-(y+100),10,1);
axis();
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
printf("Enter the scaling factor");
scanf("%d%d%d",&x,&y,&z);
axis();
printf("After scaling");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+(x*100),midy-(y*150),midx+(x*60),midy-(y*100),10*z,1);
axis();
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
printf("Enter the rotation angle");
scanf("%d",&o);
x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
y1=50*sin(o*3.14/180)+100*cos(o*3.14/180);
x2=60*cos(o*3.14/180)-90*sin(o*3.14/180);
y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);
axis();
printf("After rotating about Z-axis");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,10,1);
axis();
printf("After rotating about x-axis");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+100,midy-x1,midx+60,midy-x2,10,1);
axis();
printf("After rotating about Y-axis");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+x1,midy-150,midx+x2,midy-100,10,1);
getch();
closegraph();
}


Buy me a Cup of Coffee








MONIKA YADAV (MCA),
Software Engineer,
www.NotesGuru.in, Indore

ROHIT KESHRIYA (MCA),
Software Engineer,
www.NotesGuru.in, Indore

For guest faculty Contact us on following E-mail ID:

monikay.aerosoft@gmail.com
monikay.aerosoft@rediffmail.com
monikay.aerosoft@yahoo.com
monikay.aerosoft@hotmail.com
rohit.aerosoft@gmail.com
rohit.aerosoft@rediffmail.com
rohit.aerosoft@yahoo.com
rohit.aerosoft@hotmail.com

Note: We have been used search engines for gathering content.



















Your Ad Here







free counters

Scaling in Computer Graphics

#include
#include
#include
#include
#include

int x1,y1,x2,y2,x3,y3,mx,my;
void draw();
void scale();
void main()
{
int gd=DETECT,gm;
int c;
initgraph(&gd,&gm," ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
draw();
scale();
}

void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void scale()
{
int x,y,a1,a2,a3,b1,b2,b3;
int mx,my;
printf("Enter the scalling coordinates");
scanf("%d%d",&x,&y);
mx=(x1+x2+x3)/3;
my=(y1+y2+y3)/3;
cleardevice();
a1=mx+(x1-mx)*x;
b1=my+(y1-my)*y;
a2=mx+(x2-mx)*x;
b2=my+(y2-my)*y;
a3=mx+(x3-mx)*x;
b3=my+(y3-my)*y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
draw();
getch();
}


Buy me a Cup of Coffee








MONIKA YADAV (MCA),
Software Engineer,
www.NotesGuru.in, Indore

ROHIT KESHRIYA (MCA),
Software Engineer,
www.NotesGuru.in, Indore

For guest faculty Contact us on following E-mail ID:

monikay.aerosoft@gmail.com
monikay.aerosoft@rediffmail.com
monikay.aerosoft@yahoo.com
monikay.aerosoft@hotmail.com
rohit.aerosoft@gmail.com
rohit.aerosoft@rediffmail.com
rohit.aerosoft@yahoo.com
rohit.aerosoft@hotmail.com

Note: We have been used search engines for gathering content.



















Your Ad Here







free counters

Rotation in Computer Graphics


PROGRAM FOR ROTATION IN COMPUTER GRAPHICS

ROTATION PROGRAM
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void main()
{
int x1=100,x2=100,y1=100,y2=300,x12,y12,x22,y22;
float a=1.57;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(5);
line(x1,y1,x2,y2);
while(!kbhit())
{
for(float i=0;i<628;i+=0.1)
{
setcolor(5);
x12=x1*cos(i)-y1*sin(i);
x22=x2*cos(i)-y2*sin(i);
y12=x1*sin(i)+y1*cos(i);
y22=x2*sin(i)+y2*cos(i);
line(x12,y12,x22,y22);
delay(200);
}}
getch();
}


Buy me a Cup of Coffee








MONIKA YADAV (MCA),
Software Engineer,
www.NotesGuru.in, Indore

ROHIT KESHRIYA (MCA),
Software Engineer,
www.NotesGuru.in, Indore

For guest faculty Contact us on following E-mail ID:

monikay.aerosoft@gmail.com
monikay.aerosoft@rediffmail.com
monikay.aerosoft@yahoo.com
monikay.aerosoft@hotmail.com
rohit.aerosoft@gmail.com
rohit.aerosoft@rediffmail.com
rohit.aerosoft@yahoo.com
rohit.aerosoft@hotmail.com

Note: We have been used search engines for gathering content.



















Your Ad Here







free counters