1prac Cgr Merged
1prac Cgr Merged
1prac Cgr Merged
#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>
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();
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 poly[10];
gdriver = DETECT;
poly[1] = 100;
poly[2] = 120;
poly[4] = 240;
poly[8] = poly[0];
*/ setcolor(getmaxcolor());
setfillstyle(SOLID_FILL, RED);
fillpoly(5, poly);
getch();
closegraph();
return 0;}
OUTPUT:
//6. C Implementation for Boundary Filling Algorithm
#include <graphics.h>
getpixel(x, y) != fill_color)
putpixel(x, y, fill_color);
}
//driver code
int main()
// Rectangle function
// Function calling
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>
int main()
int gd=0,gm,x1,y1,x2,y2,x3,y3;
setcolor(RED);
cin>>x1>>y1;
cin>>x2>>y2;
cin>>x3>>y3;
setbkcolor(WHITE);
cleardevice();
line(x1,y1,x2,y2);
line(x2,y2, x3,y3);
cin>>angle;
c = cos(angle *M_PI/180); s = sin(angle *M_PI/180);
line(x2,y2, x3,y3);
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()
// "C:\\TURBOC3\\BGI");
cleardevice();
line(getmaxx() / 2, 0, getmaxx() / 2,
getmaxy());
getmaxy() / 2);
setcolor(14);
getch();
// After reflection
printf("\nAfter Reflection");
// in 4th quadrant
setcolor(4);
// in 1st quadrant
setcolor(3);
// in 3rd quadrant
setcolor(2);
getmaxy() - y2);
getmaxy() - y3);
getmaxy() - y1);
getch();
closegraph();
}
OUTPUT