#include <graphics.h>
#include <iostream>
#define ROUND(x) ((int)(x+0.5))
typedef int Point[2]; // P[0] - x coordinate P[1] - y coordinate
void seedFill(int x, int y, int color) {
if(x > 640 || x < 0 || y > 480 || y < 0)
return;
if(getpixel(x, y) == BLACK) {
putpixel(x, y, color);
seedFill(x+1, y, color);
seedFill(x-1, y, color);
seedFill(x, y+1, color);
seedFill(x, y-1, color);
}
}
void scanlineFill(Point *polygon, int nodes, int color) {
setcolor(color);
Point array[480];
for(int i=0; i<480; ++i) {
array[i][0] = 640;
array[i][1] = 0;
}
for(int i=0; i<nodes; ++i){
Point p1, p2;
p1[0] = polygon[i][0]; p1[1] = polygon[i][1];
p2[0] = polygon[(i+1)%nodes][0]; p2[1] = polygon[(i+1)%nodes][1];
if(p1[1] > p2[1]) {
p2[0] = polygon[i][0]; p2[1] = polygon[i][1];
p1[0] = polygon[(i+1)%nodes][0]; p1[1] = polygon[(i+1)%nodes][1];
}
double m = (double)(p2[1]-p1[1])/(p2[0]-p1[0]);
double xd = p1[0] - 1/m;
for(int y=p1[1]; y<=p2[1]; ++y) {
xd += 1/m;
int x = ROUND(xd);
if(array[y][0] > x)
array[y][0] = x;
if(array[y][1] < x)
array[y][1] = x;
}
}
for(int i=0; i<480; ++i)
if(array[i][0] < array[i][1])
line(array[i][0]+1, i, array[i][1]-1, i);
}
void drawPolygon(Point *polygon, int nodes) {
setcolor(WHITE);
for(int i=0; i<nodes; ++i){
Point p1, p2;
p1[0] = polygon[i][0]; p1[1] = polygon[i][1];
p2[0] = polygon[(i+1)%nodes][0]; p2[1] = polygon[(i+1)%nodes][1];
line(p1[0], p1[1], p2[0], p2[1]);
}
}
int main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\");
Point polygon[20];
int size = 0;
while(true) {
std::system("cls");
std::cout << "Enter size of polygon : (-1 for exit) : ";
std::cin >> size;
if(size == -1)
return 0;
if(size > 20){
std::cout << "Enter size less than 20" << std::endl;
continue;
}
for(int i=0; i < size; ++i) {
std::cout << "Enter vertex : (x y) : ";
std::cin >> polygon[i][0] >> polygon[i][1];
}
drawPolygon(polygon, size);
FILL:
std::system("cls");
std::cout << "Choose an algorithm to fill." << std::endl;
std::cout << "\t1. seed fill" << std::endl;
std::cout << "\t2. scan line fill" << std::endl;
std::cout << "choice: ";
int choice;
std::cin >> choice;
switch(choice) {
case 1:
{
std::cout << "Enter a point inside polygon: (x y) : ";
Point p;
std::cin >> p[0] >> p[1];
seedFill(p[0], p[1],RED);
break;
}
case 2:
scanlineFill(polygon, size, BLUE);
break;
default:
std::cout << "I can't interpret your choice." << std::endl;
goto FILL;
break;
}
}
}
Friday, December 11, 2009
Area Filling Algorithms (seed filling, ScanLine conversion)
Subscribe to:
Post Comments (Atom)
2 comments:
Hi Rooparam!,
First of all let me congratulate you on maintaining such a elegant but fruitful blog.You have presented data structure, otherwise considered as very bore and complex subject, in quite interesting manner. But please do add some commentary or introduction to topic along with code.
I also share with you the joys of programming (albeit my inclination is more towards web development and fav language is python). Hope you will find my site interesting too (www.dheerajsuthar.com). Its more about open source and some projects I have furnished.
Hoping to see more great postings from you.
Dheeraj Suthar
MCA I Yr.
NIT Warangal.
thanks dheeraj for ur encouragement. as u might have guessed, i m also an engineering student and just learned programming during college days, so i still dont remember to comment my code properly. But i will try to make my code more readable in future posts. thanks again and plz do visit my blog regularly. And the link u have provided for ur site is no longer exist. plz post new link if u changed your site's domain name.
Rooparam Choudhary
BTech from smvdu
MTech from iisc (joining from july 2010).
Post a Comment