73
CS2405 Computer Graphics Lab - Manual Bresenham’s Line Drawing EX NO.1(A) Aim: To implement Bresenham’s line drawing Algorithm for drawing lines. Functions used: Line() The function line() is used to draw a line from(x1,y1)to (x2,y2) Syntax: line (x1,y1,x2,y2) initgraph(). This function takes thee arguments and they are i).the video driver to be used (gd). ii).the graphics mode (gm). iii).the path name. Syntax: Initgraph(gd,gm,path) Algorithm: Step 1: Start Step 2: Get the values of the end points as(x1, y1) &(x2, y2) Step 3: Assign x=x1, y=y1; Step 4: Compute dx=x2-x1 Step 5: Compute dy=y2-y1 Step 6: Assign sx=x2-x1, sy=y2-y1

CS2405 Computer Graphics Lab manual.docx

Embed Size (px)

Citation preview

CS2405 Computer Graphics Lab - Manual Bresenhams Line DrawingEX NO.1(A)Aim: To implement Bresenhams line drawing Algorithm for drawing lines.

Functions used:

Line()

The function line() is used to draw a line from(x1,y1)to (x2,y2)

Syntax:

line (x1,y1,x2,y2)

initgraph().

This function takes thee arguments and they are i).the video driver to be used (gd). ii).the graphics mode (gm). iii).the path name.

Syntax:

Initgraph(gd,gm,path)

Algorithm:

Step 1: StartStep 2: Get the values of the end points as(x1, y1) &(x2, y2)Step 3: Assign x=x1, y=y1;Step 4: Compute dx=x2-x1Step 5: Compute dy=y2-y1Step 6: Assign sx=x2-x1, sy=y2-y1Step 7: If dy>dx then interchange the values of dx and dy and assign exch=1Step 8: Compute p=2xdy-dxStep 9: Put a pixel on(x,y)Step 10: If exch=1, y=sy else x=x+sxStep 11: If p>0 and exch =1, x=x+sx else y=y+sy, p=p-2xdxStep 12: Compute p=p+2xdyStep 13: Do steps (9) t0 (12) for dx timesStep 14: Stop

Program:

#include#include#include#includeint signs(int m);void bres_line(int,int,int,int);void main(){int gd=DETECT,gm;int x1,x2,y1,y2;initgraph(&gd,&gm,"");cout>x1;cout>y1;cout>x2;cout>y2;bres_line(x1,y1,x2,y2);getch();closegraph();}void bres_line(int x1,int y1,int x2,int y2){int x,y,sx,sy,p,temp,exch=0,i,dx,dy;x=x1;y=y1;dy=abs(y1-y2);dx=abs(x1-x2);sx=signs(x2-x1);sy=signs(y2-y1);if(dy>dx){temp=dx;dx=dy;dy=temp;exch=1;}p=2*dy-dx;for(i=0;i=0){if(exch==1)x+=sx;elsey+=sy;p=p-2*dx;}p=p+2*dy;}}int signs(int m){if(m0)return(1);elsereturn(0);}

Input:

Enter starting value of X-Axis----->100Enter starting value of Y-Axis----->100Enter ending value of X-Axis----->200Enter ending value of Y-Axis----->200

Output:

Result:

Thus the program to draw line using Bresenhams line drawing Algorithm was executed successfully.

Circle DrawingEX NO 1(B)

Aim: To write a program to draw a circle using Bresenhams circle drawing Algorithm.

Functions used:

Circle()

The function circle() is used to draw a circle using(x,y) as centre point.

Syntax:

circle (x,y,radius)

initgraph().

This function takes thee arguments and they are i).the video driver to be used (gd). ii).the graphics mode (gm). iii).the path name.

Syntax:

Initgraph(gd,gm,path)

Putpixel ()

The function putpixel() is used to place a pixel at particular coordinate

Syntax:

Putpixel(x,y,color)

Algorithm:

Step 1: StartStep 2: Get the center point as (xc,yc),get the radius as r.Step 3: Assign y=r,x=0Step 4: Calculate p=3-2rStep 5: If pyc;cout>r;my_circle(xc,yc,r);getch();closegraph();}void my_circle(int xc,int yc,int r){int x,y,p;p=3-2*r;x=0;y=r;while(x:200Enter radius----->: 40

Output:

Result: Thus the program to draw circle using Bresenhams circle drawing Algorithm was executed successfully.

Ellipse DrawingEX NO 1(C)Aim: To write a program to draw a ellipse using Bresenhams ellipse drawing Algorithm.

Functions used:

initgraph().

This function takes thee arguments and they are i).the video driver to be used (gd). ii).the graphics mode (gm). iii).the path name.

Syntax:

Initgraph(gd,gm,path)

Putpixel ()

The function putpixel() is used to place a pixel at particular coordinate

Syntax:

Putpixel(x,y,color)

Algorithm:Step 1: StartStep 2: Get the center point as(x1, y1)Step 3: Get the length of semi-major, semi-minor axes as r1 & r2Step 4: Calculate t=pi/180Step 5: Initialise i=0;Step 6: Compute d=i*tStep 7: Compute x=x1+y1*sin(d), y=y1+r2*cos(d).Step 8: Put a pixel on(x,y)Step 9: Increment I by 1Step 10: Repeat steps(6) to (9) until i>y1;coutr1;coutr2;for(i=0;i