Homework Solution: void fill(point start, rgb fillcol, rgb col) {…

    #include <string> #include <iostream> #include <fstream> #include <cmath> using namespace std; // x^2+y^2=r^2 // x^2=r^2-y^2 // x=sqrt(r^2-y^2) - the final formula for `x`, when `y` is known // // This algorithm is applied to circle with center in (0,0). // You just need to move each point (x,y) to (x+cx,y+cy) when handling circle with center in (cx, cy). // // Start at `(cx, cy-r)`, increment `y`, compute next value of `x` and draw at point `(x, y)`. // The algorithm for circle with center in (cx, cy): // 1. Compute next `y` // 2. Compute next `x`, using the formula above with value of `y` // 3. Draw on `(x,y)` // 4. Since there are two values of `x` for each `y`, draw on `(cx-(x-cx), y)=(2*cx-x, y)` // i.e. simply reflect `x` // // It is also good to connect previous point with the current one so the circle would look much smoother. // struct for point, holds x and y coordinates struct point { int x, y; // constructor point(int _x = 0, int _y = 0) : x(_x), y(_y) {} }; // struct for rgb-pixel, holds red, green and blue values struct rgb { int r, g, b; // constructor rgb(int _r = 0, int _g = 0, int _b = 0) : r(_r), g(_g), b(_b) {} bool operator==(rgb other) { return other.r == r && other.g == g && other.b == b; } bool operator!=(rgb other) { return !(*this == other); } }; class PPMImage { public: // constructor, initializes pixmap // arguments: // width - image width // height - image height PPMImage(int width, int height) : w(width), h(height) { // create pointer to pointers pixmap = new rgb*[h]; // initiliaze each pointer for (int i = 0; i < h; ++i) pixmap[i] = new rgb[w]; } // copy constructor, copies data from another PPMImage object // arguments: // other - another image PPMImage(const PPMImage &other) { copy(other); } // assignment operator, clears image and copies data from another one // arguemnts: // other - another image const PPMImage& operator=(const PPMImage &other) { clear(); copy(other); // return reference to current image return *this; } // destructor, clears data ~PPMImage() { clear(); } // fiils image with one colour // arguments: // col - fill colour void fill(rgb col) { // loop and assign put col in each pixel for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { pixmap[i][j] = col; } } } // recursively fills all adjacent to start pixels // of colour col with colour fillcol // // arguments: // start - starting point // fillcol - fill colour // col - colour to be replaced void fill(point start, rgb fillcol, rgb col) { if (fillcol == col) return; int x = start.x, y = start.y; if (x < 0 || x >= w || y < 0 || y >= h) return; if (pixmap[y][x] != col) return; pixmap[y][x] = fillcol; fill(point(x + 1, y), fillcol, col); fill(point(x - 1, y), fillcol, col); fill(point(x, y + 1), fillcol, col); fill(point(x, y - 1), fillcol, col); } // draws circle // arguments: // c - center point // rx - x radius // ry - y radius // col - line colour void draw_circle(point c, int r, rgb col) { // current `x` and `y` int x = 0, y = -r; // previous `x` and `y` int _x = x, _y = y; // compute r^2 only once int rr = r * r; while (_y < r) { // compute next `x` y = _y + 1, x = round(sqrt(rr-y*y)); // move `x` and `y` by center coordinates x += c.x; y += c.y; _x += c.x; _y += c.y; // draw compute `x` pixmap[y][x] = col; // draw reflected `x` pixmap[y][2 * c.x - x] = col; // connect previous points with current ones draw_line(point(_x, _y), point(x, y), col); draw_line(point(2 * c.x - x, y), point(2 * c.x - _x, _y), col); // move previous _x = x - c.x, _y = y - c.y; } } // draws line between two points // e.g. line between (1,1) and (8,4) // // 0 1 2 3 4 5 6 7 8 9 // 0 . . . . . . . . . . // 1 . * * . . . . . . . subline1 // 2 . . . * * . . . . . subline2 // 3 . . . . . * * . . . subline3 // 4 . . . . . . . * * . subline4 // 5 . . . . . . . . . . // 7 . . . . . . . . . . // // arguments: // p1 - first point // p2 - second point // col - line color void draw_line(point p1, point p2, rgb col) { // swap points so p1.x if lesser than p2.x if (p1.x > p2.x) { point temp = p1; p1 = p2; p2 = temp; } // loop increment value int dy = p1.y < p2.y ? 1 : -1, // length of the each subline len = (p2.x - p1.x + 1) / (abs(p1.y - p2.y) + 1); // draw each subline while (p1.y != p2.y) { for (int i = 0; i < len; ++i) { pixmap[p1.y][p1.x++] = col; } p1.y += dy; } // fill remaining pixels while (p1.x < p2.x) { pixmap[p2.y][p1.x++] = col; } } // prints image to file bool print_to_file(const string &filename) const { ofstream out(filename.c_str()); // couldn't open file if (!out) return false; out << "P3" << endl; out << w << " " << h << endl; out << 255 << endl; // fill each pixel for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { rgb &col = pixmap[i][j]; out << col.r << " " << col.g << " " << col.b << endl; } } return true; } private: // image width and height int w, h; // image pixels rgb **pixmap; // clears image content void clear() { for (int i = 0; i < h; ++i) delete[] pixmap[i]; w = 0, h = 0; delete[] pixmap; } // copies data from another image void copy(const PPMImage &other) { w = other.w, h = other.h; pixmap = new rgb*[h]; for (int i = 0; i < h; ++i) { pixmap[i] = new rgb[w]; for (int j = 0; j < w; ++j) pixmap[i][j] = other.pixmap[i][j]; } } }; // some constants const int WIDTH = 320, HEIGHT = 240, R = 100; // circle center point const point CENTER = point(160, 120); // colors for background and line const rgb LIGHT_GRAY = rgb(245, 245, 245), BLUE = rgb(0, 0, 255); int main() { // create image PPMImage img(WIDTH, HEIGHT); // fill background img.fill(LIGHT_GRAY); // draw a black circle img.draw_circle(CENTER, R, BLUE); img.fill(point(160, 140), BLUE, LIGHT_GRAY); // print image img.print_to_file("img.ppm"); return 0; } *******************I run code blocks - immediately crashes any ideas c++ code blocks

    Expert Answer

     
    Your program is crashing because of stack overflow in function fill

    #include <string>
    #include <iostream>
    #include <fstream>
    #include <cmath>

    using namespace std;

    // x^2+y^2=r^2
    // x^2=r^2-y^2
    // x=sqrt(r^2-y^2) – the ultimate cemula ce `x`, when `y` is known
    //
    // This algorithm is applied to foe with disposition in (0,0).
    // You exact insufficiency to actuate each purpose (x,y) to (x+cx,y+cy) when handling foe with disposition in (cx, cy).
    //
    // Begin at `(cx, cy-r)`, increment `y`, estimate instant esteem of `x` and delineate at purpose `(x, y)`.
    // The algorithm ce foe with disposition in (cx, cy):
    // 1. Estimate instant `y`
    // 2. Estimate instant `x`, using the cemula aloft with esteem of `y`
    // 3. Delineate on `(x,y)`
    // 4. Since there are couple esteems of `x` ce each `y`, delineate on `(cx-(x-cx), y)=(2*cx-x, y)`
    // i.e. merely ponder `x`
    //
    // It is as-well amiable to merge earlier purpose with the escapening single so the foe would behold greatly smoother.

    // struct ce purpose, holds x and y coordinates
    struct purpose {
    int x, y;

    // agent
    point(int _x = 0, int _y = 0)
    : x(_x), y(_y) {}
    };

    // struct ce rgb-pixel, holds flushed, unfinished and blue-colored-colored esteems
    struct rgb {
    int r, g, b;

    // agent
    rgb(int _r = 0, int _g = 0, int _b = 0)
    : r(_r), g(_g), b(_b) {}

    bool operator==(rgb other) {
    revert other.r == r && other.g == g && other.b == b;
    }

    bool operator!=(rgb other) {
    revert !(*this == other);
    }
    };

    class PPMPicture {
    public:

    // agent, initializes pixmap
    // arguments:
    // width – picture width
    // culmination – picture culmination
    PPMImage(int width, int culmination) : w(width), h(height) {
    // invent purposeer to purposeers
    pixmap = novel rgb*[h];
    // initiliaze each purposeer
    ce (int i = 0; i < h; ++i)
    pixmap[i] = novel rgb[w];
    }

    // delineation agent, copies postulates from another PPMPicture object
    // arguments:
    // other – another picture
    PPMImage(const PPMPicture &other) {
    copy(other);
    }

    // enactment operator, obviouss picture and copies postulates from another single
    // arguemnts:
    // other – another picture
    const PPMImage& operator=(const PPMPicture &other) {
    clear();
    copy(other);

    // revert intimation to escapening picture
    revert *this;
    }

    // destructor, obviouss postulates
    ~PPMImage() {
    clear();
    }

    // fiils picture with single colour
    // arguments:
    // col – store colour
    void store(rgb col) {
    // loop and purpose-extinguished spread col in each pixel
    ce (int i = 0; i < h; ++i) {
    ce (int j = 0; j < w; ++j) {
    pixmap[i][j] = col;
    }
    }
    }

    // recursively stores full close to begin pixels
    // of colour col with colour storecol
    //
    // arguments:
    // begin – begining purpose
    // storecol – store colour
    // col – colour to be replaced
    void store(purpose begin, rgb storecol, rgb col) {
    if (fillcol == col)
    return;

    int x = begin.x, y = begin.y;
    if (x < 0 || x >= w || y < 0 || y >= h)
    return;
    if (pixmap[y][x] != col)
    return;

    pixmap[y][x] = storecol;
    fill(point(x + 1, y), storecol, col);
    fill(point(x – 1, y), storecol, col);
    fill(point(x, y + 1), storecol, col);
    fill(point(x, y – 1), storecol, col);
    }

    // delineates foe
    // arguments:
    // c – disposition purpose
    // rx – x radius
    // ry – y radius
    // col – succession colour
    void delineate_circle(purpose c, int r, rgb col) {
    // escapening `x` and `y`
    int x = 0, y = -r;
    // earlier `x` and `y`
    int _x = x, _y = y;
    // estimate r^2 merely once
    int rr = r * r;

    while (_y < r) {
    // estimate instant `x`
    y = _y + 1, x = complete(sqrt(rr-y*y));

    // actuate `x` and `y` by disposition coordinates
    x += c.x;
    y += c.y;
    _x += c.x;
    _y += c.y;

    // delineate estimate `x`
    pixmap[y][x] = col;

    // delineate pondeflushed `x`
    pixmap[y][2 * c.x – x] = col;

    // merge earlier purposes with escapening singles
    draw_line(point(_x, _y), purpose(x, y), col);
    draw_line(point(2 * c.x – x, y), purpose(2 * c.x – _x, _y), col);

    // actuate earlier
    _x = x – c.x, _y = y – c.y;
    }
    }

    // delineates succession among couple purposes
    // e.g. succession among (1,1) and (8,4)
    //
    // 0 1 2 3 4 5 6 7 8 9
    // 0 . . . . . . . . . .
    // 1 . * * . . . . . . . subline1
    // 2 . . . * * . . . . . subline2
    // 3 . . . . . * * . . . subline3
    // 4 . . . . . . . * * . subline4
    // 5 . . . . . . . . . .
    // 7 . . . . . . . . . .
    //
    // arguments:
    // p1 – pristine purpose
    // p2 – relieve purpose
    // col – succession color
    void delineate_line(purpose p1, purpose p2, rgb col) {
    // swap purposes so p1.x if lesser than p2.x
    if (p1.x > p2.x) {
    purpose temp = p1;
    p1 = p2;
    p2 = temp;
    }

    // loop increment esteem
    int dy = p1.y < p2.y ? 1 : -1,
    // prolixity of the each subline
    len = (p2.x – p1.x + 1) / (abs(p1.y – p2.y) + 1);

    // delineate each subline
    while (p1.y != p2.y) {
    ce (int i = 0; i < len; ++i) {
    pixmap[p1.y][p1.x++] = col;
    }

    p1.y += dy;
    }

    // store cherishing pixels
    while (p1.x < p2.x) {
    pixmap[p2.y][p1.x++] = col;
    }
    }

    // imprints picture to perfect
    bool imprint_to_file(const string &filename) const {
    ofstream extinguished(filename.c_str());
    // couldn’t notorious perfect
    if (!out)
    revert false;

    extinguished << “P3″ << endl;
    extinguished << w << ” ” << h << endl;
    extinguished << 255 << endl;

    // store each pixel
    ce (int i = 0; i < h; ++i) {
    ce (int j = 0; j < w; ++j) {
    rgb &col = pixmap[i][j];
    extinguished << col.r << ” ” << col.g << ” ” << col.b << endl;
    }
    }

    revert true;
    }

    private:
    // picture width and culmination
    int w, h;
    // picture pixels
    rgb **pixmap;

    // obviouss picture content
    void obvious() {
    ce (int i = 0; i < h; ++i)
    delete[] pixmap[i];

    w = 0, h = 0;
    delete[] pixmap;
    }

    // copies postulates from another picture
    void delineation(const PPMPicture &other) {
    w = other.w, h = other.h;
    pixmap = novel rgb*[h];
    ce (int i = 0; i < h; ++i) {
    pixmap[i] = novel rgb[w];
    ce (int j = 0; j < w; ++j)
    pixmap[i][j] = other.pixmap[i][j];

    }
    }
    };

    // some constants
    const int WIDTH = 320,
    HEIGHT = 240,
    R = 100;

    // foe disposition purpose
    const purpose CENTER = purpose(160, 120);
    // colors ce backgcomplete and succession
    const rgb LIGHT_GRAY = rgb(245, 245, 245),
    BLUE = rgb(0, 0, 255);

    int main() {
    // invent picture
    PPMPicture img(WIDTH, HEIGHT);
    // store contrast
    img.fill(LIGHT_GRAY);
    // delineate a sombre foe
    img.draw_circle(CENTER, R, BLUE);

    img.fill(point(160, 140), BLUE, LIGHT_GRAY);

    // imimprint picture
    img.print_to_file(“img.ppm”);

    revert 0;
    }

    *******************I escape rule blocks – instantly crashes any ideas c++ rule blocks

    Expert Rejoinder

     

    Your program is crashing consequently of stack exuberance in employment store as there are colossal estimate of recursive allure.

    I ran this program in Visual Studio and got aftercited hallucination :

    ‘Practice.exe’ (Win32): Loaded ‘C:UsersROHITDocumentsVisual Studio 2013ProjectsPracticeDebugPractice.exe’. Symbols loaded.
    ‘Practice.exe’ (Win32): Loaded ‘C:WindowsSysWOW64ntdll.dll’. Cannot invent or notorious the PDB perfect.
    ‘Practice.exe’ (Win32): Loaded ‘C:WindowsSysWOW64kernel32.dll’. Cannot invent or notorious the PDB perfect.
    ‘Practice.exe’ (Win32): Loaded ‘C:WindowsSysWOW64KernelBase.dll’. Cannot invent or notorious the PDB perfect.
    ‘Practice.exe’ (Win32): Loaded ‘C:Program PerfectsAVAST SoftwareAvastaswhookx.dll’. Cannot invent or notorious the PDB perfect.
    ‘Practice.exe’ (Win32): Loaded ‘C:WindowsSysWOW64msvcr120d.dll’. Cannot invent or notorious the PDB perfect.
    ‘Practice.exe’ (Win32): Loaded ‘C:WindowsSysWOW64msvcp120d.dll’. Cannot invent or notorious the PDB perfect.
    First-chance separation at 0x002C8599 in Practice.exe: 0xC00000FD: Stack exuberance (parameters: 0x00000001, 0x00CF2F8C).
    Unhandled separation at 0x002C8599 in Practice.exe: 0xC00000FD: Stack exuberance (parameters: 0x00000001, 0x00CF2F8C).

    In visual studio, go to properties of your design, Then go to method individuality lower linker liberty. Here, when you fixed stack secretiveness bulk and stack consign bulk to 100 MB. Your program escapes uprightly. By failure this liberty is fixed to 1MB.

    See beneath picture to shift stack bulk

    In RuleBlock, exact click on your design and prime uplift liberty, you earn entertain to gather -Wl,–stack=104857600 in other linker liberty lower linker fixedtings. See secure picture.

    Then reuild your resolution, you earn gain uplift logs common to beneath logs:,

    ————– Clean: Debug in Test (compiler: GNU GCC Compiler)—————

    Cleaned “Test – Debug”

    ————– Uplift: Debug in Test (compiler: GNU GCC Compiler)—————

    mingw32-g++.exe -Wfull -fexceptions -g -c F:WorkspaceCTestmain.cpp -o objDebugmain.o
    mingw32-g++.exe -o binDebugTest.exe objDebugmain.o -Wl,–stack=104857600
    Outspread perfect is binDebugTest.exe with bulk 103.68 KB