Sie sind auf Seite 1von 30

Computer Graphics

Prof. M. T. Savaliya Government Engineering College., Patan

Presentation Outline
What

is Computer Graphics? Graphics Applications Graphics Terminology How to create graphics? Algorithms : Drawing and Filling Transformations Clipping Graphics Library in C

Center of the Computer Graphics is Picture Picture may be

Engineering

Drawing Business Graph Rendering of proposed Design Advertisement Medical Image Animated Movie

Graphics covers : creating, presenting, editing and Interacting with pictures

Saying A small Picture can express information which may take paragraphs Example

Knowing

the admission trends for the courses after 12th Science Appro. 20000 students admitted

304 Mayank C. Patel AEC

May

be grouped by branch selection Trend is known only after compiling records Best is to prepare a bar Chart, each Bar Shows the branch with no. of students admitted in branch.

Another Example
a
b

a2

ab

ab

b2

(a + b)2 = a2+2ab+b2

Graphics

Applications
Aided Design

Computer

Presentation

Graphics Computer Art Entertainment Education and Training Visulization Image Processing Graphical User Interface

Computer uses Raster Displays to display the graphics Screen is divided into small dots known as pixels (Picture Elements). Pixel is smallest addressable unit in graphics and has its coordinate (x,y)

Pixel (0,0)

Pixel (0,1)

Pixel (1,0)

No. of Pixels in a Screen is called Resolution Higher the resolution, better the picture quality is IBM Display standards & Resolutions

CGA

EGA
VGA

640 x 200 640 x 350 640 x 480

SVGA Resolutions
800 x 600, 1024 x 768

Picture is created by making pixels ON or OFF for monocrome display or assigning color values to pixel in color display For example,

Selecting Pixels to display line is called scan conversion process

Picture consists of various primitives like lines, circles, ellipses etc. For generating each of the primitive, an algorithm to select the process during scan conversion is required. Algorithm is defined using the mathematical definition of the primitive and programming techniques to make them faster Performance is critical issue and hence incremental approach is used.

For example, Line is defined as


y=mx+b Where m is slope and b is intercept

y2 y1 m = ---------x2 x1
b = y1 m x1

Simple way is to use DDA algorithm to select the pixels. For line |m| < 1, for each x from x1 to x2, y value is computed and rounded. New y can also be computed by adding slope to previous y value

Floating point operations are time consuming and rounding errors causes problem. Bresenham line algorithm overcomes this problem by using only integer calculations and incremental approach Rather than computing y value each time, it decides whether to keep in same row or to go to next row based on the distance between actual line and center of the pixel Only sign of the parameter decided the row

yk+1 yk

d2 d1

xk

Similar process is repeated for lines with slope |m| > 1 Similar algorithms are available for other primitives too Circle algorithm in addition to incremental approach, uses symmetry to improve the performance

(-x,y)

(x,y)

(-y,x)

(y,x)

(-y,-x)

(y,-x)

(-x,-y)

(x,-y)

For close primitives, filling algorithms are defined. For example,


Flood

fill Boundary fill yx scan line algorithm

Once the picture is created, following transformation operations can be performed


Scaling

x = Sx * x y = Sy * y Rotation
x = x cos y sin y = x sin y cos
Translation

x = x + tx y = y + ty

Many times it is required to enlarge a small part of picture covered in window to get the details Clipping algorithms are used to remove portion outside the window For line clipping, following algorithms are used

Cohen

sutherland Liang-Berskey Nichol-Lee-Nichol

Algorithms are also available for other clipping operations

To create graphics on computers, various graphics libraries are available Graphics library is a set of functions called API, which includes functions for primitives, colors, styles, filling patterns, animations etc. C provides such graphics library as header file <graphics.h> No. of such libraries are available

Initialization
#include <stdio.h> #include <graphics.h> void main() { int gdriver,gmode; /* detect the graphics driver and mode */ detectgraph(&gdriver,&gmode); /* initialize with detected driver and mode */ initgraph(&gdriver,&gmode,c:\\tc\\bgi); /* write your graphics program logic here */ . . /* close the graphics mode */ closegraph(); }

Initialization
#include <stdio.h> #include <graphics.h> void main() { int gdriver=DETECT,gmode; /* initialize with detected driver and mode */ initgraph(&gdriver,&gmode,c:\\tc\\bgi); /* write your graphics program logic here */ . . /* close the graphics mode */ closegraph(); }

Common Functions
cleardevice() putpixel(x,y,color) getmaxx() getmaxy() line(x1,y1,x2,y2) circle(x,y,rad) rectangle(x1,y1,x2,y2) ellipse(x,y,start,end,xrad,yrad) drawpoly(n,*point) arc(x,y,start,end,rad) outtextxy(x,y,string)

Example
#include <stdio.h> #include <graphics.h> void main() { int *gdriver=DETECT,*gmode; /* initialize with detected driver and mode */ initgraph(&gdriver,&gmode,c:\\tc\\bgi); /* draw circle */ circle(getmaxx()/2, getmaxy()/2, 100); /* close the graphics mode */ closegraph(); }

Colors, Styles and Patterns


setbkcolor(BLUE); setcolor(RED); setlinestyle(DOTTED,0,3); line(0,0,getmaxx(),getmaxy());

Fill patterns
#include<stdio.h> #include<conio.h> #include <graphics.h>

void main() { int gdriver=DETECT,gmode; int v[] = {270,210,370,210,370,270,270,270,270,210}; int xc,yc;


initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); xc = getmaxx()/2; yc = getmaxy()/2;

/* draw rectangle using drawpoly() */ drawpoly(5,v); /* draw ellipse inside rectangle */ ellipse(xc,yc,0,360,50,30); /* set the pattern and color and draw inner ellipse */ setfillstyle(SLASH_FILL,RED); fillellipse(xc,yc,10,30); /* close the graphics mode */ closegraph(); }

References

Computer Graphics
Hearn

and Baker, Pearson Education

Procedural Elements for Computer Graphics


Rogers,

TataMaGrawHill

Programming in C : ANSI Standard


M.

T. Savaliya, Atul Prakashan

Das könnte Ihnen auch gefallen