0% found this document useful (0 votes)
67 views14 pages

Abrar CG

The document contains code for generating lines, circles and 2D transformations like translation, rotation, scaling using various computer graphics algorithms like DDA, Bresenham, midpoint circle algorithm. It also contains code for Cohen-Sutherland line clipping algorithm and defines a window for clipping. The document is written by Abrar Shah and contains code snippets with input/output screenshots for visualizing the computer graphics concepts implemented.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views14 pages

Abrar CG

The document contains code for generating lines, circles and 2D transformations like translation, rotation, scaling using various computer graphics algorithms like DDA, Bresenham, midpoint circle algorithm. It also contains code for Cohen-Sutherland line clipping algorithm and defines a window for clipping. The document is written by Abrar Shah and contains code snippets with input/output screenshots for visualizing the computer graphics concepts implemented.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

ABRAR SHAH 19048112004

COMPUTER GRAPHICS
(CSE-7317L)

PRACTICALS
by

ABRAR SHAH

(19048112004)

B-Tech CSE 7th Semester

LECTURER
ER. WASEEM BAKHSHI

DEPARTMENT OF COMPUTER SCIENCE &


ENGINEERING

COMPUTER GRAPHICS CSE-7317L


ABRAR SHAH 19048112004

Line Generation using DDA Algorithm


#include <graphics.h>

#include <stdio.h>

#include <math.h>

#include <dos.h>

void main() {

float x, y, x1, y1, x2, y2, dx, dy, step;

int i, gd = DETECT, gm;

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

printf("Enter the starting point of line: ");

scanf("%f%f", &x1, &y1);

printf("Enter the ending point of line: ");

scanf("%f%f", &x2, &y2);

dx = abs(x2 - x1);

dy = abs(y2 - y1);

if (dx >= dy) {

step = dx;

} else {

step = dy;

dx = dx / step;

dy = dy / step;

x = x1;

y = y1;

i = 1;

while (i <= step) {

putpixel(x, y, 5);

x = x + dx;

y = y + dy;

i = i + 1;

delay(100);

COMPUTER GRAPHICS CSE-7317L


ABRAR SHAH 19048112004

getch();

closegraph();

Line Generation using Bresenham’s Algorithm


#include<stdio.h>

#include<graphics.h>

void drawline(int x0, int y0, int x1, int y1)

int dx, dy, p, x, y;

dx=x1-x0;

dy=y1-y0;

x=x0;

y=y0;

p=2*dy-dx;

while(x<x1)

if(p>=0)

putpixel(x,y,7);

y=y+1;

COMPUTER GRAPHICS CSE-7317L


ABRAR SHAH 19048112004

p=p+2*dy-2*dx;

else

putpixel(x,y,7);

p=p+2*dy;}

x=x+1;

int main()

int gdriver=DETECT, gmode, error, x0, y0, x1, y1;

initgraph(&gdriver, &gmode, "c:\turboc3\bgi");

printf("Enter co-ordinates of first point: ");

scanf("%d%d", &x0, &y0);

printf("Enter co-ordinates of second point: ");

scanf("%d%d", &x1, &y1);

drawline(x0, y0, x1, y1);

return 0;

getch();

closegraph();

Output :-

COMPUTER GRAPHICS CSE-7317L


ABRAR SHAH 19048112004

Circle Generation using Mid-Point Algorithm


#include<graphics.h>

#include<conio.h>

#include<stdio.h>

int main()

int x,y,x_mid,y_mid,radius,dp;

int g_mode,g_driver=DETECT;

initgraph(&g_driver,&g_mode,"");

printf("*********** MID POINT Circle drawing algorithm ********\n\n");

printf("\nenter the coordinates= ");

scanf("%d %d",&x_mid,&y_mid);

printf("\n now enter the radius =");

scanf("%d",&radius);

x=0;

y=radius;

dp=1-radius;

do

putpixel(x_mid+x,y_mid+y,YELLOW);

putpixel(x_mid+y,y_mid+x,RED);

putpixel(x_mid-y,y_mid+x,WHITE);

putpixel(x_mid-x,y_mid+y,BLUE);

putpixel(x_mid-x,y_mid-y,GREEN);

putpixel(x_mid-y,y_mid-x,BROWN);

putpixel(x_mid+y,y_mid-x,WHITE);

putpixel(x_mid+x,y_mid-y,RED);

if(dp<0) {

dp+=(2*x)+1;

else{

y=y-1;

dp+=(2*x)-(2*y)+1;

x=x+1;

}while(y>x);

getch();

COMPUTER GRAPHICS CSE-7317L


ABRAR SHAH 19048112004

OUTPUT :-

2D TRANSFORMATIONS :-
1) TRANSLATION :-
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
int main()
{
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2,tx,ty;
initgraph(&gd,&gm,"");
printf("\n Please enter first coordinate of the triangle= ");
scanf("%d %d", &x,&y);
printf("\n Enter second coordinate of the trinagle = ");
scanf("%d %d",&x1,&y1);
printf("\n Enter third coordinate of the triangle = ");
scanf("%d %d",&x2,&y2);
printf("\n\t\t********** TRIANGLE before & after translation ***********");
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
printf("\n Now enter the translation vector = ");
scanf("%d %d",&tx,&ty);

setcolor(RED);

COMPUTER GRAPHICS CSE-7317L


ABRAR SHAH 19048112004

line(x+tx,y+ty,x1+tx,y1+ty);
line(x1+tx,y1+ty,x2+tx,y2+ty);
line(x2+tx,y2+ty,x+tx,y+ty);
getch();
closegraph();
}

OUTPUT :-

2) ROTATION :-
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void TriAngle(int x1, int y1, int x2, int y2, int x3, int y3);
void Rotate(int x1, int y1, int x2, int y2, int x3, int y3, float angle);
int main() {
int gd = DETECT, gm;
int x1, y1, x2, y2, x3, y3;
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);
TriAngle(x1, y1, x2, y2, x3, y3);
float angle;

COMPUTER GRAPHICS CSE-7317L


ABRAR SHAH 19048112004

printf("Enter the angle for rotation (in degrees): ");


scanf("%f", &angle);
Rotate(x1, y1, x2, y2, x3, y3, angle);
char ch;
do {
ch = getch();
} while (ch != 'q' && ch != 'Q');
closegraph();
return 0;
}
void TriAngle(int x1, int y1, int x2, int y2, int x3, int y3) {
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
}
void Rotate(int x1, int y1, int x2, int y2, int x3, int y3, float angle) {
angle = angle * M_PI / 180.0;
int x1_new = x1 * cos(angle) - y1 * sin(angle);
int y1_new = x1 * sin(angle) + y1 * cos(angle);
int x2_new = x2 * cos(angle) - y2 * sin(angle);
int y2_new = x2 * sin(angle) + y2 * cos(angle);

int x3_new = x3 * cos(angle) - y3 * sin(angle);

int y3_new = x3 * sin(angle) + y3 * cos(angle);

TriAngle(x1_new, y1_new, x2_new, y2_new, x3_new, y3_new);

Output :-

3) SCALING:-

COMPUTER GRAPHICS CSE-7317L


ABRAR SHAH 19048112004

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

int main(){

int x,y,x1,y1,x2,y2;

int scl_fctr_x,scl_fctr_y;

int gd=DETECT,gm;

initgraph(&gd,&gm,"");

printf("\t\t\t********** Scaling ***********\n");

printf("\n\t\t\t Please enter first coordinate of Triangle = ");

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

printf("\n\t\t\t Please enter second coordinate of Triangle = ");

scanf("%d %d",&x1,&y1);

printf("\n\t\t\t Please enter third coordinate of Triangle = ");

scanf("%d %d",&x2,&y2);

line(x,y,x1,y1);

line(x1,y1,x2,y2);

line(x2,y2,x,y);

printf("\n\t\t\t Now Enter Scaling factor x and y = ");

scanf("%d %d",&scl_fctr_x,&scl_fctr_y);

x = x* scl_fctr_x;

x1 = x1* scl_fctr_x;

x2 = x2* scl_fctr_x;

y = y* scl_fctr_y;

y1 = y1* scl_fctr_y;

y2= y2 * scl_fctr_y ;

line(x,y,x1,y1);

line(x1,y1,x2,y2);

line(x2,y2,x,y);

getch();

closegraph();

COMPUTER GRAPHICS CSE-7317L


ABRAR SHAH 19048112004

OUTPUT :-

COHEN-SUTHERLAND 2D LINE CLIPPING AND


WINDOW :-
#include<graphics.h>

#include<conio.h>

#include<stdio.h>

#include<math.h>

int main()

int rcode_begin[4]={0,0,0,0},rcode_end[4]={0,0,0,0},region_code[4];

int W_xmax,W_ymax,W_xmin,W_ymin,flag=0;

float slope;

int x,y,x1,y1,i, xc,yc;

int gr=DETECT,gm;

initgraph(&gr,&gm,"");

printf("\n****** Cohen Sutherlsnd Line Clipping algorithm ***********");

printf("\n Now, enter XMin, YMin =");

scanf("%d %d",&W_xmin,&W_ymin);

printf("\n First enter XMax, YMax =");

scanf("%d %d",&W_xmax,&W_ymax);

printf("\n Please enter intial point x and y= ");

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

COMPUTER GRAPHICS CSE-7317L


ABRAR SHAH 19048112004

printf("\n Now, enter final point x1 and y1= ");

scanf("%d %d",&x1,&y1);

cleardevice();

rectangle(W_xmin,W_ymin,W_xmax,W_ymax);

line(x,y,x1,y1);

line(0,0,600,0);

line(0,0,0,600);

if(y>W_ymax) {

rcode_begin[0]=1; // Top

flag=1 ;

if(y<W_ymin) {

rcode_begin[1]=1; // Bottom

flag=1;

if(x>W_xmax) {

rcode_begin[2]=1; // Right

flag=1;

if(x<W_xmin) {

rcode_begin[3]=1; //Left

flag=1;

if(y1>W_ymax){

rcode_end[0]=1; // Top

flag=1;

if(y1<W_ymin) {

rcode_end[1]=1; // Bottom

flag=1;

if(x1>W_xmax){

rcode_end[2]=1; // Right

flag=1;

COMPUTER GRAPHICS CSE-7317L


ABRAR SHAH 19048112004

if(x1<W_xmin){

rcode_end[3]=1; //Left

flag=1;

if(flag==0)

printf("No need of clipping as it is already in window");

flag=1;

for(i=0;i<4;i++){

region_code[i]= rcode_begin[i] && rcode_end[i] ;

if(region_code[i]==1)

flag=0;

if(flag==0)

printf("\n Line is completely outside the window");

else{

slope=(float)(y1-y)/(x1-x);

if(rcode_begin[2]==0 && rcode_begin[3]==1) //left

y=y+(float) (W_xmin-x)*slope ;

x=W_xmin;

if(rcode_begin[2]==1 && rcode_begin[3]==0) // right

y=y+(float) (W_xmax-x)*slope ;

x=W_xmax;

if(rcode_begin[0]==1 && rcode_begin[1]==0) // top

x=x+(float) (W_ymax-y)/slope ;

y=W_ymax;

COMPUTER GRAPHICS CSE-7317L


ABRAR SHAH 19048112004

if(rcode_begin[0]==0 && rcode_begin[1]==1) // bottom

x=x+(float) (W_ymin-y)/slope ;

y=W_ymin;

if(rcode_end[2]==0 && rcode_end[3]==1) //left

y1=y1+(float) (W_xmin-x1)*slope ;

x1=W_xmin;

if(rcode_end[2]==1 && rcode_end[3]==0) // right

y1=y1+(float) (W_xmax-x1)*slope ;

x1=W_xmax;

if(rcode_end[0]==1 && rcode_end[1]==0) // top

x1=x1+(float) (W_ymax-y1)/slope ;

y1=W_ymax;

if(rcode_end[0]==0 && rcode_end[1]==1) // bottom

x1=x1+(float) (W_ymin-y1)/slope ;

y1=W_ymin;

delay(1000);

clearviewport();

rectangle(W_xmin,W_ymin,W_xmax,W_ymax);

line(0,0,600,0);

line(0,0,0,600);

setcolor(RED);

line(x,y,x1,y1);

getch();

closegraph();

COMPUTER GRAPHICS CSE-7317L


ABRAR SHAH 19048112004

OUTPUT :-

COMPUTER GRAPHICS CSE-7317L

You might also like