QUESTION7
QUESTION7
#include <graphics.h>
#include <stdio.h>
void flood(int x, int y, int new_col, int old_col)
{
if (getpixel(x, y) == old_col) {
putpixel(x, y, new_col);
flood(x + 1, y, new_col, old_col);
flood(x - 1, y, new_col, old_col);
flood(x, y + 1, new_col, old_col);
flood(x, y - 1, new_col, old_col);
}}
int main()
{
int gm, gd = DETECT;
int top, left, bottom, right;
int x = 11;
int y = 11;
int newcolor = 12;
int oldcolor = 0;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
top = left = 10;
bottom = right = 50;
rectangle(left, top, right, bottom);
flood(x, y, newcolor, oldcolor);
getch();
return 0;
}
OUTPUT 7:
QUESTION 8: WAP in C/C++ to fill the polygon using Boundary fill algorithm.
#include <graphics.h>
void boundaryFill8(int x, int y, int fill_color,int boundary_color)
{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill8(x + 1, y, fill_color, boundary_color);
boundaryFill8(x, y + 1, fill_color, boundary_color);
boundaryFill8(x - 1, y, fill_color, boundary_color);
boundaryFill8(x, y - 1, fill_color, boundary_color);
boundaryFill8(x - 1, y - 1, fill_color, boundary_color);
boundaryFill8(x - 1, y + 1, fill_color, boundary_color);
boundaryFill8(x + 1, y - 1, fill_color, boundary_color);
boundaryFill8(x + 1, y + 1, fill_color, boundary_color);
}}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
rectangle(50, 50, 100, 100);
boundaryFill8(55, 55, 4, 15);
delay(10000);
getch();
closegraph();
return 0;
}
OUTPUT 8:
QUESTION 9: WAP in C/C++ to translate any polygon.
/*2D Translation Rectangle Example Program In C Programming*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void RectAngle(int x, int y, int Height, int Width);
void Translate(int x, int y, int Height, int Width);
void main() {
int gd = DETECT, gm;
int x, y, Height, Width;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
printf("Enter the First point for the Rectangle:");
scanf("%d%d", &x, &y);
printf("Enter the Height&Width for the Rectangle:");
scanf("%d%d", &Height, &Width);
RectAngle(x, y, Height, Width);
getch();
cleardevice();
Translate(x, y, Height, Width);
RectAngle(x, y, Height, Width);
getch();
}
void RectAngle(int x, int y, int Height, int Width) {
line(x, y, x + Width, y);
line(x, y, x, y + Height);
line(x + Width, y, x + Width, y + Height);
line(x, y + Height, x + Width, y + Height);
}
void Translate(int x, int y, int Height, int Width) {
int Newx, Newy, a, b;
printf("Enter the Transaction coordinates");
scanf("%d%d", &Newx, &Newy);
cleardevice();
a = x + Newx;
b = y + Newy;
RectAngle(a, b, Height, Width);
}
OUTPUT 9:
QUESTION 10: WAP in C/C++ to rotate any polygon w.r.t origin.
#include<stdio.h>
#include<graphics.h>
#include<math.h>
main()
{
intgd=0,gm,x1,y1,x2,y2,x3,y3;
double s,c, angle;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
setcolor(RED);
printf("Enter coordinates of triangle: ");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2, &x3, &y3);
setbkcolor(WHITE);
cleardevice();
line(x1,y1,x2,y2);
line(x2,y2, x3,y3);
line(x3, y3, x1, y1);
getch();
setbkcolor(BLACK);
printf("Enter rotation angle: ");
scanf("%lf", &angle);
setbkcolor(WHITE);
c = cos(angle *M_PI/180);
s = sin(angle *M_PI/180);
x1 = floor(x1 * c + y1 * s);
y1 = floor(-x1 * s + y1 * c);
x2 = floor(x2 * c + y2 * s);
y2 = floor(-x2 * s + y2 * c);
x3 = floor(x3 * c + y3 * s);
y3 = floor(-x3 * s + y3 * c);
cleardevice();
line(x1, y1 ,x2, y2);
line(x2,y2, x3,y3);
line(x3, y3, x1, y1);
getch();
closegraph();
return 0;
}
OUTPUT 10: