0

I am trying to fill a circle with this algorithm. fill_circle I would like to save the output (x,y) coordinates. Can anyone help me to save the vector of struct? Thank you!

struct point
{
int x = 0;
int y = 0;
};

void fill_circle(point const& center, int radius, std::vector<point>& points)
{
    int sqr_rad = radius * radius;

    for (int px = center.x - radius; px <= center.x + radius; px++)
    {
        for (int py = center.y - radius; py <= center.y + radius; py++)
        {
            int dx = center.x - px, dy = center.y - py;
            if (dx * dx + dy * dy <= sqr_rad)
            points.push_back({px, py});
        }
    }
}

std::string input_v, input_h;   // (v, h) input string
size_t tdata_v, tdata_h;        // (v, h) of selected pixel
struct point Center;

std::vector<point> pixels;
std::cout << "Please enter (v) of pixel: ";
getline (std::cin, input_v);
std::stringstream(input_v) >> tdata_v;
Center.x = static_cast<int>(tdata_v);
std::cout << "Please enter (h) of pixel: ";
getline (std::cin, input_h);
std::stringstream(input_h) >> tdata_h;
Center.y = static_cast<int>(tdata_h);

fill_circle(Center, 50, pixels);

sprintf(circlename, "%scircle_v%04zu_h%04zu.txt", writepath.c_str(), tdata_v, tdata_h); // The path to the file to be written

How can I write all the pixels.x and pixels.y to the circle_v_h.txt in the format?

pixels_x, pixels_y; ...

Thanks!

Rolly Ng
  • 15
  • 4
  • You can [override `operator<<`](https://wandbox.org/permlink/KvFd7bzZJzaDL5Gp) for both `point` and `std::vector` And [a random tutorial I found](https://www.geeksforgeeks.org/overloading-stream-insertion-operators-c/) – felix Nov 12 '22 at 11:20

1 Answers1

0
    fill_circle(Center, 50, pixels);
        
    //std::cout << pixels[0].x << ", " << pixels[0].y << std::endl; 
    sprintf(circlename, "%scircle_v%04zu_h%04zu.txt", writepath.c_str(), tdata_v, tdata_h); // The path to the file to be written

    std::ofstream fout;
    fout.open(circlename);      
    for (l = 0; l < pixels.size(); l++)
    {
        fout << pixels.at(l).x << ", " << pixels.at(l).y << "\n";
    }   
    fout.close();
Rolly Ng
  • 15
  • 4