'How to Crop From a Basic Putpixel Function

Im currently trying to deal with my hw. But i don't know how to approach this problem. For example here is a putpixel function i wrote that draws a circle:

void cember(){
  int r=50;
  const int width=r;
  const int length=r*1.5;
  for(int y=width;y>= -width;y-=2){
    for(int x=-length;x<=length;x++){
        if ((int) hesapla(x,y)==r)
        putpixel(x+50,y+50,WHITE);
      }
      
  }
  getch();
  

}

Lets say i want to crop a square shaped piece from it with only using graphics.h library. What can i do? I couldnt even write some codes. I wrote a function that's square shaped but couldnt get any further than that.

The reason im using putpixel instead of circle function is im not allowed to. Can you help me?



Solution 1:[1]

  1. The circle function.
void GUI_Circle(int32_t xCentre, int32_t yCentre, int32_t radius, uint32_t colour)
{
    int32_t x = 0, y = radius;
    int32_t d = 3 - (2 * radius);

    if (colour == UINT32_MAX) return;

    while(x <= y)
    {
        //right bottom
        if((xCentre + x) >=0 && (yCentre + y) >= 0 && (xCentre + x) < ScreenXsize && (yCentre + y) < ScreenYsize)
            TFT_DrawPixel(colour, xCentre + x, yCentre + y);
        if((xCentre + y) >=0 && (yCentre + x) >= 0 && (xCentre + y) < ScreenXsize && (yCentre + x) < ScreenYsize)
            TFT_DrawPixel(colour, xCentre + y, yCentre + x);

        //left bottom
        if((xCentre - x) >=0 && (yCentre + y) >= 0 && (xCentre - x) < ScreenXsize && (yCentre + y) < ScreenYsize)
            TFT_DrawPixel(colour, xCentre - x, yCentre + y);
        if((xCentre - y) >=0 && (yCentre + x) >= 0 && (xCentre - y) < ScreenXsize && (yCentre + x) < ScreenYsize)
            TFT_DrawPixel(colour, xCentre - y, yCentre + x);

        //Top right
        if((xCentre + y) >=0 && (yCentre - x) >= 0 && (xCentre + y) < ScreenXsize && (yCentre - x) < ScreenYsize)
            TFT_DrawPixel(colour, xCentre + y, yCentre - x);
        if((xCentre + x) >=0 && (yCentre - y) >= 0 && (xCentre + x) < ScreenXsize && (yCentre - y) < ScreenYsize)
            TFT_DrawPixel(colour, xCentre + x, yCentre - y);

        // Top left
        if((xCentre - y) >=0 && (yCentre - x) >= 0 && (xCentre - y) < ScreenXsize && (yCentre - x) < ScreenYsize)
            TFT_DrawPixel(colour, xCentre - y, yCentre - x);
        if((xCentre - x) >=0 && (yCentre - y) >= 0 && (xCentre - x) < ScreenXsize && (yCentre - y) < ScreenYsize)
            TFT_DrawPixel(colour, xCentre - x, yCentre - y);

        if (d < 0) d += (4 * x) + 6;
        else
        {
            d += (4 * (x - y)) + 10;
            y -= 1;
        }
        x++;
    }
}

Then let modify it to check if we are in the clipping area:

typedef struct
{
    int32_t xStart, xEnd;
    int32_t yStart, yEnd;
}clip_type;

void GUI_Circle(int32_t xCentre, int32_t yCentre, int32_t radius, clip_type *c, uint32_t colour)
{
    int32_t x = 0, y = radius;
    int32_t d = 3 - (2 * radius);
    clip_type sc = *c;

    if (colour == UINT32_MAX) return;

    if(sc.xEnd - sc.xStart <= 0 || sc.yEnd - sc.yStart <= 0) return;
    if(sc.xEnd < 0 || sc.yEnd < 0) return;
    if(sc.xStart >= ScreenXsize || sc.yStart >= ScreenYsize) return;

    if(sc.xStart < 0) sc.xStart = 0;
    if(sc.yStart < 0) sc.yStart = 0;
    if(sc.xEnd >= ScreenXsize) sc.xEnd = ScreenXsize;
    if(sc.yEnd >= ScreenYsize) sc.yEnd = ScreenYsize;


    while(x <= y)
    {
        //right bottom
        if((xCentre + x) >= sc.xStart && (yCentre + y) >= sc.yStart && (xCentre + x) < sc.xEnd && (yCentre + y) < sc.yEnd)
            TFT_DrawPixel(colour, xCentre + x, yCentre + y);
        if((xCentre + y) >= sc.xStart && (yCentre + x) >= sc.yStart && (xCentre + y) < sc.xEnd && (yCentre + x) < sc.yEnd)
            TFT_DrawPixel(colour, xCentre + y, yCentre + x);

        //left bottom
        if((xCentre - x) >= sc.xStart && (yCentre + y) >= sc.yStart && (xCentre - x) < sc.xEnd && (yCentre + y) < sc.yEnd)
            TFT_DrawPixel(colour, xCentre - x, yCentre + y);
        if((xCentre - y) >= sc.xStart && (yCentre + x) >= sc.yStart && (xCentre - y) < sc.xEnd && (yCentre + x) < sc.yEnd)
            TFT_DrawPixel(colour, xCentre - y, yCentre + x);

        //Top right
        if((xCentre + y) >=sc.xStart && (yCentre - x) >= sc.yStart && (xCentre + y) < sc.xEnd && (yCentre - x) < sc.yEnd)
            TFT_DrawPixel(colour, xCentre + y, yCentre - x);
        if((xCentre + x) >=sc.xStart && (yCentre - y) >= sc.yStart && (xCentre + x) < sc.xEnd && (yCentre - y) < sc.yEnd)
            TFT_DrawPixel(colour, xCentre + x, yCentre - y);

        // Top left
        if((xCentre - y) >= sc.xStart && (yCentre - x) >= sc.yStart && (xCentre - y) < sc.xEnd && (yCentre - x) < sc.yEnd)
            TFT_DrawPixel(colour, xCentre - y, yCentre - x);
        if((xCentre - x) >= sc.xStart && (yCentre - y) >= sc.yStart && (xCentre - x) < sc.xEnd && (yCentre - y) < sc.yEnd)
            TFT_DrawPixel(colour, xCentre - x, yCentre - y);

        if (d < 0) d += (4 * x) + 6;
        else
        {
            d += (4 * (x - y)) + 10;
            y -= 1;
        }
        x++;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 0___________