fork download
  1. #include <stdio.h>
  2.  
  3. #define SZ_Y (10)
  4. #define SZ_X (10)
  5. #define SZ_YX ( SZ_Y * SZ_X )
  6.  
  7. int main( void )
  8. {
  9. int count = 0, height = SZ_Y, width = SZ_X;
  10. double x_g = 0.0, y_g = 0.0;
  11. unsigned char img_src[ SZ_YX ] = { 0 };
  12. char img_tmp[ SZ_YX + 1 ] =
  13. //...0123456789
  14. ".........." // 0
  15. ".........." // 1
  16. ".........." // 2
  17. "...ooooo.." // 3
  18. "...ooooo.." // 4
  19. "...ooxoo.." // 5
  20. "...ooooo.." // 6
  21. "...ooooo.." // 7
  22. ".........." // 8
  23. "..........";// 9
  24.  
  25. for( int i = 0; i < SZ_YX; i++ )
  26. {
  27. if( '.' != img_tmp[ i ] )
  28. {
  29. img_src[ i ] = 255;
  30. }
  31. }
  32.  
  33. for( int y = 0; y < height; y++ )
  34. {
  35. for( int x = 0; x < width; x++ )
  36. {
  37. if( 255 == img_src[ y * width + x ] )
  38. {
  39. count++;
  40. x_g += x;
  41. y_g += y;
  42. }
  43. }
  44. }
  45. x_g /= count;
  46. y_g /= count;
  47.  
  48. printf( "重心(x, y) = (%f, %f)\n", x_g, y_g );
  49. return 0;
  50. }
  51.  
  52.  
  53.  
Success #stdin #stdout 0s 4368KB
stdin
Standard input is empty
stdout
重心(x, y) = (5.000000, 5.000000)