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

Computer Graphic Lab File

The document contains multiple C code snippets that utilize the graphics.h library to create various graphical applications, including drawing shapes, animations, and a digital clock. Each code segment demonstrates different functionalities such as drawing lines, circles, and rectangles, as well as handling user input for interactive graphics. The code is intended for use with Turbo C++ and includes comments for clarity and guidance on setup.

Uploaded by

niteeshyadav175
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Computer Graphic Lab File

The document contains multiple C code snippets that utilize the graphics.h library to create various graphical applications, including drawing shapes, animations, and a digital clock. Each code segment demonstrates different functionalities such as drawing lines, circles, and rectangles, as well as handling user input for interactive graphics. The code is intended for use with Turbo C++ and includes comments for clarity and guidance on setup.

Uploaded by

niteeshyadav175
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Question 1

Code

#include <graphics.h>

#include <conio.h>

void main() {

int gd = DETECT, gm; // Corrected variable declaration

initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Ensure the path is correct for your setup

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

closegraph(); // Close the graphics mode


}
Question 2

Cdoe

#include <graphics.h>

#include <conio.h>

void main() {

int gd = DETECT, gm, i, x, y; // Corrected variable declaration

// initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Ensure the path is correct for your setup

initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Ensure the path is correct for your setup

x = getmaxx() / 3; // Calculate x-coordinate

y = getmaxy() / 3; // Calculate y-coordinate (corrected from getmaxx to getmaxy)

setbkcolor(WHITE); // Set background color to white

setcolor(BLUE); // Set drawing color to blue

for (i = 1; i <= 8; i++) {

setfillstyle(i, i); // Set fill pattern and color

delay(20); // Add a small delay for animation effect

circle(x, y, i * 20); // Draw a circle with increasing radius

floodfill(x - 2 + i * 20, y, BLUE); // Fill the circle with color

getch(); // Wait for a key press

closegraph(); // Close the graphics mode

}
Question 3

Code

// #include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <stdlib.h>

#include <time.h> // For srand() and rand()

void main() {

int gd = DETECT, gm, i = 0, x, xx, y, yy, r;

// Initialize the graphics system

// initgraph(&gd, &gm, "C:\\TC\\BGI"); // Ensure the path is correct for your setup

initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Ensure the path is correct for your setup

x = getmaxx(); // Get maximum x-coordinate of the screen

y = getmaxy(); // Get maximum y-coordinate of the screen

srand(time(0)); // Seed the random number generator

while (!kbhit()) { // Continue until a key is pressed

i++;

xx = rand() % x; // Random x-coordinate

yy = rand() % y; // Random y-coordinate

r = rand() % 30; // Random radius

circle(xx, yy, r); // Draw a circle at random position with random radius

setfillstyle(rand() % 12, rand() % 16); // Random fill style and color

floodfill(xx, yy, getmaxcolor()); // Fill the circle with random color

delay(200); // Delay for animation effect

}
getch(); // Wait for a key press

closegraph(); // Close the graphics mode

}
Question 4

Code

#include <graphics.h>

#include <conio.h>

int main() {

int gd = DETECT, gm, i, maxx, cy; // Corrected variable declaration

// initgraph(&gd, &gm, "C:\\TC\\BGI"); // Ensure the path is correct for your setup

initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Ensure the path is correct for your setup

setbkcolor(WHITE); // Set background color to white

setcolor(RED); // Set drawing color to red

maxx = getmaxx(); // Get maximum x-coordinate of the screen

cy = getmaxy() / 2; // Get the middle y-coordinate of the screen

for (i = 0; i < maxx - 140; i++) {

cleardevice(); // Clear the screen for smooth animation

// Draw the car body

line(0 + i, cy - 20, 0 + i, cy + 15);

line(0 + i, cy - 20, 25 + i, cy - 20);

line(25 + i, cy - 20, 40 + i, cy - 70);

line(40 + i, cy - 70, 100 + i, cy - 70);

line(100 + i, cy - 70, 115 + i, cy - 20);

line(115 + i, cy - 20, 140 + i, cy - 20);

line(0 + i, cy + 15, 18 + i, cy + 15);

circle(28 + i, cy + 15, 10); // Left wheel

line(38 + i, cy + 15, 102 + i, cy + 15);

circle(112 + i, cy + 15, 10); // Right wheel

line(122 + i, cy + 15, 140 + i, cy + 15);


line(140 + i, cy + 15, 140 + i, cy - 20);

// Draw the car window

rectangle(50 + i, cy - 62, 90 + i, cy - 30);

// Fill the car body with blue color

setfillstyle(1, BLUE);

floodfill(5 + i, cy - 15, RED);

// Fill the car window with light blue color

setfillstyle(1, LIGHTBLUE);

floodfill(52 + i, cy - 60, RED);

delay(20); // Adjust delay for smoother animation

getch(); // Wait for a key press

closegraph(); // Close the graphics mode

return 0;

}
Question 5

Code

#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <dos.h>

void main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Updated path for Turbo C++ 3.0

setbkcolor(9); // Set background color to light blue

setcolor(WHITE); // Set drawing color to white

// Draw the horizontal line

line(100, 100, 370, 100);

// Draw the first vertical line

line(120, 100, 120, 170);

// Draw the first arc

arc(143, 100, 0, 180, 23);

// Draw the second vertical line

line(165, 100, 165, 155);

// Draw the second arc

arc(150, 155, 100, 0, 15);

// Draw the third vertical line

line(180, 100, 180, 170);


// Draw the first circle and line

circle(210, 140, 10);

line(210, 130, 250, 130);

// Draw the second circle and line

circle(280, 140, 10);

line(280, 130, 330, 130);

// Draw the fourth vertical line

line(330, 100, 330, 170);

// Draw the fifth vertical line

line(345, 100, 345, 170);

// Draw the ellipse

ellipse(337, 100, 0, 180, 9, 18);

getch(); // Wait for a key press

closegraph(); // Close the graphics mode

}
Question 6

Ccdoe

#include <graphics.h>

#include <stdio.h>

#include <conio.h> // Added for getch()

void main() {

int gd = DETECT, gm, x, y, r = 40; // Corrected variable declaration

char ch;

initgraph(&gd, &gm, "C:\\TURBOC3\\BGI"); // Corrected path with backslashes

setbkcolor(3); // Set background color to cyan

x = getmaxx() / 2; // Initialize x to the center of the screen

y = getmaxy() / 2; // Initialize y to the center of the screen

setfillstyle(1, RED); // Set fill style to solid and color to red

circle(x, y, r); // Draw a circle at the center

floodfill(x, y, getmaxcolor()); // Fill the circle with red

while ((ch = getch()) != 13) { // Continue until the Enter key (ASCII 13) is pressed

switch (ch) {

case 75: // Left arrow key

if (x >= r + 10) { // Check if the circle can move left

cleardevice(); // Clear the screen

circle(x -= 10, y, r); // Move the circle left by 10 pixels

floodfill(x, y, getmaxcolor()); // Fill the circle

break;

case 72: // Up arrow key

if (y >= r + 10) { // Check if the circle can move up

cleardevice(); // Clear the screen


circle(x, y -= 10, r); // Move the circle up by 10 pixels

floodfill(x, y, getmaxcolor()); // Fill the circle

break;

case 77: // Right arrow key

if (x <= (getmaxx() - r - 10)) { // Check if the circle can move right

cleardevice(); // Clear the screen

circle(x += 10, y, r); // Move the circle right by 10 pixels

floodfill(x, y, getmaxcolor()); // Fill the circle

break;

case 80: // Down arrow key

if (y <= (getmaxy() - r - 10)) { // Check if the circle can move down

cleardevice(); // Clear the screen

circle(x, y += 10, r); // Move the circle down by 10 pixels

floodfill(x, y, getmaxcolor()); // Fill the circle

break;

closegraph(); // Close the graphics mode

}
Question 7

Code

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

struct time t;

void display(int,int,int);

void main()

int i=0,gd=DETECT,gm,hr,min,sec;

clrscr();

initgraph(&gd,&gm,"c:\\turboc3\\bgi");

setcolor(GREEN);

settextstyle(4,0,7);

while(!kbhit())

gettime(&t);

hr=t.ti_hour;

min=t.ti_min;

sec=t.ti_sec;

i++;

display(100,100,hr);

display(200,100,min);

display(300,100,sec);

sound(400);

delay(30);

nosound();
delay(930);

cleardevice();

getch();

void display(int x,int y,int num)

char str[3];

itoa(num,str,10);

settextstyle(4,0,7);

outtextxy(180,100,":");

outtextxy(280,100,":");

outtextxy(x,y,str);

rectangle(90,90,380,200);

rectangle(70,70,400,220);

outtextxy(90,250,"Digital Clock");

}
Question 8

Code

#include <stdio.h>

#include <graphics.h>

#include <conio.h> // Added for kbhit() and getch()

#define HEIGHT getmaxy()

#define WIDTH getmaxx()

#define GROUND 450

#define MAXHEIGHT 420

void main() {

int x, y = 0, t = MAXHEIGHT, c = 1;

int gd = DETECT, gm;

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

for (x = 40; x <= getmaxx(); x += 2) {

// Draw Ground

setcolor(WHITE);

rectangle(0, MAXHEIGHT, getmaxx(), MAXHEIGHT + 5);

floodfill(5, MAXHEIGHT + 3, WHITE);

// Draw Ball

setcolor(RED);

setfillstyle(SOLID_FILL, RED);

pieslice(x, y, 0, 360, 20);

delay(50); // Adjust delay for smoother animation

// Ball movement logic

if (y > MAXHEIGHT - 20) {


c = 0;

t -= 40;

if (y <= (MAXHEIGHT - t)) {

c = 1;

if (t >= 40) {

y += (c ? 15 : -15);

cleardevice(); // Clear the screen for the next frame

// Exit upon keypress

if (kbhit()) {

break;

closegraph(); // Close the graphics mode

}
Question 9

Code

#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <dos.h>

int main() {

int gd = DETECT, gm;

int x, y, flag = 0;

initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Updated path for Turbo C++ 3.0

x = getmaxx() / 2; // Initialize x to the center of the screen

y = 30; // Initialize y to the top of the screen

while (!kbhit()) { // Continue until a key is pressed

// Toggle the flag when the ball reaches the top or bottom

if (y >= getmaxy() - 30 || y <= 30) {

flag = !flag;

// Draw the red ball

setcolor(RED);

setfillstyle(SOLID_FILL, RED);

circle(x, y, 30); // Draw the circle

floodfill(x, y, RED); // Fill the circle with red

delay(50); // Delay for smooth animation

cleardevice(); // Clear the screen for the next frame

// Move the ball up or down based on the flag


if (flag) {

y += 5; // Move the ball down

} else {

y -= 5; // Move the ball up

getch(); // Wait for a key press

closegraph(); // Close the graphics mode

return 0;

}
Question 10

Code

#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <math.h>

int x1, y1, x2, y2, x3, y3;

void draw();

void rotate();

int main(void) {

int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Updated path for Turbo C++ 3.0

printf("Enter first co-ordinate value for triangle: ");

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

printf("Enter second co-ordinate values for triangle: ");

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

printf("Enter third co-ordinate values for triangle: ");

scanf("%d%d", &x3, &y3);

draw(); // Draw the original triangle

getch(); // Wait for a key press

rotate(); // Rotate the triangle

getch(); // Wait for a key press

closegraph(); // Close the graphics mode

return 0;

}
void draw() {

// Draw the triangle using the given coordinates

line(x1, y1, x2, y2);

line(x2, y2, x3, y3);

line(x3, y3, x1, y1);

void rotate() {

int a1, a2, a3, b1, b2, b3;

float angle;

// Calculate the centroid (pivot point) of the triangle

int a = (x1 + x2 + x3) / 3;

int b = (y1 + y2 + y3) / 3;

printf("Enter the rotation angle in degrees: ");

scanf("%f", &angle);

cleardevice(); // Clear the screen

// Convert the angle from degrees to radians

angle = (angle * 3.14) / 180;

// Calculate the new coordinates after rotation

a1 = a + (x1 - a) * cos(angle) - (y1 - b) * sin(angle);

b1 = b + (x1 - a) * sin(angle) + (y1 - b) * cos(angle);

a2 = a + (x2 - a) * cos(angle) - (y2 - b) * sin(angle);

b2 = b + (x2 - a) * sin(angle) + (y2 - b) * cos(angle);


a3 = a + (x3 - a) * cos(angle) - (y3 - b) * sin(angle);

b3 = b + (x3 - a) * sin(angle) + (y3 - b) * cos(angle);

printf("ROTATION");

printf("\nChanged coordinates:\n");

printf("%d %d\n%d %d\n%d %d", a1, b1, a2, b2, a3, b3);

// Draw the rotated triangle

line(a1, b1, a2, b2);

line(a2, b2, a3, b3);

line(a3, b3, a1, b1);

}
Question 11

Code

#include<stdio.h>

#include<graphics.h>

void drawcircle(int x0, int y0, int radius)

int x = radius;

int y = 0;

int err = 0;

while (x >= y)

putpixel(x0 + x, y0 + y, 7);

putpixel(x0 + y, y0 + x, 7);

putpixel(x0 - y, y0 + x, 7);

putpixel(x0 - x, y0 + y, 7);

putpixel(x0 - x, y0 - y, 7);

putpixel(x0 - y, y0 - x, 7);

putpixel(x0 + y, y0 - x, 7);

putpixel(x0 + x, y0 - y, 7);

if (err <= 0)

y += 1;

err += 2*y + 1;

if (err > 0)

x -= 1;

err -= 2*x + 1;
}

void main()

int gdriver=DETECT, gmode, error, x, y, r;

initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

printf("Enter radius of circle: ");

scanf("%d", &r);

printf("Enter co-ordinates of center(x and y): ");

scanf("%d%d", &x, &y);

drawcircle(x, y, r);

getch();

}
Question 12

Code

#include <stdio.h>

#include <graphics.h>

#include <math.h>

#include <stdlib.h>

#include <conio.h> // For getch()

#define pi 3.14

// Function prototypes

void drawSine(float amplitude);

void drawCosine(float amplitude);

void drawTangent(float amplitude);

void initializeGraphics();

int main() {

int choice;

float amplitude;

// Initialize graphics mode

initializeGraphics();

// Input amplitude

printf("Enter the maximum value for Y (amplitude): ");

scanf("%f", &amplitude);

// Menu

printf("\nMENU:\n");

printf("1. SINE CURVE\n");

printf("2. COSINE CURVE\n");


printf("3. TANGENT CURVE\n");

printf("4. EXIT\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

drawSine(amplitude);

break;

case 2:

drawCosine(amplitude);

break;

case 3:

drawTangent(amplitude);

break;

case 4:

exit(0);

default:

printf("Invalid choice!\n");

break;

getch(); // Wait for a key press

closegraph(); // Close the graphics mode

return 0;

void drawSine(float amplitude) {

int xm, ym;

float x, y;
cleardevice();

xm = getmaxx() / 2;

ym = getmaxy() / 2;

// Draw axes

line(xm, 0, xm, 2 * ym); // Y-axis

line(0, ym, 2 * xm, ym); // X-axis

outtextxy(xm + 5, 10, "Y-AXIS");

outtextxy(2 * xm - 50, ym + 5, "X-AXIS");

// Draw sine curve

for (x = -300; x <= 300; x += 0.5) {

y = amplitude * sin((pi * x) / 180);

putpixel(x + xm, -y + ym, RED);

void drawCosine(float amplitude) {

int xm, ym;

float x, y;

cleardevice();

xm = getmaxx() / 2;

ym = getmaxy() / 2;

// Draw axes

line(xm, 0, xm, 2 * ym); // Y-axis

line(0, ym, 2 * xm, ym); // X-axis

outtextxy(xm + 5, 10, "Y-AXIS");

outtextxy(2 * xm - 50, ym + 5, "X-AXIS");


// Draw cosine curve

for (x = -300; x <= 300; x += 0.5) {

y = amplitude * cos((pi * x) / 180);

putpixel(x + xm, -y + ym, RED);

void drawTangent(float amplitude) {

int xm, ym;

float x, y;

cleardevice();

xm = getmaxx() / 2;

ym = getmaxy() / 2;

// Draw axes

line(xm, 0, xm, 2 * ym); // Y-axis

line(0, ym, 2 * xm, ym); // X-axis

outtextxy(xm + 5, 10, "Y-AXIS");

outtextxy(2 * xm - 50, ym + 5, "X-AXIS");

// Draw tangent curve

for (x = -300; x <= 300; x += 0.5) {

y = amplitude * tan((pi * x) / 180);

putpixel(x + xm, -y + ym, RED);

void initializeGraphics() {

int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Updated path for Turbo C++ 3.0


int errorcode = graphresult();

if (errorcode != grOk) {

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt: ");

getch();

exit(1);

}
Question 13

Cdoe

#include <graphics.h>

#include <conio.h>

int main() {

int gd = DETECT, gm;

int left = 100, top = 100, right = 200, bottom = 200;

int x = 300, y = 150, radius = 50;

// Initialize graphics mode

initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Updated path for Turbo C++ 3.0

// Draw a rectangle

rectangle(left, top, right, bottom);

// Draw a circle

circle(x, y, radius);

// Draw a bar (filled rectangle)

bar(left + 300, top, right + 300, bottom);

// Draw a horizontal line

line(left - 10, top + 150, left + 410, top + 150);

// Draw an ellipse

ellipse(x, y + 200, 0, 360, 100, 50);

// Display text

outtextxy(left + 100, top + 325, "My first C graphics program");


getch(); // Wait for a key press

closegraph(); // Close the graphics mode

return 0;

}
Question 14

Code

#include <graphics.h>

#include <conio.h>

int main()

int gd = DETECT, gm;

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

setcolor(YELLOW);

rectangle(0,30,639,450);

settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);

setcolor(WHITE);

outtextxy(275,0,"Bar Chart");

setlinestyle(SOLID_LINE,0,2);

line(100,420,100,60);

line(100,420,600,420);

line(90,70,100,60);

line(110,70,100,60);

line(590,410,600,420);

line(590,430,600,420);

outtextxy(95,35,"Y");

outtextxy(610,405,"X");

outtextxy(85,415,"O");

setfillstyle(LINE_FILL,BLUE);

bar(150,100,200,419);

setfillstyle(XHATCH_FILL,RED);

bar(225,150,275,419);

setfillstyle(WIDE_DOT_FILL,GREEN);

bar(300,200,350,419);
setfillstyle(INTERLEAVE_FILL,MAGENTA);

bar(375,125,425,419);

setfillstyle(HATCH_FILL,BROWN);

bar(450,175,500,419);

getch();

return 0;

}
Question 15

Code

#include <graphics.h>

#include <math.h>

#include <stdio.h>

#include <conio.h> // For getch()

// Function for finding absolute value

int abs(int n) {

return ((n > 0) ? n : (n * (-1)));

// Custom round function (since round() may not be available in Turbo C++)

int round(float num) {

return (int)(num + 0.5);

// DDA Function for line generation

void DDA(int X0, int Y0, int X1, int Y1) {

// Calculate dx & dy

int dx = X1 - X0;

int dy = Y1 - Y0;

// Calculate steps required for generating pixels

int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);

// Calculate increment in x & y for each step

float Xinc = dx / (float)steps;

float Yinc = dy / (float)steps;

// Put pixel for each step


float X = X0;

float Y = Y0;

int i; // Declare loop variable at the beginning of the block

for (i = 0; i <= steps; i++) {

putpixel(round(X), round(Y), RED); // Put pixel at (X, Y)

X += Xinc; // Increment in x at each step

Y += Yinc; // Increment in y at each step

delay(50); // Delay for visualization of line generation

// Driver program

int main() {

int gd = DETECT, gm;

int X0, Y0, X1, Y1; // Declare all variables at the beginning of the block

// Initialize graphics function

initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Updated path for Turbo C++ 3.0

// Coordinates of the line

X0 = 100;

Y0 = 100;

X1 = 400;

Y1 = 300;

// Function call to draw the line using DDA algorithm

DDA(X0, Y0, X1, Y1);

getch(); // Wait for a key press

closegraph(); // Close the graphics mode

return 0;
}
Question 16

Code

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

}
Question 17

Code

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void main()

int xc=340,yc=280,y,r,x;

float theta;

int gd=DETECT,gm;

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

//printf("Enter the center of circle:");

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

printf("Enter radius of circle:");

scanf("%d",&r);

for(theta=0;theta<=2*3.14;theta+=0.01)

x=(int)(xc+(r*cos(theta)));

y=(int)(yc+(r*sin(theta)));

putpixel(x,y,WHITE);

getch();

closegraph();

}
Question 18

Code

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void main()

int gd=DETECT,gm;

int r,x,y,p,xc=320,yc=240;

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

cleardevice();

printf("Enter the radius");

scanf("%d",&r);

x=0;

y=r;

putpixel(xc+x,yc-y,1);

p=3-(2*r);

for(x=0;x<=y;x++)

if(p<0)

y=y;

p=(p+(4*x)+6);

else

y=y-1;
p=p+((4*(x-y)+10));

putpixel(xc+x,yc-y,1);

putpixel(xc-x,yc-y,2);

putpixel(xc+x,yc+y,3);

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

putpixel(xc+y,yc-x,5);

putpixel(xc-y,yc-x,6);

putpixel(xc+y,yc+x,7);

putpixel(xc-y,yc+x,8);

getch();

closegraph();

}
Question 19

Code

//Write a program to implement Cohen Sutherland line clipping algorithm.

#include<graphics.h>

#include<conio.h>

#include<stdio.h>

const int t=1,b=2,r=2,l=8;

float xmin,ymin,xmax,ymax;

int calcode(float x,float y)

int code=0;

if(y>ymax)

code |=t;

else if(y<ymin)

code|=b;

else if(x>xmax)

code|=r;

else if(x<xmin)

code|=l;

return(code);

void lineclip(float x1,float y1,float x2,float y2)

unsigned int code1,code2,codeout;

int accept=0,done=0;

code1=calcode(x1,y1);

code2=calcode(x2,y2);

do

if(!(code1|code2))
{

accept=1;

done=1;

else if(code1 & code2)

done=1;

else

float x,y;

codeout=code1?code1:code2;

if(codeout& t)

x=x1+(x2-x1)*(ymax-y1)/(y2-y1);

y=ymax;

else if(codeout& b)

x=x1+(x2-x1)*(ymin-y1)/(y2-y1);

y=ymin;

else if(codeout& r)

y=y1+(y2-y1)*(xmax-x1)/(x2-x1);

x=xmax;

else

y=y1+(y2-y1)*(xmin-x1)/(x2-x1);

x=xmin;

if(codeout==code1)
{

x1=x;

y1=y;

code1=calcode(x1,y1);

else

x2=x;

y2=y;

code2=calcode(x2,y2);

while(done==0);

if(accept)

line(x1,y1,x2,y2);

rectangle(xmin,ymin,xmax,ymax);

main()

float x1,y1,x2,y2;

int gd=DETECT,gm;

clrscr();

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

printf("Enter the co-ordinates of line ::::\n\t x1");

scanf("%f",&x1);

printf("\n\t y1:");

scanf("%f",&y1);

printf("\n\t x2:");

scanf("%f",&x2);

printf("\n\t y2:");
scanf("%f",&y2);

printf("Enter the co-ordinates of window :::\n");

printf("\n\txmin");

scanf("%f",&xmin);

printf("\n\tymin");

scanf("%f",&ymin);

printf("\n\txmax");

scanf("%f",&xmax);

printf("\n\tymax");

scanf("%f",&ymax);

clrscr();

line(x1,y1,x2,y2);

rectangle(xmin,ymin,xmax,ymax);

getch();

clrscr();

lineclip(x1,y1,x2,y2);

getch();

closegraph();

return 0;

}
Question 20

Code

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

#include<math.h>

int main()

int gd = DETECT,gm,a,b,i,r,x,y;

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

//draw the bottom rectangle

setfillstyle(SOLID_FILL,1);

rectangle(10,320,200,340);

floodfill(11,321,15);

rectangle(30,300,175,320);

floodfill(31,301,15);

rectangle(50,280,155,300);

floodfill(51,281,15);

//draw the pole

setfillstyle(SOLID_FILL,3);

rectangle(100,38,110,280);

floodfill(101,39,15);

//draw the top rectangle

setfillstyle(SOLID_FILL,RED);

rectangle(110,40,220,58);

floodfill(111,43,15);

setfillstyle(SOLID_FILL,15);

rectangle(110,58,220,78);

floodfill(111,59,15);
setfillstyle(SOLID_FILL,GREEN);

rectangle(110,78,220,98);

floodfill(111,79,15);

//Ashok chakra

a=160;

b=68;

r=13;

setcolor(BLUE);

circle(a,b,r);

for(i=0;i<=360;i=i+25)

x=r*cos(i*3.14/180);

y=r*sin(i*3.14/180);

line(a,b,a+x,b-y);

getch();

return 0;

// closegraph();

You might also like