Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
27 views11 pages

Computer Graphics

The document contains code snippets from a student named Opila Singh. The code snippets show programs to perform basic graphics operations using functions like putpixel, line, circle, rectangle in C graphics. These include plotting a pixel, drawing lines and shapes, animating a circle, drawing a graph and rainbow, and implementing line drawing algorithms like DDA.

Uploaded by

singhopila
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)
27 views11 pages

Computer Graphics

The document contains code snippets from a student named Opila Singh. The code snippets show programs to perform basic graphics operations using functions like putpixel, line, circle, rectangle in C graphics. These include plotting a pixel, drawing lines and shapes, animating a circle, drawing a graph and rainbow, and implementing line drawing algorithms like DDA.

Uploaded by

singhopila
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/ 11

Name: Opila Singh

Roll No: 29
BCA ‘G’

Problem Statement: Write a program to plot a pixel in screen.


Code:

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

int main() { int gd =


DETECT, gm; int x =
100, y = 100;
initgraph(&gd, &gm, "");
putpixel(x, y, WHITE);
getch();
closegraph(); return
0;
}
Name: Opila Singh
Roll No: 29
BCA ‘G’

Problem Statement: Write a program to implement the predefined graphic functions.


Code:

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

int main() { int gd =


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

line(100, 100, 300, 100);


rectangle(100, 150, 300, 250);
circle(400, 200, 50);
ellipse(550, 200, 0, 360, 100, 50);

getch();
closegraph(); return
0;
}
Name: Opila Singh
Roll No: 29
BCA ‘G’

Problem Statement: Write a program to Plot a Graph using Line function.


Code:

#include<graphics.h>
int main( )
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
int x=100, y=100; for(int
i=0; i<=300; i++)
{ putpixel(x+i,y,RED);
}
for(int i=0; i<=300; i++)
{ putpixel(x,y+i,GREEN);
}
for(int i=0; i<=250; i++)
{ putpixel(x+i,y+i,CYAN);
} getch( );
closegraph( );
}
Name: Opila Singh
Roll No: 29
BCA ‘G’

Problem Statement: Write a program to draw a rainbow using arc.


Code:
#include <graphics.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, const_cast<char *>(""));
for (int i = 90; i <= 100; i++)
{
setcolor(RED);//4 arc(300,
250, 0, 180, 100 + i);
setcolor(LIGHTRED);//12
arc(300, 250, 0, 180, 90 + i);
setcolor(YELLOW);//14
arc(300, 250, 0, 180, 80 + i);
setcolor(GREEN);//2
arc(300, 250, 0, 180, 70 + i);
setcolor(BLUE);//1 arc(300,
250, 0, 180, 60 + i);
setcolor(LIGHTBLUE);//9
arc(300, 250, 0, 180, 50 + i);
setcolor(MAGENTA);//4
arc(300, 250, 0, 180, 40 + i);
// delay(150);
} getch();
closegraph();
return 0; }
Name: Opila Singh
Roll No: 29
BCA ‘G’

Problem Statement: Write a program to draw a figure using the combination of graphic
functions.
Code:

#include<graphics.h>
int main( )
{
int gd=0,gm;
initgraph(&gd,&gm,(char*)" ");

line(50,245,550,245); //for Road

rectangle(80,200,255,230);
rectangle(150,175,180,200);
rectangle(250,200,255,205);

line(130,175,200,175);
line(130,175,110,200);
line(200,175,220,200);

circle(125,232,12);
circle(205,232,12);

setcolor(YELLOW);
floodfill(81,201,WHITE);

setcolor(WHITE);
floodfill(206,231,BLACK);
floodfill(126,231,BLACK);
floodfill(206,228,BLACK);
floodfill(126,228,BLACK);

setcolor(YELLOW);
floodfill(251,201,WHITE);
getch( );
closegraph( );

return 0;

}
Name: Opila Singh
Roll No: 29
BCA ‘G’

Problem Statement: Write a program to move a circle to- and-fro in linear motion.
Code:

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

int main() {
int gd = DETECT, gm;
int x = 50, y = 200;
int xm, ym;

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

xm = getmaxx();
ym = getmaxy();

for (int i = 0; i < 5; i++) {


while (x < (xm - 50) && y < (ym - 50)) {
delay(50);
cleardevice();
circle(x, y, 50);
x += 3;
}

while (x > 50 && y > 50) {


delay(50);
cleardevice();
circle(x, y, 50);
x -= 3;
}
}

getch();
closegraph();
return 0;
}
Name: Opila Singh
Roll No: 29
BCA ‘G’

Problem Statement: Write a program to draw a line using equation y=mx+c.


Code:

#include <graphics.h>

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");

int x1 = 100, y1 = 50; // Starting point


int x2 = 400, y2 = 250; // Ending point

float m = (float)(y2 - y1) / (x2 - x1); // Slope


int c = y1 - m * x1; // Y-intercept

for (int x = x1; x <= x2; x++) {


int y = m * x + c;
putpixel(x, y, WHITE);
}

getch();
closegraph();
return 0; }
Name: Opila Singh
Roll No: 29
BCA ‘G’

Problem Statement: Write a program to draw a line using DDA.


Code:

#include <graphics.h>
int main() {
float x, y, x1, y1, x2, y2, dx, dy, steps;
int i, gd = DETECT, gm;

printf("Enter (x1, y1) : ");


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

printf("Enter (x2,y2): ");


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

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

dx = abs(x2 - x1);
dy = abs(y2 - y1);

if (dx >= dy)


steps = dx;
else steps
= dy;

dx = dx / steps;
dy = dy / steps;

x = x1;
y = y1;
i = 1;

while (i <= steps) {


putpixel(x, y, 5);
x = x + dx; y=y
+ dy; i = i + 1;
delay(50);
}

delay(5000);
closegraph();
}

You might also like