You are on page 1of 11

Experiment No – 07

Aim – Write a program to fill color by


1. Boundary fill algorithm
2. Flood fill algorithm
Program –
1. Boundary fill Algorithm
#include<iostream>
#include<graphics.h>
void boundaryFill(int x,int y,int f_col,int b_col) {
if(getpixel(x,y)!=b_col && getpixel(x,y)!=f_col) {
putpixel(x,y,f_col);
boundaryFill(x+1,y,f_col,b_col);
boundaryFill(x-1,y,f_col,b_col);
boundaryFill(x,y+1,f_col,b_col);
boundaryFill(x,y-1,f_col,b_col);
} }
int main() {
printf("Name: Aastha Mahajan");
printf(" (0701CS211001)");
initwindow(800,400);
rectangle(100, 150, 400, 300);
rectangle(400,260,500,290);
arc(400,260,0,90,100);
line(400,260,500,260);
circle(150, 320, 20);
circle(350, 320, 20);
boundaryFill(250, 210, RED, WHITE);
boundaryFill(351, 320, DARKGRAY, WHITE);
boundaryFill(151,320,DARKGRAY,WHITE);
boundaryFill(401,240,CYAN,WHITE);
boundaryFill(401,261,BLUE,WHITE);
closegraph();
return 0;
}

Page | 1 Aastha Mahajan


(0701CS211001)
2. Flood fill Algorithm
Program –
#include<iostream>
#include<conio.h>
#include<graphics.h>
void floodFill(int x,int y,int fillColor,int defaultColor) {
if(getpixel(x,y)==defaultColor) {
putpixel(x,y,fillColor);
floodFill(x+1,y,fillColor,defaultColor);
floodFill(x-1,y,fillColor,defaultColor);
floodFill(x,y+1,fillColor,defaultColor);
floodFill(x,y-1,fillColor,defaultColor);
} }
int main() {
printf("Name: Aastha Mahajan");
printf(" (0701CS211001)");
initwindow(800,400);
rectangle(100, 150, 400, 300);
rectangle(400,260,500,290);
arc(400,260,0,90,100);
line(400,260,500,260);
circle(150, 320, 20);
circle(350, 320, 20);
floodFill(251, 210, RED, BLACK);
floodFill(351, 320, DARKGRAY, BLACK);
floodFill(151,320,DARKGRAY,BLACK);
floodFill(401,240,CYAN,BLACK);
floodFill(401,261,BLUE,BLACK);
closegraph();
return 0;
}

Page | 2 Aastha Mahajan


(0701CS211001)
OUTPUT –

Page | 3 Aastha Mahajan


(0701CS211001)
Experiment No – 08
Aim – Write a program to translate a triangle.
Program –
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
int main() {
int gd = DETECT, gm;
int x, y, x1, y1, x2, y2, tx, ty;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
printf("\n Please enter first coordinate of the triangle = ");
scanf("%d %d", &x, &y);
printf("\n Enter second coordinate of the trinagle = ");
scanf("%d %d", &x1, &y1);
printf("\n Enter third coordinate of the triangle = ");
scanf("%d %d", &x2, &y2);
printf("\n\t\t********** TRIANGLE before & after translation ***********");
line(x, y, x1, y1);
line(x1, y1, x2, y2);
line(x2, y2, x, y);
printf("\n Now enter the translation vector = ");
scanf("%d %d", &tx, &ty);
setcolor(WHITE);
line(x + tx, y + ty, x1 + tx, y1 + ty);
line(x1 + tx, y1 + ty, x2 + tx, y2 + ty);
line(x2 + tx, y2 + ty, x + tx, y + ty);
getch();
closegraph();
return 0;
}

Page | 4 Aastha Mahajan


(0701CS211001)
OUTPUT -

Page | 5 Aastha Mahajan


(0701CS211001)
Experiment No – 09
Aim – Write a program to scale a triangle.
Program –
int main() {

initwindow(1200,800);

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

float sx,sy;

printf("Enter first x coordinate : ");

scanf("%d",&x1);

printf("Enter first y coordinate : ");

scanf("%d",&y1);

printf("Enter second x coordinate : ");

scanf("%d",&x2);

printf("Enter second y coordinate : ");

scanf("%d",&y2);

printf("Enter third x coordinate : ");

scanf("%d",&x3);

printf("Enter third y coordinate : ");

scanf("%d",&y3);

line(x1,y1,x2,y2);

line(x2,y2,x3,y3);

line(x3,y3,x1,y1);

printf("Enter Scaling factor(Sx) : ");

scanf("%f",&sx);

printf("Enter Scaling factor(Sy) : ");

Page | 6 Aastha Mahajan


(0701CS211001)
scanf("%f",&sy);

x1=x1*sx;

y1=y1*sy;

x2=x2*sx;

y2=y2*sy;

x3=x3*sx;

y3=y3*sy;

line(x1,y1,x2,y2);

line(x2,y2,x3,y3);

line(x3,y3,x1,y1);

getch();

return 0;

Page | 7 Aastha Mahajan


(0701CS211001)
OUTPUT -

Page | 8 Aastha Mahajan


(0701CS211001)
Experiment No – 10
Aim – Write a program to rotate a triangle.
Program –
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#define PI 3.14159265

void rotate(int *x, int *y, int angle, int cx, int cy) {
double radians = (PI / 180) * angle;
int newX = cx + (*x - cx) * cos(radians) - (*y - cy) * sin(radians);
int newY = cy + (*x - cx) * sin(radians) + (*y - cy) * cos(radians);
*x = newX;
*y = newY;
}

int main() {
int gd = DETECT, gm;
int x1, y1, x2, y2, x3, y3;
int angle, centerX, centerY;
printf("Enter coordinates of triangle (x1 y1 x2 y2 x3 y3): ");
scanf("%d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x3, &y3);
printf("Enter the rotation angle: ");
scanf("%d", &angle);
printf("Enter the coordinates of rotation center (cx cy): ");
scanf("%d %d", &centerX, &centerY);
initgraph(&gd, &gm, "");
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);

Page | 9 Aastha Mahajan


(0701CS211001)
// Rotating the triangle
rotate(&x1, &y1, angle, centerX, centerY);
rotate(&x2, &y2, angle, centerX, centerY);
rotate(&x3, &y3, angle, centerX, centerY);
// Drawing the rotated triangle
setcolor(WHITE);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
getch();
closegraph();
return 0;
}

Page | 10 Aastha Mahajan


(0701CS211001)
OUTPUT –

Page | 11 Aastha Mahajan


(0701CS211001)

You might also like