unko 描画 アルゴリズム


unko 描画 アルゴリズム


```algo.cpp

void algo_unko(int x1, int y1, int x2, int y2, void *data, AlgoPixel proc)
{
  int mx, my, rx, ry;
  int x, y;

  int mx2, my2;

  mx = (x1 + x2) / 2;
  mx2 = (x1 + x2 + 1) / 2;
  my = (y1 + y2) / 2;
  my2 = (y1 + y2 + 1) / 2;
  rx = ABS(x1 - x2);
  ry = ABS(y1 - y2);

  if (rx == 1) { algo_line(x2, y1, x2, y2, data, proc); rx--; }
  if (rx == 0) { algo_line(x1, y1, x1, y2, data, proc); return; }

  if (ry == 1) { algo_line(x1, y2, x2, y2, data, proc); ry--; }
  if (ry == 0) { algo_line(x1, y1, x2, y1, data, proc); return; }

  rx /= 2;
  ry /= 2;

  proc(mx, my - ry, data);  // center top
  
  int unkoy = ABS(y1 - y2) / 3;

  int unko_hedgh = unkoy * 0.3;
  if ( ry > rx ) {
    //unko_hedgh = unkoy * 0.4;
  }

  int j = 1;
  int k = 0;
  for ( int i = my - ry + 1; i <= (my - ry)+ unkoy; i++)
  {
    proc(mx+j, i, data);
    proc(mx-j, i, data);

    if ( i > ((my - ry)+unkoy)-unko_hedgh*2){

      int r = unko_hedgh;
      int y = (((my - ry)+unkoy)-unko_hedgh) - i;
      int x = (int)round(sqrt((double)(r*r-y*y)));
      j = x+k;

    } else {
      j++;
      if ( i+1 > ((my - ry)+unkoy)-unko_hedgh*2){
        k = j;
      }

    }
    
  }

  unko_hedgh = unkoy * 0.4;

  for ( int i = (my - ry)+ unkoy + 1 ; i <= (my - ry)+ unkoy*2; i++)
  {
    proc(mx+j, i, data);
    proc(mx-j, i, data);

    if ( i > ((my - ry)+unkoy*2)-unko_hedgh*2){

      int r = unko_hedgh;
      int y = (((my - ry)+unkoy*2)-unko_hedgh) - i;
      int x = (int)round(sqrt((double)(r*r-y*y)));
      j = x+k;

    } else {
      j++;
      if ( i+1 > ((my - ry)+unkoy*2)-unko_hedgh*2){
        k = j;
      }

    }
    
  }

  unko_hedgh = unkoy * 0.5;
  for ( int i = (my - ry)+ unkoy*2 + 1 ; i <= (my - ry)+ unkoy*3; i++)
  {
    proc(mx+j, i, data);
    proc(mx-j, i, data);
    
    if ( i > ((my - ry)+unkoy*3)-unko_hedgh*2){

      int r = unko_hedgh;
      int y = (((my - ry)+unkoy*3)-unko_hedgh) - i;
      int x = (int)round(sqrt((double)(r*r-y*y)));
      j = x+k;

    } else {
      j++;
      if ( i+1 > ((my - ry)+unkoy*3)-unko_hedgh*2){
        k = j;
      }

    }

  }


  for ( int i = mx-j; i <= (mx+j); i++ )
  {
    proc(i, (my - ry)+unkoy*3, data);
  }

}

void algo_unkofill(int x1, int y1, int x2, int y2, void *data, AlgoHLine proc)
{
  int mx, my, rx, ry;
  int x, y;

  int mx2, my2;

  mx = (x1 + x2) / 2;
  mx2 = (x1 + x2 + 1) / 2;
  my = (y1 + y2) / 2;
  my2 = (y1 + y2 + 1) / 2;
  rx = ABS (x1 - x2);
  ry = ABS (y1 - y2);

  if (rx == 1) { int c; for (c=y1; c<=y2; c++) proc(x2, c, x2, data); rx--; }
  if (rx == 0) { int c; for (c=y1; c<=y2; c++) proc(x1, c, x1, data); return; }

  if (ry == 1) { proc(x1, y2, x2, data); ry--; }
  if (ry == 0) { proc(x1, y1, x2, data); return; }

  rx /= 2;
  ry /= 2;

  proc(mx, my - ry, mx2, data);

  int unkoy = ABS(y1 - y2) / 3;

  int unko_hedgh = unkoy * 0.3;

  int j = 1;
  int k = 0;
  for ( int i = my - ry + 1; i <= (my - ry) + unkoy; i++)
  {
    proc(mx+j, i, mx-j, data);

    if ( i > ((my - ry)+unkoy)-unko_hedgh*2){

      int r = unko_hedgh;
      int y = (((my - ry)+unkoy)-unko_hedgh) - i;
      int x = (int)round(sqrt((double)(r*r-y*y)));
      j = x+k;

    } else {
      j++;
      if ( i+1 > ((my - ry)+unkoy)-unko_hedgh*2){
        k = j;
      }

    }
    
  }

  unko_hedgh = unkoy * 0.4;
  for ( int i = (my - ry)+ unkoy + 1; i <= (my - ry)+ unkoy*2; i++)
  {
    proc(mx+j, i, mx-j,data);
    
    if ( i > ((my - ry)+unkoy*2)-unko_hedgh*2){

      int r = unko_hedgh;
      int y = (((my - ry)+unkoy*2)-unko_hedgh) - i;
      int x = (int)round(sqrt((double)(r*r-y*y)));
      j = x+k;

    } else {
      j++;
      if ( i+1 > ((my - ry)+unkoy*2)-unko_hedgh*2){
        k = j;
      }

    }

  }

  unko_hedgh = unkoy * 0.5;
  for ( int i = (my - ry)+ unkoy*2 + 1 ; i <= (my - ry)+ unkoy*3; i++)
  {
    proc(mx+j, i, mx-j,data);
    if ( i > ((my - ry)+unkoy*3)-unko_hedgh*2){

      int r = unko_hedgh;
      int y = (((my - ry)+unkoy*3)-unko_hedgh) - i;
      int x = (int)round(sqrt((double)(r*r-y*y)));
      j = x+k;

    } else {
      j++;
      if ( i+1 > ((my - ry)+unkoy*3)-unko_hedgh*2){
        k = j;
      }

    }
  }

}

```