BRESEN.H

[目次 | 型・クラス・構造体 | マクロ]

目次

型・クラス・構造体一覧

マクロ一覧


   1|/*************************************************************************
   2|*  1. <<< ブレゼンハム・アルゴリズム (Bresen) >>> 
   3|*【補足】
   4|*・現在、以下の図のような中点処理を行っていません。
   5|*  ↓次の図は無くしてしまいました (^^;
   6|*  
   7|*  下の辺の場合、現在の点より「下」にある前または後ろの点と中点を取ります。
   8|*  ただし、最初の点と最後の点には前または後ろの点が無いことに注意
   9|**************************************************************************/
  10|
  11|#ifndef  __BRESEN_H
  12|#define  __BRESEN_H
  13|
  14|/*----------------------------------------------------------------------
  15|[Module Property]
  16|name = Bresen
  17|title = ブレゼンハム・アルゴリズム(一次関数)
  18|category = 数学
  19|src =
  20|depend =
  21|priority =
  22|accord =
  23|----------------------------------------------------------------------*/
  24|
  25|#ifndef USES_PRIORITY_HEADER
  26|/*[START_OF_PRIORITY_HEADER]*/
  27|
  28|#define  USES_BRESEN
  29|typedef struct _Bresen     Bresen;
  30|typedef struct _Bresen_Ex  Bresen_Ex;
  31|
  32|/*[END_OF_PRIORITY_HEADER]*/
  33|#endif
  34|
  35|
  36| 
  37|/*---------------------------------------------------------------------*/
  38|/*  2. <<< Interface Area ------------------------------------------- >>> */ 
  39|/*---------------------------------------------------------------------*/
  40|
  41|
  42| 
  43|/*************************************************************************
  44|*  3. <<< [Bresen] ブレゼンハム・アルゴリズム >>> 
  45|*【役割】
  46|*・一次関数(x を1つずつ移動して y を計算する)を描画するための
  47|*  線形補間計算を整数演算で高速に行います。
  48|*・y の値は、y の初期値をユーザが持っておき、Bresen_next の返り値に応じて
  49|*  ユーザが持っている y の変数をプラス1します。
  50|*【制限】
  51|*・傾きは0から1の間の小数だけ指定できます。
  52|**************************************************************************/
  53|struct _Bresen {
  54|  int  cup;
  55|  int  plus;
  56|  int  minus;
  57|};
  58|
  59|void  Bresen_init( Bresen*, int dx, int dy );
  60|int   Bresen_next( Bresen* );
  61|int   Bresen_getFixDec( Bresen*, int sft );
  62|/* type  Bresen_getDec( Bresen*, type );*/
  63|
  64|
  65| 
  66|/*************************************************************************
  67|*  4. <<< [Bresen_Ex] 拡張ブレゼンハム・アルゴリズム >>> 
  68|*【役割】
  69|*・一次関数(x を1つずつ移動して y を計算する)を描画するための
  70|*  線形補間計算を整数演算で高速に行います。
  71|*・y の値は、y の初期値をユーザが持っておき、Bresen_Ex_next の返り値に応じて
  72|*  ユーザが持っている y の変数をプラスします。
  73|*・傾きは0以上の有理数を任意に指定できますが、Bresen より演算が多くなります。
  74|**************************************************************************/
  75|struct _Bresen_Ex {
  76|  int  cup;
  77|  int  plus;
  78|  int  minus;
  79|  int  dint;   /* 整数部の増分 */
  80|  int  direc;  /*  */
  81|};
  82|
  83|void  Bresen_Ex_init( Bresen_Ex*, int dx, int dy );
  84|int   Bresen_Ex_next( Bresen_Ex* );
  85|int   Bresen_Ex_getFixDec( Bresen_Ex*, int sft );
  86|/* type  Bresen_Ex_getDec( Bresen_Ex*, type );*/
  87|
  88|
  89| 
  90|/*---------------------------------------------------------------------*/
  91|/*  5. <<< Mapping Area --------------------------------------------- >>> */ 
  92|/*---------------------------------------------------------------------*/
  93|
  94|
  95| 
  96|/*************************************************************************
  97|*  6. <<< [Bresen_init()] 初期化する >>> 
  98|*【引数】
  99|*  ・int  dy,dx;   傾き
 100|*【補足】
 101|*・初期化した直後は、現在位置が端点にある状態です。
 102|*・傾きは0〜1です。ただし、dx>=dy>=0。
 103|**************************************************************************/
 104|#define  Bresen_init( this, dx, dy ) \
 105|  { \
 106|    ASSERT( dy >= 0 ); \
 107|    ASSERT( dx >= dy ); \
 108|    (this)->plus = (dy) << 1;             /* plus = 2dy */ \
 109|    (this)->cup = (this)->plus - (dx);    /* (dy % dx) * 2 */ \
 110|    (this)->minus = (this)->cup - (dx);   /* minus = -2dx */ \
 111|  }
 112|
 113|
 114| 
 115|/*************************************************************************
 116|*  7. <<< [Bresen_next()] X をプラス1して Y の増分を返す >>> 
 117|*【引数】
 118|*  ・int  返り値;   X をプラス1したら Y もプラス1する =1, not=0
 119|*【補足】
 120|*・この関数を呼び出す回数は dx 回であり、Y を参照する回数が dx + 1 回
 121|*  であることに注意。
 122|*【例】
 123|*  y += Bresen_next( this );
 124|**************************************************************************/
 125|#define  Bresen_next( this ) \
 126|  ( ( (this)->cup >= 0 ) ? \
 127|    ( (this)->cup += (this)->minus, 1 ) : \
 128|    ( (this)->cup += (this)->plus, 0 ) )
 129|
 130|
 131| 
 132|/*************************************************************************
 133|*  8. <<< [Bresen_getFixDec()] 現在の Y の小数部を固定小数で返す >>> 
 134|*【引数】
 135|*  ・int  sft;    返す固定小数のシフト数
 136|*  ・int  返り値;  現在の Y の小数部(固定小数)
 137|**************************************************************************/
 138|#define  Bresen_getFixDec( this, sft ) \
 139|   ( ( ((this)->minus - (this)->cup) << sft ) / (this)->minus )
 140|
 141|
 142| 
 143|/*************************************************************************
 144|*  9. <<< [Bresen_getDec()] 現在の Y の小数部を浮動小数で返す >>> 
 145|*【引数】
 146|*  ・type;          返り値の浮動小数の型
 147|*  ・type  返り値;  現在の Y の小数部
 148|**************************************************************************/
 149|#define  Bresen_getDec( this, type ) \
 150|   ( (type)((this)->minus - (this)->cup) / (this)->minus )
 151|
 152|
 153| 
 154|/*************************************************************************
 155|*  10. <<< [Bresen_Ex_init()] 初期化する >>> 
 156|*【引数】
 157|*  ・int  dy,dx;   傾き
 158|*【補足】
 159|*・初期化した直後は、現在位置が端点にある状態です。
 160|*・傾きは任意です。ただし、dx>0。
 161|*  dx <= 0 を指定したい場合は、独自に関数を作ってください。
 162|**************************************************************************/
 163|#define  Bresen_Ex_init( this, dx, dy ) \
 164|  { \
 165|    int  _dy_dec;  /* dy の小数部 */ \
 166|    ASSERT( dx > 0 ); \
 167|    (this)->dint = (dy)/(dx); \
 168|    if ( dy > 0 ) { \
 169|      (this)->direc = +1; \
 170|      _dy_dec = (dy) - (dx) * (this)->dint; \
 171|    }\
 172|    else { \
 173|      (this)->direc = -1; \
 174|      _dy_dec = -(dy) + (dx) * (this)->dint; \
 175|    }\
 176|    Bresen_init( this, dx, _dy_dec ); \
 177|  }
 178|
 179|
 180| 
 181|/*************************************************************************
 182|*  11. <<< [Bresen_Ex_next()] X をプラス1して Y の増分を返す >>> 
 183|*【引数】
 184|*  ・int  返り値;   Y の増分
 185|*【補足】
 186|*・この関数を呼び出す回数は dx 回であり、Y を参照する回数が dx + 1 回
 187|*  であることに注意。
 188|*【例】
 189|*  y += Bresen_Ex_next( this );
 190|**************************************************************************/
 191|#define  Bresen_Ex_next( this ) \
 192|  ( ( (this)->cup >= 0 ) ? \
 193|    ( (this)->cup += (this)->minus, (this)->dint + (this)->direc ) : \
 194|    ( (this)->cup += (this)->plus, (this)->dint ) )
 195|
 196|
 197| 
 198|/*************************************************************************
 199|*  12. <<< [Bresen_Ex_getFixDec()] 現在の Y の小数部を固定小数で返す >>> 
 200|**************************************************************************/
 201|#define  Bresen_Ex_getFixDec   Bresen_getFixDec
 202|
 203|
 204| 
 205|/*************************************************************************
 206|*  13. <<< [Bresen_Ex_getDec()] 現在の Y の小数部を浮動小数で返す >>> 
 207|**************************************************************************/
 208|#define  Bresen_Ex_getDec   Bresen_getDec
 209|
 210|
 211| 
 212|/*************************************************************************
 213|*  14. <<< [Bresen_Ex_print()] デバッグ表示する >>> 
 214|*【補足】
 215|*・ソースで #include <stdio.h> してください。
 216|**************************************************************************/
 217|#define  Bresen_Ex_print( this ) \
 218|  printf( "cup = %d, plus = %d, minus = %d, dint = %d, direc = %d\n", \
 219|          (this)->cup, (this)->plus, (this)->minus, \
 220|          (this)->dint, (this)->direc )
 221|
 222|
 223| 
 224|#endif  /* __BRESEN_H */ 
 225| 
 226|