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

0% found this document useful (0 votes)
95 views15 pages

CG Practical

The document contains a series of practical programming exercises using C/C++ graphics libraries, focusing on drawing shapes, implementing algorithms for line and circle drawing, and performing geometric transformations like translation, scaling, and rotation on triangles. Each exercise includes a specific aim and the corresponding source code. The exercises cover basic graphics operations and algorithms such as DDA and Bresenham's algorithms for line drawing, as well as circle drawing algorithms.

Uploaded by

jkboss gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views15 pages

CG Practical

The document contains a series of practical programming exercises using C/C++ graphics libraries, focusing on drawing shapes, implementing algorithms for line and circle drawing, and performing geometric transformations like translation, scaling, and rotation on triangles. Each exercise includes a specific aim and the corresponding source code. The exercises cover basic graphics operations and algorithms such as DDA and Bresenham's algorithms for line drawing, as well as circle drawing algorithms.

Uploaded by

jkboss gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Practical no 1 A. Aim – Write a program to display a line.

Source code—
#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

int gd=DETECT,gm;

clrscr();

initgraph(&gd,&gm,"c:\\tc\\BGI");

line(100,100,200,1200);

getch();

closegraph();

B. Aim-- Coordinate calculation of SUN


Source code---
#include <graphics.h>

#include <conio.h>

#include <stdio.h>

int main() {

int gd = DETECT, gm;

// Provide the correct BGI path for Turbo C++

initgraph(&gd, &gm, "C:\\TC\\BGI");

// Check if graphics initialized successfully

if (graphresult() != grOk) {

printf("Graphics initialization failed! Check BGI path.\n");

return 1;

// Draw lines

line(300, 50, 300, 180);


line(370, 250, 500, 250);

line(300, 320, 300, 450);

line(100, 250, 230, 250);

// Draw a circle

circle(300, 250, 70);

// Wait for key press

getch();

// Close graphics mode

closegraph();

return 0;

Practical no 2 A. Aim—Divide your screen into four regions draw circle, rectangle,
ellipse and half ellipse in each region with appropriate message.
Source code—
#include <graphics.h>

#include <conio.h>

#include <stdio.h>

void main() {

int gd = DETECT, gm;

// Initialize graphics mode

initgraph(&gd, &gm, "C:\\TC\\BGI");

int xmax, ymax;

xmax = getmaxx();

ymax = getmaxy();

// Draw X and Y axis

line(xmax / 2, 0, xmax / 2, ymax);

line(0, ymax / 2, xmax, ymax / 2);


// Draw circle

circle(120, 120, 100);

outtextxy(100, 220, "Circle");

// Draw rectangle

rectangle(20, 250, 270, 400);

outtextxy(50, 410, "Rectangle");

// Draw arc

arc(450, 150, 50, 140, 100);

outtextxy(480, 170, "Arc");

// Draw ellipse

ellipse(500, 300, 0, 360, 100, 50);

outtextxy(460, 360, "Ellipse");

getch();

closegraph();

B. Aim— Draw a simple hut on screen


Source code---
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

void main() {

int gd = DETECT, gm;

// Initialize graphics

initgraph(&gd, &gm, "C:\\TC\\BGI");

// Draw lines (Roof of the house)

line(100, 100, 320, 10);

line(320, 10, 500, 100);


// Draw rectangles (House structure)

rectangle(100, 100, 500, 400);

rectangle(150, 150, 400, 400);

getch(); // Wait for key press

closegraph(); // Close graphics mode

Practical no 3 A. Aim—Draw following basic shapes. 1.line 2.Rectangle 3.Arc 4.Circle


5.ellipse
Source code—
#include <stdio.h>

#include <graphics.h>

#include <conio.h>

void main() {

int gd = DETECT, gm;

// Initialize graphics

initgraph(&gd, &gm, "C:\\TC\\BGI");

// Set background color

setbkcolor(BLACK);

cleardevice(); // Clear screen to apply background color

// Display text and draw shapes

outtextxy(50, 20, "LINE");

line(50, 40, 190, 40);

outtextxy(50, 100, "RECTANGLE");

rectangle(125, 115, 215, 165);

outtextxy(50, 180, "ARC");

arc(120, 200, 180, 0, 30);


outtextxy(50, 250, "CIRCLE");

circle(120, 270, 30);

outtextxy(50, 320, "ELLIPSE");

ellipse(120, 350, 0, 360, 30, 20);

getch(); // Wait for key press

closegraph(); // Close graphics mode

Practical no 4 A. Aim—Develop a program for DDA line drawing algorithm

Source code—
#include <graphics.h>

#include <conio.h>

#include <stdio.h>

#include <math.h>

#include <dos.h>

void main() {

int gd = DETECT, gm;

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

int i;

// Initialize graphics

initgraph(&gd, &gm, "C:\\TC\\BGI");

// Get user input

printf("Enter the value of x1 and y1: ");

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

printf("Enter the value of x2 and y2: ");

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

// Calculate dx and dy

dx = abs(x2 - x1);

dy = abs(y2 - y1);
// Calculate steps

if (dx > dy)

step = dx;

else

step = dy;

// Calculate increment

dx = (x2 - x1) / step;

dy = (y2 - y1) / step;

x = x1;

y = y1;

i = 1;

// Draw the line using DDA Algorithm

while (i <= step) {

putpixel(x, y, WHITE);

x += dx;

y += dy;

i++;

delay(100); // Delay for visibility

getch(); // Wait for key press

closegraph(); // Close graphics mode

B. Aim—Develop the program for Bradenham’s line drawing algorithm .


Source code—
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <stdlib.h> // For abs()
#include <dos.h> // For delay()
void drawline(int x0, int y0, int x1, int y1) {
int dx, dy, p, x, y;

dx = abs(x1 - x0);
dy = abs(y1 - y0);
p = 2 * dy - dx;

x = x0;
y = y0;

// Handling different cases for direction


int x_increment = (x1 > x0) ? 1 : -1;
int y_increment = (y1 > y0) ? 1 : -1;

while (x != x1) {
putpixel(x, y, WHITE);

if (p >= 0) {
y += y_increment;
p += 2 * (dy - dx);
} else {
p += 2 * dy;
}

x += x_increment;
delay(50); // Small delay for visualization
}
}

void main() {
int gd = DETECT, gm;
int x0, y0, x1, y1;

// Initialize graphics
initgraph(&gd, &gm, "C:\\TC\\BGI");

// Get user input


printf("Enter coordinates of first point (x0 y0): ");
scanf("%d %d", &x0, &y0);

printf("Enter coordinates of second point (x1 y1): ");


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

// Draw line using Bresenham's Algorithm


drawline(x0, y0, x1, y1);

getch(); // Wait for key press


closegraph(); // Close graphics mode
}

Practical no 5 A. Aim— Write a program to display Bradenham ‘s Circle.


Source code—
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>

void drawCircle(int xc, int yc, int x, int y) {


putpixel(xc + x, yc + y, RED);
putpixel(xc - x, yc + y, RED);
putpixel(xc + x, yc - y, RED);
putpixel(xc - x, yc - y, RED);
putpixel(xc + y, yc + x, RED);
putpixel(xc - y, yc + x, RED);
putpixel(xc + y, yc - x, RED);
putpixel(xc - y, yc - x, RED);
}
void Circlebres(int xc, int yc, int r) {
int x = 0, y = r;
int d = 3 - (2 * r);

drawCircle(xc, yc, x, y);

while (y >= x) {
x++;
if (d > 0) {
y--;
d = d + 4 * (x - y) + 10;
} else {
d = d + 4 * x + 6;
}
drawCircle(xc, yc, x, y);
delay(50);
}
}

void main() {
int xc = 100, yc = 100, r = 50;
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI"); // Initialize graphics


Circlebres(xc, yc, r); // Function call

getch();
closegraph(); // Close graphics mode
}

B. Aim— Develop the program for the mid – point circle drawing algorithm
Source code—
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h> // For delay()

void drawCircle(int xc, int yc, int r) {


int x = 0, y = r;
int d = 1 - r;

do {
putpixel(xc + x, yc + y, 4);
putpixel(xc + x, yc - y, 4);
putpixel(xc - x, yc + y, 4);
putpixel(xc - x, yc - y, 4);
putpixel(xc + y, yc + x, 4);
putpixel(xc + y, yc - x, 4);
putpixel(xc - y, yc + x, 4);
putpixel(xc - y, yc - x, 4);

if (d < 0) {
d = d + 2 * x + 3;
} else {
y = y - 1;
d = d + 2 * (x - y) + 5;
}

x++;
delay(50);
} while (x <= y);
}

void main() {
int xc, yc, r;
int gd = DETECT, gm;
clrscr(); // Clear screen

printf("Enter center (xc, yc): ");


scanf("%d %d", &xc, &yc);

printf("Enter radius: ");


scanf("%d", &r);

initgraph(&gd, &gm, "C:\\TC\\BGI"); // Initialize graphics mode

drawCircle(xc, yc, r);

getch();
closegraph();
}

Practical no 6 A. Aim—To perform the translation on a given triangle.


Source code—
#include <iostream.h>
#include <conio.h>
#include <graphics.h>

void main() {
int gd = DETECT, gm;
int x1 = 100, y1 = 100, x2 = 80, y2 = 150, x3 = 120, y3 = 150;
int tx, ty;

clrscr(); // Clear screen


initgraph(&gd, &gm, "C:\\TC\\BGI"); // Initialize graphics mode

// Draw original triangle


line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
// Get translation values
cout << "Enter translation value along x-axis: ";
cin >> tx;
cout << "Enter translation value along y-axis: ";
cin >> ty;

// Apply translation
x1 += tx; x2 += tx; x3 += tx;
y1 += ty; y2 += ty; y3 += ty;

// Draw translated triangle


line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);

getch(); // Wait for user input


closegraph(); // Close graphics mode
}

B. Aim— To perform the scaling transformation on a given triangle.


Source code—
#include <iostream.h>
#include <conio.h>
#include <graphics.h>

void main() {
int gd = DETECT, gm;
int x1 = 100, y1 = 100, x2 = 80, y2 = 150, x3 = 120, y3 = 150;
int sx, sy;

clrscr(); // Clear the screen


initgraph(&gd, &gm, "C:\\TC\\BGI"); // Initialize graphics mode
// Draw original triangle
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);

// Get scaling values


cout << "Enter scaling value along x-axis: ";
cin >> sx;
cout << "Enter scaling value along y-axis: ";
cin >> sy;

// Apply scaling
x1 *= sx; x2 *= sx; x3 *= sx;
y1 *= sy; y2 *= sy; y3 *= sy;

// Draw scaled triangle


line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);

getch(); // Wait for user input


closegraph(); // Close graphics mode
}

C. Aim— To perform the rotation transformation on a given triangle.


Source code—
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>

void main() {
int gd = DETECT, gm;
int x1 = 200, y1 = 200, x2 = 180, y2 = 250, x3 = 220, y3 = 250;
float angle, radian;
int xr = 200, yr = 200; // Rotation point (about x1, y1)

clrscr();
initgraph(&gd, &gm, "C:\\TC\\BGI"); // Initialize graphics mode

// Draw original triangle


line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);

// Get rotation angle from user


cout << "Enter the rotation angle (in degrees): ";
cin >> angle;

// Convert angle to radians


radian = angle * (3.14159 / 180);

// Apply rotation transformation


int x1_new = xr + (x1 - xr) * cos(radian) - (y1 - yr) * sin(radian);
int y1_new = yr + (x1 - xr) * sin(radian) + (y1 - yr) * cos(radian);

int x2_new = xr + (x2 - xr) * cos(radian) - (y2 - yr) * sin(radian);


int y2_new = yr + (x2 - xr) * sin(radian) + (y2 - yr) * cos(radian);

int x3_new = xr + (x3 - xr) * cos(radian) - (y3 - yr) * sin(radian);


int y3_new = yr + (x3 - xr) * sin(radian) + (y3 - yr) * cos(radian);

// Draw rotated triangle


line(x1_new, y1_new, x2_new, y2_new);
line(x2_new, y2_new, x3_new, y3_new);
line(x3_new, y3_new, x1_new, y1_new);
getch(); // Wait for user input
closegraph(); // Close graphics mode
}

You might also like