computer graphics

21
ACKNOWLEDGEMENT We would like to acknowledge my sincere gratitude to Er. Ganga Subha for providing the chance of doing project on the Computer Graphics. We must express our gratitude to Er. Anand Shah for his guidelines for Computer Graphics as demanded by syllabus. Lastly but not least we would like to thanks our friends from BEX-070 for their continuous suggestions and motivation. 1

Upload: cvaraz

Post on 14-Apr-2017

103 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Computer Graphics

ACKNOWLEDGEMENT

We would like to acknowledge my sincere gratitude to Er. Ganga Subha for providing the chance of doing project on the Computer Graphics.

We must express our gratitude to Er. Anand Shah for his guidelines for Computer Graphics as demanded by syllabus. Lastly but not least we would like to thanks our friends from BEX-070 for their continuous suggestions and motivation.

1

Page 2: Computer Graphics

INTRODUCTION

The project mainly deals with the illustration of the composite transformation of the object. The composite transformation here described includes the illustration of scaling about the origin, scaling about the arbitrary points and the reflection about the line y= mx+c . The object we draw here is the house like frame which easily illustrates the concept of these three composite transformation.

The practical use of these composite transformation is in the image processing , animation and the video processing. The detail explanation of these transformation is given below:

1) Scaling about the origin:In this transformation, the scaling factor of x and y are multiplied with the x and y coordinate of the vertices of the object.Let sx and sy be the scaling factor of x and y coordinates then the scaling about origin is given as

x=x*sx

y=y*sy

2) Scaling about the arbitrary point:In this case , a point(x,y) in the object is selected as the arbitrary point followed by translation by translation factor (-x,-y) to shift that arbitrary point to the origin and is scaled according to the given scaling factor and is again translated with (x,y) translation factor. The steps followed here are given below:

Selection of some arbitrary point( xc, yc) Translation of the arbitrary points to the origin as

x=x-xc

2

Page 3: Computer Graphics

y=y-yc

Scaling with the scaling factor sx and sy as x=x*sx

Y=y*sy

Translation of the object to the arbitrary points by adding arbitrary point coordinate to the respective coordinateX=x+xc

Y=y+yc

3) Reflection about the line y=mx+cThe object is to be reflected about the line . The slope and the y-intercept of the object are given. The main task is to translate the y-intercept to the origin and then the object is rotated with the angle made by the line with the origin . Again the object is reflected about x axis and then it is rerotated by the same angle made by the line with the origin. Finally our result is obtained by translating the line passing through the origin to the given y intercept of the line.

Firstly the y-intercept is translated to the origin x=x;

y=y-c; Then the object is rotated through angle made with the origin

R(-a) Then the object is reflected about the x-axis

X=x; Y=-y;

Now rerotate the objects as R(a) Again the object is translated as:

X=x;

In this way we can process composite transformation of the objects.

3

Page 4: Computer Graphics

Source code:

#include <stdio.h>

#include <graphics.h>

#include <stdlib.h>

#include <math.h>

#include <conio.h>

void reset (int h[][2])

{

int val[9][2] = {

{ 50, 50 },{ 75, 50 },{ 75, 75 },{ 100, 75 },

{ 100, 50 },{ 125, 50 },{ 125, 100 },{ 87, 125 },{ 50, 100 }

};

int i;

for (i=0; i<9; i++)

{

h[i][0] = val[i][0]-50;

h[i][1] = val[i][1]-50;//(0,0),(25,0),(25,25),(50,25),(50,0),(75,0),(75,50),(37,75),(0,50)

}

}

void draw (int h[][2])

4

Page 5: Computer Graphics

{

int i;

setlinestyle (DOTTED_LINE, 0, 1);//axis creation with dot line

line (320, 0, 320, 480);

line (0, 240, 640, 240);

setlinestyle (SOLID_LINE, 0, 1);//for creating the object

for (i=0; i<8; i++)

line (320+h[i][0], 240-h[i][1], 320+h[i+1][0], 240-h[i+1][1]);

line (320+h[0][0], 240-h[0][1], 320+h[8][0], 240-h[8][1]);

}

void rotate (int h[][2], float angle)

{

int i;

for (i=0; i<9; i++)

{

int xnew, ynew;

xnew = h[i][0] * cos (angle) - h[i][1] * sin (angle);

ynew = h[i][0] * sin (angle) + h[i][1] * cos (angle);

h[i][0] = xnew; h[i][1] = ynew;

}

}

void scale (int h[][2], int sx, int sy)

5

Page 6: Computer Graphics

{

int i;

for (i=0; i<9; i++)

{

h[i][0] *= sx;

h[i][1] *= sy;

}

}

void translate (int h[][2], int dx, int dy)

{

int i;

for (i=0; i<9; i++)

{

h[i][0] += dx;

h[i][1] += dy;

}

}

void reflect (int h[][2], int m, int c)

{

int i;

float angle;

for (i=0; i<9; i++)

6

Page 7: Computer Graphics

h[i][1] -= c;

angle = M_PI/2 - atan (m);

rotate (h, angle);

for (i=0; i<9; i++)

h[i][0] = -h[i][0];

angle = -angle;

rotate (h, angle);

for (i=0; i<9; i++)

h[i][1] += c;

}

void ini()

{

int gd=DETECT,gm;

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

}

void dini()

{

getch();

closegraph();

}

void main()

7

Page 8: Computer Graphics

{

int h[9][2],sx,sy,x,y,m,c,choice;

do

{

clrscr();

printf("1. Scaling about the origin.\n");

printf("2. Scaling about an arbitrary point.\n");

printf("3. Reflection about the line y = mx + c.\n");

printf("4. Exit\n");

printf("Enter the choice: ");

scanf("%d",&choice);

switch(choice)

{

case 1: printf ("Enter the x- and y-scaling factors: ");

scanf ("%d%d", &sx, &sy);

ini();

reset (h);

draw (h);getch();

scale (h, sx, sy);

cleardevice();

draw (h);

8

Page 9: Computer Graphics

dini();

break;

case 2: printf ("Enter the x- and y-scaling factors: ");

scanf ("%d%d", &sx, &sy);

printf ("Enter the x- and y-coordinates of the point: ");

scanf ("%d%d", &x, &y);

ini();

reset (h);

translate (h, x, y) ;// Go to arbitrary point

draw(h); getch();//Show its arbitrary position

cleardevice(); /*cleardevice function clears the screen in graphics mode and sets the current position to (0,0). Clearing the screen consists of filling the screen with current background color.*

translate(h,-x,-y);//Take it back to origin

draw(h);

getch();

cleardevice();

scale (h, sx, sy);//Now Scale it

draw(h);

getch();

translate (h, x, y);//Back to Arbitrary point

9

Page 10: Computer Graphics

cleardevice();

draw (h);

putpixel (320+x, 240-y, WHITE);

dini();

break;

case 3: printf ("Enter the values of m and c: ");

scanf ("%d%d", &m, &c);

ini();

reset (h);

draw (h); getch();

reflect (h, m, c);

cleardevice();

draw (h);

dini();

break;

case 4: exit(0);

}

}while(choice!=4);

}

10

Page 11: Computer Graphics

OUTPUT:

The output screen appears as:

After that for options 1, the output appears as

Then the output of the objects will be as:

11

Page 12: Computer Graphics

Then the object is scaled as:

For option 2:

12

Page 13: Computer Graphics

Then the output of the object will be followed by the steps:

Then the object is translated to the origin as

Then the object is scaled as:

13

Page 14: Computer Graphics

Now finally is translated as:

14

Page 15: Computer Graphics

For option 3 we have output as:

15

Page 16: Computer Graphics

16

Page 17: Computer Graphics

CONCLUSION:

From the above description we reach to the conclusion that the composite transformation is very important for the image processing, animations and video making . The composite transformation includes the concepts of basic transformations(i.e. translation, rotation, scaling) and others transformations like shearing and reflections.

Hence the project is fruitful for enhancing our knowledge on basic as well as composite transformation.

17

Page 18: Computer Graphics

REFERENCES:

1) Computer Graphics C version,Donald D. Hearn&M.Pauline Baker, 2nd

Edition, PEARSON 2) Hands out , Anand Shah

18