PFUκ椵(hsakai@pfu.co.jp)ꡣcircle ؿĺޤ

եåؿƤΤ礭ʤäƤΤǡ
ΤΤαƤޤɬפϻȤäƤߤƤ

------- 

sin, cos η׻ˤϡȼΥ롼ѤƤΤǡط׻Ѥ
饤֥󥯤ʤƤȤޤ(Ĥޤꡤѥ -lm ɬ̵)
Ȥϡ
draw_circle(100, 100, 300, 300);
ǡ(200, 200) 濴ȤơȾ100αߤޤ

ʲɲʬǤ


 ʲ mgl.h ؤɲâ


/* sin, cos ΥޥŸˤʰ׷׻
 * ٤㤤
 * ٤ degree ǻ
 */
double easy_sin(int degree);
double easy_cos(int degree);

/* ߤ */
void draw_circle(int x1, int y1, int x2, int y2);


 ʲ mgl.c ؤɲâ


/* sin, cos ΥޥŸˤʰ׷׻
 * ٤㤤
 * ٤ degree ǻ
 */
double easy_sin(int degree)
{
  double rad, value, r2, r3, r5, r7;
  int d;

  while (degree < 0) degree += 360;
  degree = degree % 360;

  if (degree ==   0 || degree == 180) return ( 0.0);
  if (degree ==  30 || degree == 150) return ( 0.5);
  if (degree ==  90                 ) return ( 1.0);
  if (degree == 210 || degree == 330) return (-0.5);
  if (degree == 270                 ) return (-1.0);

  /* 090 ϰϤˤ */
  d = degree % 180;
  if (d >= 90) d = 180 - d;

  rad = (double)d * (3.1415926536 / 180.0);
  r2 = rad * rad;
  r3 = rad * r2;
  r5 = r2 * r3;
  r7 = r2 * r5;
  value = rad
    - r3 * (1.0 / (3*2))
    + r5 * (1.0 / (5*4*3*2))
    - r7 * (1.0 / (7*6*5*4*3*2));

  /* ʰ׷׻ʤΤǡ0.01.0 ۤ뤫? */
  if (value <   0) value = 0.0;
  if (value > 1.0) value = 1.0;

  if (degree >= 180) value = -value;

  return (value);
}

double easy_cos(int degree)
{
  return (easy_sin(degree + 90));
}

/* ߤ */
void draw_circle(int x1, int y1, int x2, int y2)
{
  int i, tmp, d;
  int cx, cy, rx, ry;
  double c, s, oc = 0.0, os = 0.0;

  if (x1 > x2) { tmp = x1; x1 = x2; x2 = tmp; }
  if (y1 > y2) { tmp = y1; y1 = y2; y2 = tmp; }

  cx = (x1 + x2) + 1;
  cy = (y1 + y2) + 1;
  rx = (x2 - x1);
  ry = (y2 - y1);

  if      (rx + ry <  20) d = 45;
  else if (rx + ry < 100) d = 15;
  else if (rx + ry < 200) d =  9;
  else                    d =  5;

  for (i = 0; i <= 45; i += d) {
    c = easy_cos(i);
    s = easy_sin(i);
    if (i) {
      draw_line((cx+oc*rx)*0.5,(cy+os*ry)*0.5,(cx+c*rx)*0.5,(cy+s*ry)*0.5);
      draw_line((cx+oc*rx)*0.5,(cy-os*ry)*0.5,(cx+c*rx)*0.5,(cy-s*ry)*0.5);
      draw_line((cx-oc*rx)*0.5,(cy+os*ry)*0.5,(cx-c*rx)*0.5,(cy+s*ry)*0.5);
      draw_line((cx-oc*rx)*0.5,(cy-os*ry)*0.5,(cx-c*rx)*0.5,(cy-s*ry)*0.5);
      draw_line((cx+os*rx)*0.5,(cy+oc*ry)*0.5,(cx+s*rx)*0.5,(cy+c*ry)*0.5);
      draw_line((cx+os*rx)*0.5,(cy-oc*ry)*0.5,(cx+s*rx)*0.5,(cy-c*ry)*0.5);
      draw_line((cx-os*rx)*0.5,(cy+oc*ry)*0.5,(cx-s*rx)*0.5,(cy+c*ry)*0.5);
      draw_line((cx-os*rx)*0.5,(cy-oc*ry)*0.5,(cx-s*rx)*0.5,(cy-c*ry)*0.5);
    }
    oc = c;
    os = s;
  }
}




