-1

With given x and y coordinates for the centre of the circle, as well as the radius, i managed to make it using this formula: (x−h)^2+(y−k)^2=r^2

And the code does work for some values, for example x = 15, y = 15 and radius = 6, it does create a correct circle, but if i make the radius bigger, once it gets close to the value of x or y, the last 2 "parts" of the circle do not work and the first part draws a weird shape.

void cerc(int x, int y, int raza)
{
    // first part
    for (int i = y; i < 50; i++) {
        for (int j = x; j < 50; j++) {
            if ((pow(j - x, 2)) + (pow(i - y, 2)) <= pow(raza, 2) && matriceHarta[i][j] == 0) {
                matriceHarta[i][j] = -1;
            }
        }
    }
    // second part
    for (int i = y - raza; i <= y; i++) {
        for (int j = x - raza; j <= x; j++) {
            if ((pow(j - x, 2)) + (pow(i - y, 2)) <= pow(raza, 2) && matriceHarta[i][j] == 0) {
                matriceHarta[i][j] = -1;
            }
        }
    }
    // third part
    for (int i = y; i <= raza + y; i++) {
        for (int j = x; j >= raza; j--) {
            if ((pow(j - x, 2)) + (pow(i - y, 2)) <= pow(raza, 2) && matriceHarta[i][j] == 0) {
                matriceHarta[i][j] = -1;
            }
        }
    }
    // fourth part
    for (int i = raza; i <= y; i++) {
        for (int j = x; j <= x + raza; j++) {
            if ((pow(j - x, 2)) + (pow(i - y, 2)) <= pow(raza, 2) && matriceHarta[i][j] == 0) {
                matriceHarta[i][j] = -1;
            }
        }
    }

}

*cerc means 'circle', raza means 'radius' and matriceHarta means 'matrixMap'

This is the shape it creates when i use x = 15, y = 15, radius = 10; enter image description here

,this is the shape it creates when i input x = 15. y = 15 and radius = 6 enter image description here

And this is the shape for x = 15, y= 15 and radius = 15enter image description here:

Spektre
  • 45,598
  • 10
  • 100
  • 347
MickeyMoise
  • 215
  • 1
  • 11
  • 3
    You don't need 4 separate loops. You can just use 1 loop, `j` from `y-r` to `y+r`, and `i` from `x-r` to `x+r`. You also need some bounds check. – Aziz May 27 '22 at 20:41
  • Perhaps [An efficient circle drawing algorithm](https://weber.itn.liu.se/~stegu/circle/circlealgorithm.pdf) could be a start – Ted Lyngmo May 27 '22 at 20:42
  • Your loops are wrong for example `for (int i = y; i < 50; i++)` should be `for (int i = y; i <= y+raza; i++)` but as mentioned before its enough to have just 2 loops doing whole circle/disc at once... see duplicate [Is there a more efficient way of texturing a circle?](https://stackoverflow.com/a/61097673/2521214) also you using pow which is overkill not to mention you power the radius each iteration while it is constant and mixing `float` and `int` computations causing slow conversions making your performance dropping fast ... – Spektre May 28 '22 at 06:52
  • Does this answer your question? [Is there a more efficient way of texturing a circle?](https://stackoverflow.com/questions/60968157/is-there-a-more-efficient-way-of-texturing-a-circle) – Spektre May 28 '22 at 06:52
  • I slightly retag your question btw what to heck there is no more `[circle]` tag? what to use instead ? – Spektre May 28 '22 at 06:57

0 Answers0