0

I'm using floodfill() and it's not coloring the places I want it to, instead it colors the entire window.

I want the Cyan background inside the rectangle and the magenta under the line line(conx(30) - 2, cony(0)+2, conx(100) + 2, cony(30) - 2); but still within the rectangle boundary.

Here's the code, with relevant libraries included:

#include <iostream>
#include <graphics.h>
#include <cmath>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

using namespace std;

//convert to pixel value (scale of 6)
double conx(double x)
{
    return x * (600/100) + 50;
}

double cony(double y)
{
    return -y * (600/100) + 650;
}

int main()
{
initwindow(700,700);

rectangle(50, 0, 650, 650);
setfillstyle(SOLID_FILL, CYAN);
floodfill(100, 100, CYAN);
setfillstyle(SOLID_FILL, MAGENTA);
floodfill(620, 620, MAGENTA);


settextstyle(DEFAULT_FONT, HORIZ_DIR, 3);
outtextxy(150, 655, "ELASTIC PARTICLE");
setcolor(15);

setcolor(15);
line(0, 0, 700, 0);
line(50, 0, 50, 650);
line(650, 0, 650, 652);
line(50, 650, 652, 650);

//drawing the line for the wedge/incline
line(conx(30) - 2, cony(0)+2, conx(100) + 2, cony(30) - 2);

//borders
setcolor(15);
line(0, 0, 700, 0);
line(50, 0, 50, 650);
line(650, 0, 650, 652);
line(50, 650, 652, 650);
}

Here's the sample of the output

  • 1
    It would help if you describe the library you are attempting to use. There isn't a single line of what you have posted that is C++ itself. Your example says "Windows BGI" -- is that the library you are working with? sdl_bgi? – David C. Rankin Jul 10 '18 at 03:29
  • I'm currently using graphics.h – Fragmentize Jul 10 '18 at 03:33
  • OK, so that is the old Windows graphics.h, (last updated 2004?) e.g, [functions of graphics.h](https://www.programmingsimplified.com/c/graphics.h) and [How I can get and use the header file in my C++](https://stackoverflow.com/questions/7860569/how-i-can-get-and-use-the-header-file-graphics-h-in-my-c-program). – David C. Rankin Jul 10 '18 at 03:37
  • As I read the `floodfill` description, you set the interior of the rectangle to `CYAN` then immediately overwrite the `CYAN` with `MAGENTA` because the rectangle is the only enclosing shape at the time of your calls to `floodfill`. Both `(100,100)` and `(620,620)` lie within the rectangle. – David C. Rankin Jul 10 '18 at 03:50
  • `floodfill` color and `setcolor` should be same, then it'll work. I don't know why it's working, but yeah, it works! – Preetkaran Singh Mar 29 '21 at 12:26

0 Answers0