0% found this document useful (0 votes)
2 views24 pages

1prac Cgr Merged

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 24

/*Write a Program to draw basic graphics construction like line, circle, arc,

ellipse and rectangle.*/

#include<stdio.h>
#include<graphics.h
> #include<conio.h>
void main(){
int gd=DETECT,gm;
initgraph (&gd,&gm,"c:\\tc\\bgi");
setbkcolor(GREEN); printf("\t\t\t\n\
nLINE"); line(50,40,190,40);
printf("\t\t\n\n\n\nRECTANGLE");
rectangle(125,115,215,165);
printf("\t\t\t\n\n\n\n\n\n\nARC");
arc(120,200,180,0,30);
printf("\t\n\n\n\nCIRCLE");
circle(120,270,30); printf("\t\n\n\
n\nECLIPSE");
ellipse(120,350,0,360,30,20);
getch();}
OUTPUT
//2 Program to implement DDA Line Drawing Algorithm:

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

void main()
{
intgd = DETECT
,gm, i; float x,
y,dx,dy,steps; int
x0, x1, y0, y1;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setbkcolor(WHITE);
x0 = 100 , y0 = 200, x1 = 500, y1 = 300;
dx = (float)(x1 - x0);
dy = (float)(y1 - y0);

if(dx>=dy)
{
steps = dx;
}

else
{
steps = dy;
}

dx =
dx/steps; dy
= dy/steps;
x = x0;
y=
y0; i
= 1;

while(i<= steps)
{
putpixel(x, y,
RED); x += dx;
y +=
dy;
i=i+1;
}
getch();
closegraph
();

OUTPUT:
//3.Program to implement Bresenham's Line Drawing 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;
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;
}

Outpu
t:
//4. Program to draw a circle using Bresenham's circle
drawing algorithm:

#include
<graphics.h>
#include
<stdlib.h>
#include <stdio.h>
#include
<conio.h>
#include
<math.h>

void EightWaySymmetricPlot(int xc,int yc,int x,int y)


{
putpixel(x+xc,y+yc,RED);
putpixel(x+xc,-
y+yc,YELLOW); putpixel(-
x+xc,-y+yc,GREEN);
putpixel(-
x+xc,y+yc,YELLOW);
putpixel(y+xc,x+yc,12);
putpixel(y+xc,-x+yc,14);
putpixel(-y+xc,-x+yc,15);
putpixel(-y+xc,x+yc,6);
}

void BresenhamCircle(int xc,int yc,int r)


{
int x=0,y=r,d=3-(2*r);
EightWaySymmetricPlot(xc,yc,x,y
);

while(x<=y)
{
if(d<=0)
{
d=d+(4*x)+6;
}
else
{
d=d+(4*x)-(4*y)
+10; y=y-1;
}
x=x+1;
EightWaySymmetricPlot(xc,yc,x
,y);
}
}

int main(void)
{
/* request auto detection */
int xc,yc,r,gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\
BGI");

/* read result of
initialization */ errorcode =
graphresult();

if (errorcode != grOk) /* an error occurred */


{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
printf("Enter the values of xc and yc :");
scanf("%d%d",&xc,&yc);
printf("Enter the value of
radius :"); scanf("%d",&r);
BresenhamCircle(xc,yc,r);

getch();
closegraph
(); return
0;
}

Output:
/* 5. C Program to draw a Polygon and fill color in C language */

#include <graphics.h>

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

int main(){

/* Declaring a variable */

int gdriver, gmode;

/* Polygon array to define points on the polygon shape */

int poly[10];

/* Initialize the variables */

gdriver = DETECT;

/* Initialize the graph and set path to BGI files */

initgraph(&gdriver, &gmode, "D:\\TURBOC3\\BGI");

/* Polygon Points in Pairs */

poly[0] = 20; /* 1st vertex */

poly[1] = 100;

poly[2] = 120;

poly[3] = 140; /* 2nd vertex */

poly[4] = 240;

poly[5] = 260; /* 3rd vertex */


poly[6] = 120;

poly[7] = 320; /* 4th vertex */

poly[8] = poly[0];

poly[9] = poly[1]; /* The polygon does

not close automatically, so we close it */

/* Set the fill style for the Polygon

*/ setcolor(getmaxcolor());

setfillstyle(SOLID_FILL, RED);

/* Draw the Polygon */

fillpoly(5, poly);

getch();

/* Close the graph */

closegraph();

return 0;}

OUTPUT:
//6. C Implementation for Boundary Filling Algorithm

#include <graphics.h>

// Function for 8 connected Pixels

void boundaryFill8(int x, int y, int fill_color,int boundary_color)

if(getpixel(x, y) != boundary_color &&

getpixel(x, y) != fill_color)

putpixel(x, y, fill_color);

boundaryFill8(x + 1, y, fill_color, boundary_color);

boundaryFill8(x, y + 1, fill_color, boundary_color);

boundaryFill8(x - 1, y, fill_color, boundary_color);

boundaryFill8(x, y - 1, fill_color, boundary_color);

boundaryFill8(x - 1, y - 1, fill_color, boundary_color);

boundaryFill8(x - 1, y + 1, fill_color, boundary_color);

boundaryFill8(x + 1, y - 1, fill_color, boundary_color);

boundaryFill8(x + 1, y + 1, fill_color, boundary_color);

}
//driver code

int main()

// gm is Graphics mode which is

// a computer display mode that

// generates image using pixels.

// DETECT is a macro defined in

// "graphics.h" header file

int gd = DETECT, gm;

// initgraph initializes the

// graphics system by loading a

// graphics driver from disk

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

// Rectangle function

rectangle(50, 50, 100, 100);

// Function calling

boundaryFill8(55, 55, 4, 15);

delay(10000);

getch();

closegraph();

return 0;

}
Output :
// 7. Prgram for two dimensional transformations (Scaling)

#include<stdio.h>

#include<graphics.h>

#include<math.h>

#include<iostream>

using namespace std;

int main()

int gd=0,gm,x1,y1,x2,y2,x3,y3;

double s,c, angle;

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

setcolor(RED);

cout<<"Enter coordinates of triangle: ";

cin>>x1>>y1;

cin>>x2>>y2;

cin>>x3>>y3;

setbkcolor(WHITE);

cleardevice();

line(x1,y1,x2,y2);

line(x2,y2, x3,y3);

line(x3, y3, x1, y1);

cout<<"Enter rotation angle: ";

cin>>angle;
c = cos(angle *M_PI/180); s = sin(angle *M_PI/180);

x1 = floor(x1 * c + y1 * s); y1 = floor(-x1 * s + y1 * c); x2 = floor(x2 * c + y2 * s);


y2 = floor(-x2 * s + y2 * c); x3 = floor(x3 * c + y3 * s); y3 = floor(-x3 * s + y3 * c);
setcolor(GREEN);

line(x1, y1 ,x2, y2);

line(x2,y2, x3,y3);

line(x3, y3, x1, y1); getch(); closegraph();

return 0;

Output
// 8.Program to rotate a Triangle:
#include<stdio.h>
#include<graphics.h
>
#include<math.h>
main()
{
intgd=0,gm,x1,y1,x2,y2,x3,y3;
double s,c, angle;
initgraph(&gd, &gm, "C:\\TURBOC3\\
BGI"); setcolor(RED);
printf("Enter coordinates of triangle: ");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,
&x3, &y3); setbkcolor(WHITE);
cleardevice();
line(x1,y1,x2,y2);
line(x2,y2, x3,y3);
line(x3, y3, x1,
y1); getch();
setbkcolor(BLAC
K);
printf("Enter rotation
angle: "); scanf("%lf",
&angle);
setbkcolor(WHITE);
c = cos(angle
*M_PI/180); s =
sin(angle *M_PI/180);
x1 = floor(x1 * c + y1
* s); y1 = floor(-x1 * s
+ y1 * c); x2 =
floor(x2 * c + y2 * s);
y2 = floor(-x2 * s + y2
* c); x3 = floor(x3 * c
+ y3 * s); y3 = floor(-
x3 * s + y3 * c);
cleardevice();
line(x1, y1 ,x2, y2);
line(x2,y2, x3,y3);
line(x3, y3, x1,
y1); getch();
closegraph();
return 0;
}

Output:

Before rotation
After rotation
// 9.C program for two dimensional transformation (Reflection and Shearing)

#include <conio.h>

#include <graphics.h>

#include <stdio.h>

// Driver Code

void main()

// Initialize the drivers

int gm, gd = DETECT, ax, x1 = 100;

int x2 = 100, x3 = 200, y1 = 100;

int y2 = 200, y3 = 100;

// Add in your BGI folder path

// like below initgraph(&gd, &gm,

// "C:\\TURBOC3\\BGI");

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

cleardevice();

// Draw the graph

line(getmaxx() / 2, 0, getmaxx() / 2,

getmaxy());

line(0, getmaxy() / 2, getmaxx(),

getmaxy() / 2);

// Object initially at 2nd quadrant

printf("Before Reflection Object"


" in 2nd Quadrant");
// Set the color

setcolor(14);

line(x1, y1, x2, y2);

line(x2, y2, x3, y3);

line(x3, y3, x1, y1);

getch();

// After reflection

printf("\nAfter Reflection");

// Reflection along origin i.e.,

// in 4th quadrant

setcolor(4);

line(getmaxx() - x1, getmaxy() - y1,

getmaxx() - x2, getmaxy() - y2);

line(getmaxx() - x2, getmaxy() - y2,

getmaxx() - x3, getmaxy() - y3);

line(getmaxx() - x3, getmaxy() - y3,

getmaxx() - x1, getmaxy() - y1);

// Reflection along x-axis i.e.,

// in 1st quadrant

setcolor(3);

line(getmaxx() - x1, y1,

getmaxx() - x2, y2);

line(getmaxx() - x2, y2,


getmaxx() - x3, y3);

line(getmaxx() - x3, y3,

getmaxx() - x1, y1);

// Reflection along y-axis i.e.,

// in 3rd quadrant

setcolor(2);

line(x1, getmaxy() - y1, x2,

getmaxy() - y2);

line(x2, getmaxy() - y2, x3,

getmaxy() - y3);

line(x3, getmaxy() - y3, x1,

getmaxy() - y1);

getch();

// Close the graphics

closegraph();

}
OUTPUT

You might also like