0% found this document useful (0 votes)
112 views6 pages

QUESTION7

The third document presents a C++ program that translates any polygon by getting new translation coordinates from the user, calculating new vertex positions, and redrawing the translated polygon using provided functions to draw and translate the shape.

Uploaded by

Ravi Verma
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)
112 views6 pages

QUESTION7

The third document presents a C++ program that translates any polygon by getting new translation coordinates from the user, calculating new vertex positions, and redrawing the translated polygon using provided functions to draw and translate the shape.

Uploaded by

Ravi Verma
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/ 6

QUESTION 7: WAP in C/C++ to fill the polygon using flood fill algorithm.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy