在位图中操纵位于中心圆外部的区域,使用C语言 – 建议?

huangapple go评论66阅读模式
英文:

Manipulating the area outside a centered circle in a bitmap, in C - suggestions?

问题

I have a 8bit-bitmap 1440:1080 image, with a centered circle on it. Now, I want to manipulate everything that's outside that circle. The image is pointed by the pointer *img.

I was thinking of doing it with distances, like this pseudo-code below:

unsigned char* img;

for (l = 0; l < width; l++) {
    for (j = 0; j < height; j++) {
        if(sqrt(pow(x2-x1,2) + pow(y2-y1,2)) > radius ){
            *(img + j * width + l) = 0;
        }
    }
}

Like you probably noticed, my problem is how to relate x1,x2,y1,y2 to j and l.

I was thinking of making a function like this:

typedef struct {
    int x;
    int y;
} coord;

coord getCoords(int index, int width){
    coord output;
    output.y=0;
    while(index>=width){
        index=index-width;
        output.y+=1;
    }
    output.x=index;
    return output;
}

And the input index would be j * width + l for the pixel I'm trying to know if it's inside the circle or not. The center pixel is [720,540] or if I make it a float to be more precise [720.5, 540.5].

Am I wrong? Or is there a better way to do this?

PS: it's just simpler to create to local variables for coordinates inside the function, and do the math (the distance calculation) inside the function and return a bool. Instead of creating a new type of variable.

英文:

I have a 8bit-bitmap 1440:1080 image, with a centered circle on it. Now, I want to manipulate everything that's outside that circle. The image is pointed by the pointer *img.

I was thinking of doing it with distances, like this pseudo-code below:

unsigned char* img;

for (l = 0; l &lt; width; l++) {
    for (j = 0; j &lt; height; j++) {
        if(sqrt(pow(x2-x1,2) + pow(y2-y1,2)) &gt; radius ){
            *(img + j * width + l) = 0;
        }
    }
}

Like you probably noticed, my problem is how to relate x1,x2,y1,y2 to j and l.

I was thinking of making a function like this:

typedef struct {
    int x;
    int y;
} coord;

coord getCoords(int index, int width){
    coord output;
    output.y=0;
    while(index&gt;=width){
        index=index-width;
        output.y+=1;
    }
    output.x=index;
    return output;
}

And the input index would be j * width + l for the pixel I'm trying to know if it's inside the circle or not. The center pixel is [720,540] or if I make it a float to be more precise [720.5, 540.5].

Am I wrong? Or is there a better way to do this?

PS: it's just simpler to create to local variables for coordinates inside the function, and do the math (the distance calculation) inside the function and return a bool. Instead of creating a new type of variable.

答案1

得分: 1

使用以下代码部分:

pow(l-xc, 2) + pow(j-yc, 2) > pow(radius, 2)
英文:

Use

pow(l-xc, 2) + pow(j-yc, 2) &gt; pow(radius, 2)

huangapple
  • 本文由 发表于 2023年5月29日 15:17:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定