VC++6下写的棋盘上叠加棋子(圆形)的:bmp_combine.zip与bmp_combine_circle.zip 。
另两个大概不常用的:bmp_change_backround_out_of_edge.c与bmp_change_backround_out_of_circle.c 。
后来,在处理字迹时,感到可较多用于字体处理(生成等),写了scan_bmp_font_process.c 。
bmp_circle.c或者bmp_combine_circle\bmp_combineView.cpp:
void calculate_circle_alpha(double*** ppp_alpha, double** pp_data,int R)
{
int XDim, YDim;
int m, n;
int a;
int i;
int iDEBUG = 0;
XDim = YDim = 2*R;
a = R;
double* p_Alpha_circle = (double*)malloc((2*R)*(2*R)*sizeof(double));
if(!p_Alpha_circle)
{
printf("cannot malloc memory for p_Alpha_circle.") ;
return;
}
*pp_data = p_Alpha_circle;
double** alpha = (double** )malloc(2*R * sizeof(double*));
for(i = 0; i < 2*R; i++)
alpha[i] = p_Alpha_circle + (2*R - 1 - i)*2*R;
*ppp_alpha = alpha;
//NOTE: be sure to free these two outside of this function!
for(n = 0; n < YDim; n++)
{
for(m = 0; m < XDim; m++)
{
if(m < a && n < a)
{
double right_bottom_x = m + 1.0f;
double right_bottom_y = -n - 1.0f;
double left_top_x = (double)m;
double left_top_y = -n;
double right_top_x = m + 1.0f;
double right_top_y = -n ;
double left_bottom_x = (double)m;
double left_bottom_y = -n-1.0f;
double origin_x = a;
double origin_y = -a;
if( SQUARE(right_bottom_x - origin_x) + SQUARE(right_bottom_y - origin_y) >= SQUARE(R))
{
alpha[n][m] = 0.0f;
if(iDEBUG)printf("row n:%d,m:%d,\n", n,m);
if(iDEBUG)printf("outside.\n\n");
}
else if(SQUARE(left_top_x - origin_x) + SQUARE(left_top_y - origin_y) <= SQUARE(R))
{
alpha[n][m] = 1.0f;
if(iDEBUG)printf("row n:%d,m:%d,\n", n,m);
if(iDEBUG)printf("inside.\n\n");
}
else
{
double point1_x, point1_y, point2_x, point2_y, point3_x, point3_y;
double area, percent;
if( SQUARE(right_top_x - origin_x) + SQUARE(right_top_y - origin_y) > SQUARE(R))
{
//solve point 1: x=m+1
point1_x = m+1.0f;
point1_y = sqrt( SQUARE(R) - SQUARE(point1_x - a) ) - a;
//
if(SQUARE(left_bottom_x - origin_x) + SQUARE(left_bottom_y - origin_y) > SQUARE(R))
{
//solve point 3: y=-n-1
point3_y = -n - 1.0f;
point3_x = -( sqrt(SQUARE(R) - SQUARE(point3_y + a)) ) + a;
area = ( point1_y - (-n-1.0f) ) * ( (m+1.0f) - point3_x ) * 0.5f;
percent = area / 1.0f;
if(iDEBUG)printf("row n:%d,m:%d,\n", n,m);
if(iDEBUG)printf("point1_x:%f,point1_y:%f,\n", point1_x,point1_y);
if(iDEBUG)printf("point3_x:%f,point3_y:%f,\n", point3_x,point3_y);
if(iDEBUG)printf("area:%f,percent:%f,\n\n", area,percent);
}
else
{
//solve point 2: x=m
point2_x = m;
point2_y = sqrt(SQUARE(R) - SQUARE(point2_x - a)) - a;
area = 0.5f * ( ( point1_y - (-n-1.0f) ) + (point2_y - (-n-1.0f)) ) * 1.0f ;
percent = area / 1.0f;
if(iDEBUG)printf("row n:%d,m:%d,\n", n,m);
if(iDEBUG)printf("point1_x:%f,point1_y:%f,\n", point1_x,point1_y);
if(iDEBUG)printf("point2_x:%f,point2_y:%f,\n", point2_x,point2_y);
if(iDEBUG)printf("area:%f,percent:%f,\n\n", area,percent);
}
}
else
{
//solve point 1: y=-n
point1_y = -n;
point1_x = -( sqrt( SQUARE(R) - SQUARE(point1_y + a)) ) + a;
//
if(SQUARE(left_bottom_x - origin_x) + SQUARE(left_bottom_y - origin_y) > SQUARE(R))
{
//solve point 3: y=-n-1
point3_y = -n-1.0f;
point3_x = -( sqrt(SQUARE(R) - SQUARE(point3_y + a)) ) + a;
area = 0.5f * ( (m+1.0f)-point1_x + (m+1.0f)-point3_x ) * 1.0f;
percent = area / 1.0f;
if(iDEBUG)printf("row n:%d,m:%d,\n", n,m);
if(iDEBUG)printf("point1_x:%f,point1_y:%f,\n", point1_x,point1_y);
if(iDEBUG)printf("point3_x:%f,point3_y:%f,\n", point3_x,point3_y);
if(iDEBUG)printf("area:%f,percent:%f,\n\n", area,percent);
}
else
{
//solve point 2: x=m
point2_x = m;
point2_y = sqrt(SQUARE(R) - SQUARE(point2_x - a)) - a;
area = 1.0f - (0.5f * (point1_x - m) * (-n - point2_y) );
percent = area / 1.0f;
if(iDEBUG)printf("row n:%d,m:%d,\n", n,m);
if(iDEBUG)printf("point1_x:%f,point1_y:%f,\n", point1_x,point1_y);
if(iDEBUG)printf("point2_x:%f,point2_y:%f,\n", point2_x,point2_y);
if(iDEBUG)printf("area:%f,percent:%f,\n\n", area,percent);
}
}
alpha[n][m] = percent;
}
}
else if(m >= a && n < a)
{
//pixel[m][n] = pixel[a-1-(m-a)][n]
alpha[n][m] = alpha[n][a-1-(m-a)];
}
else if(m < a && n >= a)
{
//pixel[m][n] = pixel[m][a-1-(n-a)]
alpha[n][m] = alpha[a-1-(n-a)][m];
}
else if(m >=a && n >= a)
{
//pixel[m][n] = pixel[m][a-1-(n-a)] = pixel[a-1-(m-a)][a-1-(n-a)]
alpha[n][m] = alpha[a-1-(n-a)][m];
}
}
}
//printf("verify:%f,%f,%f ?", alpha[16][0], alpha[15][31], alpha[16][31]);
}