0% found this document useful (0 votes)
3 views

Cg Practical

The document contains multiple C programs that utilize the graphics.h library to draw various shapes and implement graphical transformations. Programs include drawing lines, circles, rectangles, ellipses, triangles, and implementing techniques like flood fill, 2-D mirror reflection, shearing, translation, and Bresenham's circle algorithm. Each program initializes graphics mode, performs the drawing or transformation, and then closes the graphics mode after waiting for a key press.

Uploaded by

ujjwalpanghal262
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)
3 views

Cg Practical

The document contains multiple C programs that utilize the graphics.h library to draw various shapes and implement graphical transformations. Programs include drawing lines, circles, rectangles, ellipses, triangles, and implementing techniques like flood fill, 2-D mirror reflection, shearing, translation, and Bresenham's circle algorithm. Each program initializes graphics mode, performs the drawing or transformation, and then closes the graphics mode after waiting for a key press.

Uploaded by

ujjwalpanghal262
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/ 32

1. Program To Draw Line.

#include <graphics.h>

#include <conio.h>

int main() {

int gd = DETECT, gm;

// Initialize graphics mode

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

// Draw a line from (100, 100) to (200, 200)

line(100, 100, 200, 200);

getch(); // Wait for a key press

closegraph(); // Close graphics mode

return 0;

}
2. Program To Draw Circle.

#include <graphics.h>

#include <conio.h>

int main() {

int gd = DETECT, gm;

// Initialize graphics mode

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

// Draw a circle with center at (200, 200) and radius 100

circle(200, 200, 100);

getch(); // Wait for a key press

closegraph(); // Close graphics mode

return 0;

}
3. Program To Draw Rectangle.

#include <graphics.h>

#include <conio.h>

int main() {

int gd = DETECT, gm;

// Initialize graphics mode

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

// Draw a rectangle from top-left (100, 100) to bottom-right (300,


200)

rectangle(100, 100, 300, 200);

getch(); // Wait for a key press

closegraph(); // Close graphics mode

return 0;

}
4.Program To Implementation Flood Fill.

#include <graphics.h>

#include <conio.h>

void floodFill(int x, int y, int oldColor, int newColor) {

int currentColor = getpixel(x, y);

if (currentColor == oldColor) {

putpixel(x, y, newColor);

floodFill(x + 1, y, oldColor, newColor);

floodFill(x - 1, y, oldColor, newColor);

floodFill(x, y + 1, oldColor, newColor);

floodFill(x, y - 1, oldColor, newColor);

int main() {

int gd = DETECT, gm;

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


// Draw a rectangle to fill

rectangle(100, 100, 200, 200);

// Call flood fill inside the rectangle

int x = 150, y = 150; // A point inside the rectangle

int oldColor = getpixel(x, y);

int newColor = RED;

floodFill(x, y, oldColor, newColor);

getch();

closegraph();

return 0;

}
5. Program To Draw Ellipse.

#include <graphics.h>

#include <conio.h>

int main() {

int gd = DETECT, gm;

// Initialize graphics mode

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

// Set color (optional)

setcolor(WHITE);

// Draw an ellipse

// Syntax: ellipse(x, y, start_angle, end_angle, x_radius, y_radius)

ellipse(250, 200, 0, 360, 100, 50);

getch(); // Wait for a key press


closegraph(); // Close graphics mode

return 0;

}
6. Program To Implement 2-D Mirror Reflection.

#include <graphics.h>

#include <conio.h>

#include <dos.h>

void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int color) {

setcolor(color);

line(x1, y1, x2, y2);

line(x2, y2, x3, y3);

line(x3, y3, x1, y1);

int main() {

int gd = DETECT, gm;

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

// Original triangle coordinates

int x1 = 100, y1 = 100;

int x2 = 150, y2 = 50;


int x3 = 200, y3 = 100;

// Draw axes

line(0, getmaxy()/2, getmaxx(), getmaxy()/2); // X-axis

line(getmaxx()/2, 0, getmaxx()/2, getmaxy()); // Y-axis

// Shift origin to center of screen

int originX = getmaxx()/2;

int originY = getmaxy()/2;

// Draw original triangle (Blue)

drawTriangle(originX + x1, originY - y1,

originX + x2, originY - y2,

originX + x3, originY - y3, BLUE);

// Reflect across Y-axis: change sign of x-coordinates

drawTriangle(originX - x1, originY - y1,

originX - x2, originY - y2,

originX - x3, originY - y3, RED);


outtextxy(10, 10, "Blue: Original | Red: Reflected (Y-axis)");

getch();

closegraph();

return 0;

}
7. Program To Draw A Smile.

#include <graphics.h>

#include <conio.h>

int main() {

int gd = DETECT, gm;

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

int centerX = getmaxx() / 2;

int centerY = getmaxy() / 2;

// Face

setcolor(YELLOW);

setfillstyle(SOLID_FILL, YELLOW);

fillellipse(centerX, centerY, 100, 100); // Face circle

// Eyes

setcolor(BLACK);

setfillstyle(SOLID_FILL, BLACK);
fillellipse(centerX - 35, centerY - 30, 10, 15); // Left eye

fillellipse(centerX + 35, centerY - 30, 10, 15); // Right eye

// Smile (arc)

setcolor(RED);

arc(centerX, centerY + 10, 200, 340, 50); // Smile arc

// Label

setcolor(WHITE);

outtextxy(centerX - 40, centerY + 120, "Keep Smiling :)");

getch();

closegraph();

return 0;

}
8. Program To Implement 2-D Shearing.

#include <graphics.h>

#include <conio.h>

// Function to draw rectangle using 4 points

void drawRectangle(int x1, int y1, int x2, int y2, int color) {

setcolor(color);

rectangle(x1, y1, x2, y2);

int main() {

int gd = DETECT, gm;

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

// Original rectangle coordinates

int x1 = 100, y1 = 100;

int x2 = 200, y2 = 200;

// Draw original rectangle (blue)


drawRectangle(x1, y1, x2, y2, BLUE);

outtextxy(x1, y2 + 10, "Original Rectangle");

// Shearing factors

float shx = 1.0; // X-shear factor

float shy = 0.5; // Y-shear factor

// Coordinates after X-shear (only x changes)

int x1_xshear = x1 + shx * y1;

int x2_xshear = x2 + shx * y1;

int x3_xshear = x2 + shx * y2;

int x4_xshear = x1 + shx * y2;

// Y remains the same for X-shear

int y1_xshear = y1;

int y2_xshear = y1;

int y3_xshear = y2;

int y4_xshear = y2;

// Draw X-sheared rectangle (red)


setcolor(RED);

line(x1_xshear, y1_xshear, x2_xshear, y2_xshear);

line(x2_xshear, y2_xshear, x3_xshear, y3_xshear);

line(x3_xshear, y3_xshear, x4_xshear, y4_xshear);

line(x4_xshear, y4_xshear, x1_xshear, y1_xshear);

outtextxy(x1_xshear, y4_xshear + 10, "X-sheared");

// Coordinates after Y-shear (only y changes)

int x1_yshear = x1;

int x2_yshear = x2;

int x3_yshear = x2;

int x4_yshear = x1;

int y1_yshear = y1 + shy * x1;

int y2_yshear = y1 + shy * x2;

int y3_yshear = y2 + shy * x2;

int y4_yshear = y2 + shy * x1;

// Draw Y-sheared rectangle (green)

setcolor(GREEN);
line(x1_yshear, y1_yshear, x2_yshear, y2_yshear);

line(x2_yshear, y2_yshear, x3_yshear, y3_yshear);

line(x3_yshear, y3_yshear, x4_yshear, y4_yshear);

line(x4_yshear, y4_yshear, x1_yshear, y1_yshear);

outtextxy(x1_yshear, y4_yshear + 10, "Y-sheared");

getch();

closegraph();

return 0;

}
9. Program To Draw Circle Using Bresenham’s Algorithm.

#include <graphics.h>

#include <conio.h>

#include <dos.h>

// Function to plot all 8 symmetrical points

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

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);

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

int x = 0;
int y = r;

int d = 3 - 2 * r;

while (x <= y) {

plotCirclePoints(xc, yc, x, y);

if (d < 0) {

d = d + 4 * x + 6;

} else {

d = d + 4 * (x - y) + 10;

y--;

x++;

delay(10); // Optional: to visualize drawing step-by-step

int main() {

int gd = DETECT, gm;

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


int xc = getmaxx() / 2;

int yc = getmaxy() / 2;

int radius = 100;

drawCircleBresenham(xc, yc, radius);

outtextxy(xc - 70, yc + radius + 20, "Bresenham's Circle Algorithm");

getch();

closegraph();

return 0;

}
10. Program To Implement 2-D Translation.

#include <graphics.h>

#include <conio.h>

// Function to draw triangle

void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int color) {

setcolor(color);

line(x1, y1, x2, y2);

line(x2, y2, x3, y3);

line(x3, y3, x1, y1);

int main() {

int gd = DETECT, gm;

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

// Original coordinates of triangle

int x1 = 100, y1 = 100;

int x2 = 150, y2 = 50;


int x3 = 200, y3 = 100;

// Translation factors

int tx = 100, ty = 50;

// Draw original triangle (blue)

drawTriangle(x1, y1, x2, y2, x3, y3, BLUE);

outtextxy(x1, y1 + 10, "Original Triangle");

// Calculate new translated coordinates

int x1_new = x1 + tx;

int y1_new = y1 + ty;

int x2_new = x2 + tx;

int y2_new = y2 + ty;

int x3_new = x3 + tx;

int y3_new = y3 + ty;

// Draw translated triangle (red)

drawTriangle(x1_new, y1_new, x2_new, y2_new, x3_new, y3_new,


RED);
outtextxy(x1_new, y1_new + 10, "Translated Triangle");

getch();

closegraph();

return 0;

You might also like