/*
 楕円を描く（ぬりつぶす）
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <eggx.h>

/*
 eclipse
 座標 (x, y) を中心に、幅 w, 高さ h の楕円を angle （ラジアン単位）
 だけ回転させて描く
 楕円は非常に頂点数が多い多角形として fillpoly 関数を用いて描く
*/
#define ECLIPSEPOINTS 100 // 100 角形として描く
void eclipse(int win, double x, double y, double w, double h, double angle)
{
  double ptx[ECLIPSEPOINTS+1], pty[ECLIPSEPOINTS+1]; // 頂点座標位置
  double th; // 楕円の各頂点位置を求める際の角
  double xx, yy; // 元の回転させない楕円の各頂点位置
  int i;

  /* 各頂点の座標位置を得る */
  i=0;
  for(th=0.0; th<2.0*M_PI; th+=2.0*M_PI / ECLIPSEPOINTS) {
    xx=cos(th)*w;
    yy=sin(th)*h;
    ptx[i] = x + xx * cos(angle) - yy * sin(angle);
    pty[i] = y + xx * sin(angle) + yy * cos(angle);
    i++;
  }
  /* 描画 */
  fillpoly(win, ptx, pty, ECLIPSEPOINTS, 0);
}


int main()
{
  int win;

  win=gopen(400,400);  
  winname(win, "rotation");

  // 座標軸を描く
  newpen(win, 1); 
  drawline(win, 0.0, 200.0, 400.0, 200.0); 
  drawline(win, 200.0, 0.0, 200.0, 400.0); 
  
  newpen(win, 2);  // 赤色に設定
  eclipse(win, 200.0, 100.0, 100.0, 50.0, 0.0);     // 回転なし
  newpen(win, 3);  // 緑色に設定
  eclipse(win, 200.0, 200.0, 100.0, 50.0, M_PI/6.0); // 1/6 π回転
  newpen(win, 4);  // 青色に設定
  eclipse(win, 200.0, 300.0, 100.0, 50.0, M_PI/3.0); // 1/3 π回転

  ggetch();
  gclose(win); 
  return 0;
}

