英文:
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 < 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.
答案1
得分: 1
使用以下代码部分:
pow(l-xc, 2) + pow(j-yc, 2) > pow(radius, 2)
英文:
Use
pow(l-xc, 2) + pow(j-yc, 2) > pow(radius, 2)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论