CG File
CG File
Practical-1
Write a program to draw the following using pre-defined functions in computer
graphics:
1. Circle
2. Line
3. Rectangle
4. Arc
5. Ellipse
6. Polygon
Also, change the background colour.
Code
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
void drawShapes() {
int points[] = {300, 400, 350, 450, 400, 400, 350, 350, 300, 400};
char info[100];
setbkcolor(CYAN);
cleardevice();
setcolor(RED);
setfillstyle(SOLID_FILL, YELLOW);
setcolor(GREEN);
setcolor(BLUE);
setfillstyle(SOLID_FILL, MAGENTA);
setcolor(YELLOW);
setcolor(WHITE);
Khushi Singhal; 03521402022
setfillstyle(SOLID_FILL, GREEN);
setcolor(BROWN);
setfillstyle(SOLID_FILL, RED);
fillpoly(5, points);
setcolor(YELLOW);
int main() {
drawShapes();
getch();
closegraph();
return 0;
Code Explanation
Graphics Initialization:
initgraph(&gd, &gm, "C:\\Turboc3\\BGI") initializes the graphics mode. Ensure that
the path is correct based on your environment setup.
Changing Background Color:
setbkcolor(CYAN) changes the background color to cyan.
cleardevice() clears the screen and applies the background color.
Drawing Shapes:
Circle: Drawn with circle() and filled with floodfill().
Line: Drawn with line().
Rectangle: Drawn with rectangle() and filled with floodfill().
Arc: Drawn with arc().
Khushi Singhal; 03521402022
Output
Khushi Singhal; 03521402022
Practical-2
Write a program to draw a moving Cycle/ Any other moving graphics image of
your choice using Pre defined functions (basic shapes) in computer graphics
Code
#include <graphics.h>
#include <conio.h>
int x = 200, y = 350;
void drawHut() {
rectangle(50, 300, 150, 400); // Main hut body
rectangle(75, 350, 125, 400); // Door
line(50, 300, 100, 250);
line(100, 250, 150, 300);
line(50, 300, 150, 300);
}
void drawCycle(int x, int y) {
circle(x, y, 20);
circle(x + 80, y, 20);
line(x + 20, y, x + 60, y);
line(x + 20, y, x + 30, y - 30);
line(x + 30, y - 30, x + 60, y - 30);
line(x + 60, y - 30, x + 80, y);}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
drawHut();
while (x < getmaxx() - 100) {
cleardevice();
drawHut();
drawCycle(x, y);
delay(50);
Khushi Singhal; 03521402022
x += 5;
getch();
closegraph();
return 0;
Code Explanation:
Initialization: The program initializes the graphics mode.
Draw Hut: The hut is drawn using basic shapes like rectangles and lines.
Animation: The cycle is moved across the screen by updating its position in a loop.
Graphics Library: The program uses the graphics.h library, which is specific to Turbo
C++. Modern C compilers like GCC do not support this library directly. For modern
systems, you might consider using more advanced libraries like SDL, OpenGL, or SFML.
Running the Code: To run this program, you would need Turbo C++ or similar IDEs
that support the BGI graphics library.
Animation: The cycle moves from left to right by incrementing its x-coordinate in a loop.
Output
Khushi Singhal; 03521402022
Khushi Singhal; 03521402022
Practical-3
Write a program using pre-defined functions in computer graphics to draw a
night sky and stars flickering with moon in the metro City with any other object
of their desire underneath
Code
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
void drawStars() {
int i, x, y;
for(i = 0; i < 100; i++) {
x = rand() % getmaxx();
y = rand() % (getmaxy() / 2);
putpixel(x, y, WHITE);
}
}
void flickerStars() {
int i, x, y;
for(i = 0; i < 100; i++) {
x = rand() % getmaxx();
y = rand() % (getmaxy() / 2);
putpixel(x, y, BLACK);
}
delay(100);
drawStars();
}
void drawMoon(int x, int y, int radius) {
setcolor(WHITE);
setfillstyle(SOLID_FILL, WHITE);
fillellipse(x, y, radius, radius);
}
void drawTree(int x, int y) {
setcolor(BROWN);
setfillstyle(SOLID_FILL, BROWN);
rectangle(x, y, x + 20, y - 60);
floodfill(x + 10, y - 1, BROWN);
setcolor(GREEN);
setfillstyle(SOLID_FILL, GREEN);
fillellipse(x + 10, y - 70, 30, 30);
floodfill(x + 10, y - 70, GREEN);
}
void displayText() {
setcolor(WHITE);
Khushi Singhal; 03521402022
Code Explanation
Stars and Flickering:
drawStars() function draws 100 stars randomly positioned in the upper half of the screen.
flickerStars() creates a flickering effect by turning off stars and redrawing them.
Moon:
drawMoon() function draws a filled ellipse to represent the moon at a specified position.
Tree:
drawTree() function draws a simple tree with a brown rectangle for the trunk and a green
ellipse for the foliage.
Text Display:
displayText() function displays the name and roll number at the top of the screen.
Main Loop:
The program runs in a loop, continuously flickering the stars until a key is pressed.
Khushi Singhal; 03521402022
Output
Khushi Singhal; 03521402022
PRACTICAL-4
Write a program in C to draw our tri-color national flag using setfill,floodfill,
setcolor commands.
Code
#include <graphics.h>
#include <conio.h>
#include <math.h>
void drawFlag() {
int left = 200, top = 100;
int right = left + 300, bottom = top + 60;
int radius = 30;
int centerX = (left + right) / 2;
int centerY;
int i;
float angle;
int x, y;
setcolor(RED);
rectangle(left, top, right, bottom);
setfillstyle(SOLID_FILL, RED);
floodfill((left + right) / 2, (top + bottom) / 2, RED);
top += 60; bottom += 60;
setcolor(WHITE);
rectangle(left, top, right, bottom);
setfillstyle(SOLID_FILL, WHITE);
floodfill((left + right) / 2, (top + bottom) / 2, WHITE);
top += 60; bottom += 60;
setcolor(GREEN);
rectangle(left, top, right, bottom);
setfillstyle(SOLID_FILL, GREEN);
floodfill((left + right) / 2, (top + bottom) / 2, GREEN);
centerY = (top - 60 + bottom) / 2;
setcolor(BLUE);
circle(centerX, centerY, radius);
for (i = 0; i < 24; i++) {
angle = i * 15.0 * M_PI / 180.0;
x = centerX + radius * cos(angle);
y = centerY + radius * sin(angle);
line(centerX, centerY, x, y);
}
setcolor(BROWN);
rectangle(left - 10, 100, left, 400);
setfillstyle(SOLID_FILL, BROWN);
floodfill(left - 5, 150, BROWN);
}
Khushi Singhal; 03521402022
void drawFlagStand() {
int baseLeft = 150, baseTop = 400;
int baseRight = baseLeft + 200, baseBottom = baseTop + 20;
int standLeft, standTop, standRight, standBottom;
setcolor(DARKGRAY);
rectangle(standLeft, standTop, standRight, standBottom);
setfillstyle(SOLID_FILL, DARKGRAY);
floodfill((standLeft + standRight) / 2, (standTop + standBottom) / 2, DARKGRAY);
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
drawFlag();
drawFlagStand();
getch();
closegraph();
return 0;
}
Code Explanation
Saffron, White, and Green Bands- Each band is drawn as a rectangle and filled with the
respective color using floodfill().
Ashoka Chakra- A blue circle is drawn at the center of the white band with 24 spokes
drawn using the line() function.
Flagpole- A brown rectangle represents the flagpole, extending from the top of the flag
to the base of the stand.
Flag Stand- The stand is drawn with two rectangles: a base and a top part, filled with a
dark gray color.
Declarations:
I. int radius = 30; declares the radius of the Ashoka Chakra.
II. int centerX = (left + right) / 2; calculates the X coordinate for the center of the
Ashoka Chakra.
III. int centerY = top - 60 + radius; calculates the Y coordinate for the center of the
Ashoka Chakra, adjusted to be within the white band.
Khushi Singhal; 03521402022
Output
Khushi Singhal; 03521402022
Practical-5
Draw concentric circle on screen along with changing radius of circle. Choose
any key for Shrink and Expand option respectively to show the effects.
Code
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
void drawConcentricCircles(int centerX, int centerY, int radius) {
int i;
setcolor(WHITE);
for (i = radius; i > 0; i -= 10) {
circle(centerX, centerY, i);
}
}
int main() {
int gd = DETECT, gm;
int centerX, centerY;
int radius = 50; // Initial radius
char ch;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
centerX = getmaxx() / 2;
centerY = getmaxy() / 2;
setcolor(YELLOW);
outtextxy(10, 10, "Name: Your Name");
outtextxy(10, 30, "Roll No: Your Roll Number");
drawConcentricCircles(centerX, centerY, radius);
while (1) {
ch = getch();
cleardevice();
setcolor(YELLOW);
outtextxy(10, 10, "Name: khushi singhal");
outtextxy(10, 30, "Roll No: 03521402022");
if (ch == 'w') {
radius += 10;
}
else if (ch == 's') {
radius -= 10;
if (radius < 10) radius = 10;
}
else if (ch == 27) { // ESC key to exit
break;
}
drawConcentricCircles(centerX, centerY, radius);
}
closegraph();
Khushi Singhal; 03521402022
return 0;
}
Code Explanation
Graphics Initialization:
initgraph(&gd, &gm, "C:\\Turboc3\\BGI") initializes the graphics mode. Adjust the path to
where your graphics library is located.
Drawing Concentric Circles:
drawConcentricCircles function draws concentric circles with a given radius.
User Information:
Displays "Name: ChatGPT | Roll No: 12345" on the screen.
Key Input Handling:
The getch() function captures key presses:
+ increases the radius.
- decreases the radius.
ESC key exits the loop and closes the program.
Clear Screen:
cleardevice() clears the screen before redrawing the circles to prevent overlapping graphics.
Output
Khushi Singhal; 03521402022
Khushi Singhal; 03521402022
Practical-6
Write a program in C to draw a line using Digital Differential Analyzer (DDA)
Algorithm.
Code
#include <graphics.h>
#include <dos.h>
#include <math.h>
xinc = (float) dx / s;
yinc = (float) dy / s;
putpixel(x, y, 15);
for (k = 0; k < s; k++) {
x += xinc;
y += yinc;
putpixel((int)x, (int)y, 15);
delay(50);
}
}
void main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
DDAline(100, 200, 250, 450);
outtextxy(10, 10, "Name:khushi singhal");
outtextxy(10, 30, "Roll No:03521402022");
getch();
closegraph();
}
Code Explanation
The DDAline() function draws a line between two points (x1, y1) and (x2, y2) using the
Digital Differential Analyzer (DDA) algorithm.
dx = x2 - x1; dy = y2 - y1;: These variables store the difference between the x and y
coordinates of the two points, giving the slope of the line.
Khushi Singhal; 03521402022
s: This represents the number of steps to be taken. It is the greater of the absolute
values of dx or dy to ensure that the line is drawn with enough pixels to look smooth.
xinc and yinc: These store the incremental change in x and y for each step, calculated
by dividing dx and dy by s. This ensures a smooth line is drawn, taking into account
the slope.
putpixel(x, y, 15);: This function plots a pixel at (x, y) on the graphics screen, with 15
representing the color white (based on the color palette of Turbo C).
Loop (for (k = 0; k < s; k++)): This loop iterates over the calculated steps (s) and
increments x and y by xinc and yinc, drawing each successive pixel on the line.
delay(50);: Adds a 50-millisecond delay between each pixel being drawn, so you can
visually see the line being created.
main() Function:
int gd = DETECT, gm;: Initializes the graphics driver (gd) and graphics mode (gm).
DETECT is a special value that lets the compiler detect the best available graphics
driver.
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");: Initializes the graphics system,
pointing to the location of the BGI (Borland Graphics Interface) files required by
Turbo C to work in graphics mode.
DDAline(100, 200, 250, 450);: Calls the DDAline() function to draw a line from (100,
200) to (250, 450) using the DDA algorithm.
Output
Khushi Singhal; 03521402022
Khushi Singhal; 03521402022
Practical-7
Write a program in C to draw a chessboard using Digital Differential Analyzer
(DDA) Algorithm.
Code
#include<graphics.h>
#include<dos.h>
#include<math.h>
void main() {
int gd = DETECT, gm;
int x0, y0, x1, y1;
initgraph(&gd, &gm, "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);
BresenhamLine(x0, y0, x1, y1);
outtextxy(60, 50, "Name: Khushi Singhal");
outtextxy(60, 80, "Roll No: 03521402022");
getch();
closegraph();
}
Khushi Singhal; 03521402022
Code Explanation
Bresenham's Line Algorithm (BresenhamLine function):
This function draws a line between two points (x0, y0) and (x1, y1) using Bresenham’s Line
Drawing Algorithm.
Function Details:
Input Parameters:
x0, y0: Coordinates of the first point.
x1, y1: Coordinates of the second point.
Variables:
dx = x1 - x0: The change in x-coordinates (distance along the x-axis).
dy = y1 - y0: The change in y-coordinates (distance along the y-axis).
p: Decision parameter used in the algorithm to determine the next pixel to be plotted.
x, y: Current pixel coordinates being plotted.
Algorithm Steps:
Step 1: Initialization:
Set the initial point (x, y) to (x0, y0).
Calculate the initial decision parameter p = 2 * dy - dx.
Step 2: Line Drawing Loop:
The loop runs as long as x is less than x1, meaning it moves along the x-axis from x0 to x1.
For each iteration:
If p >= 0:
Plot the pixel at (x, y) using putpixel(x, y, 7), with 7 being the color (gray).
Move the y-coordinate to the next row (y = y + 1), and update the decision parameter p.
If p < 0:
Plot the pixel, increment x by 1, and update p.
Step 3: Finalize:
The loop continues until the line is completely drawn.
3. main() Function:
int gd = DETECT, gm;: These variables are used to detect the graphics driver (gd) and
graphics mode (gm).
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");: Initializes the graphics system. The BGI path
is necessary for Turbo C to locate the graphics files.
Input Section:
printf("Enter co-ordinates of first point:");: Asks the user to input the first point (x0, y0) for
the line.
scanf("%d%d", &x0, &y0);: Reads the first point coordinates.
printf("Enter co-ordinates of second point:");: Asks for the second point (x1, y1).
scanf("%d%d", &x1, &y1);: Reads the second point coordinates.
Line Drawing:
BresenhamLine(x0, y0, x1, y1);: Calls the BresenhamLine() function to draw the line
between the two points.
Khushi Singhal; 03521402022
Output
Khushi Singhal; 03521402022
Practical-8
Write a program in C to draw a line using Bresenham’s Algorithm.
Code
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
putpixel(x, y, WHITE);
int main() {
int gdriver = DETECT, gmode;
int startX = 100, startY = 100;
int squareSize = 50;
int rows = 6, cols = 6;
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");
drawChessBoard(startX, startY, squareSize, rows, cols);
setcolor(WHITE);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
outtextxy(10, 50, "Name: Khushi Singhal");
outtextxy(10, 30, "Roll No: 03521402022");
getch();
closegraph();
return 0;
}
Code Explanation
drawLineBresenham Function
This function implements Bresenham's Line Drawing Algorithm to draw a straight line
between two points (x1, y1) and (x2, y2).
Parameters: x1, y1, x2, y2 are the starting and ending points of the line.
Variables:
o dx, dy: Differences between x and y coordinates.
Khushi Singhal; 03521402022
drawChessBoard Function
This function draws a chessboard grid and fills the squares alternately with black and white.
Parameters:
o startX, startY: Top-left corner of the chessboard.
o squareSize: Size of each square on the chessboard.
o rows, cols: Number of rows and columns (here, both are 6).
Output
Khushi Singhal; 03521402022
Practical-9
Write a menu driven program in C to draw an Equilateral, Isosceles and Scalene triangle
using line drawing function.
Code
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void drawLine(int x1, int y1, int x2, int y2) {
line(x1, y1, x2, y2);
}
void drawEquilateralTriangle(int x, int y, int side) {
int height = (int)(side * sqrt(3) / 2);
drawLine(x, y, x + side / 2, y - height);
drawLine(x + side / 2, y - height, x + side, y);
drawLine(x, y, x + side, y);
}
void drawIsoscelesTriangle(int x, int y, int base, int height) {
drawLine(x, y, x + base, y); // Base
drawLine(x, y, x + base / 2, y - height); // Left side
drawLine(x + base / 2, y - height, x + base, y); // Right side
}
void drawScaleneTriangle(int x1, int y1, int x2, int y2, int x3, int y3) {
drawLine(x1, y1, x2, y2);
drawLine(x2, y2, x3, y3);
drawLine(x3, y3, x1, y1);
}
int main() {
int gdriver = DETECT, gmode;
int choice, x, y, side, base, height, x2, y2, x3, y3;
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");
Khushi Singhal; 03521402022
while(1) {
cleardevice();
setcolor(WHITE);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
outtextxy(5, 20, "Menu - Draw Triangles");
outtextxy(10, 40, "1. Equilateral Triangle");
outtextxy(10, 60, "2. Isosceles Triangle");
outtextxy(10, 80, "3. Scalene Triangle");
outtextxy(10, 100, "4. Exit");
outtextxy(10, 120, "Enter your choice: ");
scanf("%d", &choice);
if (choice == 4) {
break;
}
switch (choice) {
case 1:
x = 200, y = 300;
side = 150;
drawEquilateralTriangle(x, y, side);
break;
case 2:
x = 150, y = 300;
base = 200;
height = 150;
drawIsoscelesTriangle(x, y, base, height);
break;
case 3:
x = 100, y = 300;
x2 = 250, y2 = 250;
x3 = 200, y3 = 350;
drawScaleneTriangle(x, y, x2, y2, x3, y3);
Khushi Singhal; 03521402022
break;
default:
outtextxy(150, 210, "Invalid Choice! Please try again.");
getch();
continue;
}
setcolor(WHITE);
outtextxy(200, 400, "Name: Khushi Singhal");
outtextxy(200, 430, "Roll No: 03521402022");
getch();
}
closegraph();
return 0;
}
Code Explanation
drawLine Function:
A simple wrapper for the line() function from the graphics.h library to draw a
line between two points (x1, y1) and (x2, y2).
drawEquilateralTriangle Function:
drawIsoscelesTriangle Function:
Draws an isosceles triangle given the base and height. The base is drawn horizontally,
and the sides converge at the top center point.
drawScaleneTriangle Function:
Draws a scalene triangle by connecting three points (x1, y1), (x2, y2), and
(x3, y3), where all sides are of different lengths.
Main Program:
Khushi Singhal; 03521402022
A menu is displayed for the user to choose the type of triangle to draw.
Based on the user's input, the corresponding triangle is drawn using the respective
function.
After drawing, the user's name and roll number are displayed on the screen.
Output
Khushi Singhal; 03521402022
Khushi Singhal; 03521402022
Practical-10
Write a program in C to draw a circle using Bresenham’s Algorithm and present it as a
Compass with 4 directions and indicator
Code
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
void drawCircle(int xc, int yc, int r) {
int x = 0, y = r;
int d = 3 - 2 * r;
while (y >= x) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
x++;
if (d > 0) {
y--;
d = d + 4 * (x - y) + 10;
} else {
d = d + 4 * x + 6;
}
}
}
int main() {
int gd = DETECT, gm;
int xc = 250, yc = 250, radius = 100;
Khushi Singhal; 03521402022
Code explanation
drawCircle function implements Bresenham's circle drawing algorithm, which works by
calculating pixel positions symmetrically in all eight octants.
The program initializes graphics mode using initgraph(), and the circle is drawn using
putpixel().
The name and roll number are printed on the screen using outtextxy().
Output
Khushi Singhal; 03521402022
Practical-11
Write a program in C to draw a line and circle using Mid-Point Algorithms.
Code
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
void drawLine(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
int d = dy - (dx / 2);
int x = x1, y = y1;
putpixel(x, y, WHITE);
while (x < x2) {
x++;
if (d < 0) {
d = d + dy;
} else {
y++;
d = d + (dy - dx);
}
putpixel(x, y, WHITE);
}
}
void drawCircle(int xc, int yc, int r) {
int x = 0, y = r;
int d = 1 - r;
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
Khushi Singhal; 03521402022
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
while (x < y) {
x++;
if (d < 0) {
d = d + 2 * x + 1;
} else {
y--;
d = d + 2 * (x - y) + 1;
}
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
drawLine(100, 200, 300, 200);
drawCircle(200, 200, 100);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
outtextxy(150, 50, "Name: Khushi Singhal");
outtextxy(150, 80, "Roll No: 03521402022");
Khushi Singhal; 03521402022
getch();
closegraph();
return 0;
}
Code explanation
Midpoint Line Algorithm (drawLine) calculates and plots the points between the given two
endpoints of the line using decision parameters.
Midpoint Circle Algorithm (drawCircle) calculates and plots the symmetric points of the circle
using decision parameters.
The program initializes the graphics mode using initgraph() and then uses putpixel() to
draw the circle and line pixel by pixel.
outtextxy() is used to display your name and roll number on the output screen.
Output
Khushi Singhal; 03521402022
Practical-12
Write a program in C to draw a moving cart wheel using Mid-Point algorithm.
Code
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <dos.h>
void drawCircle(int xc, int yc, int r) {
int x = 0, y = r;
int d = 1 - r;
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
while (x < y) {
x++;
if (d < 0) {
d = d + 2 * x + 1;
} else {
y--;
d = d + 2 * (x - y) + 1;
}
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
Khushi Singhal; 03521402022
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
}
}
void drawSpokes(int xc, int yc, int r) {
line(xc, yc, xc + r, yc);
line(xc, yc, xc, yc + r);
line(xc, yc, xc - r, yc);
line(xc, yc, xc, yc - r);
line(xc, yc, xc + r / 2, yc + r / 2);
line(xc, yc, xc - r / 2, yc + r / 2);
line(xc, yc, xc + r / 2, yc - r / 2);
line(xc, yc, xc - r / 2, yc - r / 2);
}
int main() {
int gd = DETECT, gm;
int x = 50, y = 200, r = 50;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
outtextxy(150, 50, "Name: Khushi Singhal");
outtextxy(150, 80, "Roll No: 03521402022");
while (x < getmaxx() - r) {
cleardevice();
outtextxy(150, 50, "Name: Khushi Singhal");
outtextxy(150, 80, "Roll No: 03521402022");
drawCircle(x, y, r);
Khushi Singhal; 03521402022
drawSpokes(x, y, r);
delay(50);
x += 5;
}
getch();
closegraph();
return 0;
}
Code explanation
drawCircle function implements the Midpoint Circle Algorithm to draw the wheel.
drawSpokes function draws the spokes of the wheel, with four horizontal, vertical, and diagonal
lines.
The main part of the program moves the wheel from left to right on the screen. It clears the
previous frame (cleardevice()), redraws the wheel at the new position, and adds a delay
(delay(50)) to control the speed of movement.
outtextxy() displays your name and roll number at the top of the screen in each frame.
Output
Khushi Singhal; 03521402022
Khushi Singhal; 03521402022
Practical-13
Make an analog clock sync with system time using Bresenham circle algo and Bresnham line
algo.
Code
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <time.h>
void drawCircle(int xc, int yc, int r) {
int x = 0, y = r;
int d = 3 - 2 * r;
while (y >= x) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
x++;
if (d > 0) {
y--;
d = d + 4 * (x - y) + 10;
} else {
d = d + 4 * x + 6;
}
}
Khushi Singhal; 03521402022
}
void drawLine(int x1, int y1, int x2, int y2) {
int dx = abs(x2 - x1), dy = abs(y2 - y1);
int sx = (x1 < x2) ? 1 : -1;
int sy = (y1 < y2) ? 1 : -1;
int err = dx - dy, e2;
while (1) {
putpixel(x1, y1, WHITE);
if (x1 == x2 && y1 == y2) break;
e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x1 += sx;
}
if (e2 < dx) {
err += dx;
y1 += sy;
}
}
}
void drawClockHand(int xc, int yc, int length, float angle) {
int x = xc + length * cos(angle * M_PI / 180);
int y = yc - length * sin(angle * M_PI / 180);
drawLine(xc, yc, x, y);
}
void drawClockHands(int xc, int yc, int radius) {
time_t rawtime;
struct tm *current_time;
int hours, minutes, seconds;
Khushi Singhal; 03521402022
getch();
closegraph();
return 0;
}
Code explanation
Bresenham's Circle Algorithm (drawCircle) is used to draw the clock face, which is the outer
circle.
Bresenham's Line Algorithm (drawLine) is used to draw the clock hands, including the hour,
minute, and second hands.
System Time Integration: The time() function from the C library retrieves the system time,
which is used to calculate the angles for the hour, minute, and second hands.
The hour hand moves 30° for each hour (360° / 12 = 30° per hour).
The minute and second hands move 6° for each minute/second (360° / 60 = 6°).
The clock continuously updates every second using a loop and the delay(1000) function to
sync with the system time.
Output
Khushi Singhal; 03521402022
Khushi Singhal; 03521402022
Practical-14
Write a program in C to translate and rotate a square about any of its vertex by 45 degree
around a fixed point point A of the sqaure.
Code
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#define PI 3.14159265
void drawSquare(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x4, y4);
line(x4, y4, x1, y1);
}
void rotatePoint(int x, int y, int x1, int y1, int *x_new, int *y_new, float theta) {
float radian = theta * (PI / 180.0);
*x_new = x1 + (x - x1) * cos(radian) - (y - y1) * sin(radian);
*y_new = y1 + (x - x1) * sin(radian) + (y - y1) * cos(radian);
}
int main() {
int gd = DETECT, gm;
int x1, y1, x2, y2, x3, y3, x4, y4;
int x2_new, y2_new, x3_new, y3_new, x4_new, y4_new;
float theta = 45;
int tx, ty;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
x1 = 100; y1 = 100;
x2 = 200; y2 = 100;
x3 = 200; y3 = 200;
x4 = 100; y4 = 200;
Khushi Singhal; 03521402022
tx = 50; ty = 50;
x1 += tx; y1 += ty;
x2 += tx; y2 += ty;
x3 += tx; y3 += ty;
x4 += tx; y4 += ty;
setcolor(YELLOW);
drawSquare(x1, y1, x2, y2, x3, y3, x4, y4);
outtextxy(150, 50, "Original Square (Translated)");
rotatePoint(x2, y2, x1, y1, &x2_new, &y2_new, theta);
rotatePoint(x3, y3, x1, y1, &x3_new, &y3_new, theta);
rotatePoint(x4, y4, x1, y1, &x4_new, &y4_new, theta);
setcolor(WHITE);
drawSquare(x1, y1, x2_new, y2_new, x3_new, y3_new, x4_new, y4_new);
outtextxy(150, 80, "Square Rotated by 45 Degrees");
settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
outtextxy(150, 400, "Name: Khushi Singhal");
outtextxy(150, 430, "Roll No: 03521402022");
getch();
closegraph();
return 0;
}
Code explanation
Vertices of the Square: We define the square using four vertices (x1, y1), (x2, y2), (x3,
y3), and (x4, y4). Initially, a square is formed with these points.
Translation: We translate all the points of the square by a fixed amount (tx, ty).
Rotation: The rotation is performed using the rotation matrix. We rotate the points (x2, y2),
(x3, y3), and (x4, y4) around the fixed point (x1, y1) (vertex A).
Graphics Output: The original (translated) square is drawn in yellow, and the rotated square is
drawn in white. Your name and roll number are displayed on the screen.
Khushi Singhal; 03521402022
Output
Khushi Singhal; 03521402022
Practical-15
Write a menu driven program in C to apply 4 basic transformation techniques on a triangle. –
Translation , Scaling , Rotation , Reflection
Code
Code explanation
Translation: You can shift the triangle by providing tx and ty, which are added to the
coordinates of each vertex.
Scaling: You can scale the triangle by providing sx (scaling factor in x direction) and sy (scaling
factor in y direction).
Rotation: The triangle can be rotated by providing an angle. The rotation uses the standard 2D
rotation matrix.
Reflection: You can reflect the triangle either over the X-axis or the Y-axis.
Menu: The user can select which transformation to apply from the menu, and the corresponding
transformation will be applied to the triangle.
output
Khushi Singhal; 03521402022