質問
をしたいのリファクタリング一部のレガシーコードの最近のために機能することができました座標系の問題は、これらの機能のみが異なる直交変数れているというようなものが
void DrawScaleX(HDC dc, int step, int x0, int x1, int y0, int y1)
{
for(int x = x0; x < x1; x += step)
{
MoveToEx(dc, x, y0, NULL);
LineTo(dc, x, y1);
}
}
void DrawScaleY(HDC dc, int step, int x0, int x1, int y0, int y1)
{
for(int y = y0; y < y1; y += step)
{
MoveToEx(dc, x0, y, NULL);
LineTo(dc, x1, y);
}
}
その場合どうしたらよろしいですかあは仮のものように、アンチエイリアスは変わるだけで絵筆であったのと同じコードの両方でコードの重複が悪いかがでしょうか。
私の質問はがきに書き換えこの二つの機能を一つ一つにこの問題を回避するためには?
解決
線引きは、単に当社では豊富な種類のポイントを描画のスケーリングincrementing(x0,y0)及び(x1,y1)特定の方向を、X、および/またはY.ここを突き詰めれば、その規模の場合は、方向(s)ステッピングが発生したものの両方に向けています。
template< int XIncrement, YIncrement >
struct DrawScale
{
void operator()(HDC dc, int step, int x0, int x1, int y0, int y1)
{
const int deltaX = XIncrement*step;
const int deltaY = YIncrement*step;
const int ymax = y1;
const int xmax = x1;
while( x0 < xmax && y0 < ymax )
{
MoveToEx(dc, x0, y0, NULL);
LineTo(dc, x1, y1);
x0 += deltaX;
x1 += deltaX;
y0 += deltaY;
y1 += deltaY;
}
}
};
typedef DrawScale< 1, 0 > DrawScaleX;
typedef DrawScale< 0, 1 > DrawScaleY;
テンプレートのない仕事をした:コンパイル時にコンパイラを削除しますすべての場合は、null諸表につdeltaXはdeltaYは0に関する関数が呼び出されるまで、半分のコードがら各元.
を追加できますの抗エイリアス、鉛筆もこのuniq機能コードを取得します正しく生成され生成されるコンパイラです。
このカットを貼り付けるステロイド;-)
--ppi
他のヒント
なぜあなただけない抽出物体のためのサイクル分機能しているのでしょうか。次のように面白いものを抽出します。
void DrawScaleX(HDC dc, int step, int x0, int x1, int y0, int y1)
{
for(int x = x0; x < x1; x += step)
{
DrawScale(dc, x, y0, x, y1);
}
}
void DrawScaleY(HDC dc, int step, int x0, int x1, int y0, int y1)
{
for(int y = y0; y < y1; y += step)
{
DrawScale(dc, x0, y, x1, y);
}
}
private void DrawScale(HDC dc, int x0, int y0, int x1, int y1)
{
//Add funny stuff here
MoveToEx(dc, x0, y0, NULL);
LineTo(dc, x1, y1);
//Add funny stuff here
}
ここでは私の自社ソリューション
class CoordGenerator
{
public:
CoordGenerator(int _from, int _to, int _step)
:from(_from), to(_to), step(_step), pos(_from){}
virtual POINT GetPoint00() const = 0;
virtual POINT GetPoint01() const = 0;
bool Next()
{
if(pos > step) return false;
pos += step;
}
protected:
int from;
int to;
int step;
int pos;
};
class GenX: public CoordGenerator
{
public:
GenX(int x0, int x1, int step, int _y0, int _y1)
:CoordGenerator(x0, x1, step),y0(_y0), y1(_y1){}
virtual POINT GetPoint00() const
{
const POINT p = {pos, y0};
return p;
}
virtual POINT GetPoint01() const
{
const POINT p = {pos, y1};
return p;
}
private:
int y0;
int y1;
};
class GenY: public CoordGenerator
{
public:
GenY(int y0, int y1, int step, int _x0, int _x1)
:CoordGenerator(y0, y1, step),x0(_x0), x1(_x1){}
virtual POINT GetPoint00() const
{
const POINT p = {x0, pos};
return p;
}
virtual POINT GetPoint01() const
{
const POINT p = {x1, pos};
return p;
}
private:
int x1;
int x0;
};
void DrawScale(HDC dc, CoordGenerator* g)
{
do
{
POINT p = g->GetPoint00();
MoveToEx(dc, p.x, p.y, 0);
p = g->GetPoint01();
LineTo(dc, p.x, p.y);
}while(g->Next());
}
だいしんでいただけるように複雑すぎるような小さな問題で、楽しみにしているものを見るソリューション。
も明らかな"ソリューション"つながる単機能の追加につ追加パラメータのenumのようなタイプ)です。している場合()またはスイッチ()内を適切な行動します。でこんにちは、 機能 の機能が異なるので、だれらの異なる行動 どこかで.
しかし、この追加ランタイムの複雑さの確認も実行時)場所ではできるだけでよく確認めます。
かわからないか、問題追加パラメータは、将来の両方(以ます。では以下のようになります。
- 追加パラメータのすべての機能
- コードのコンパイル、コンパイルに束の場所で渡すことはありません.
- 全ての場所で呼び出すこれらの機能により通過新しいパラメータ。
- します。:)
の場合はC++のコースが機能するテンプレートではなく、を追加するパラメータを追加したテンプレートパラメータ、そして専門のテンプレートを実装いに違います。これはobfuscatingに思います。コードになりにくか、もしくは伸びるのではパラメータはいまだに 正確に 同:
- 追加パラメータ
- コンパイルコードなコンパイルに束の場所
- 全ての場所で呼び出する機能
そんなものが作ったコードにくい。なにふさわしい目標と思う。
思い動き:
MoveToEx(dc, x0, y, NULL);
LineTo(dc, x1, y);
は、自らの機能DrawLine(x0,y0,x0,y0)で呼びからそれぞれの既存の機能
その一つの場所への追加図のでしょうか。
少しテンプレート...:)
void DrawLine(HDC dc, int x0, int y0, int x0, int x1)
{
// anti-aliasing stuff
MoveToEx(dc, x0, y0, NULL);
LineTo(dc, x1, y1);
}
struct DrawBinderX
{
DrawBinderX(int y0, int y1) : y0_(y0), y1_(y1) {}
void operator()(HDC dc, int i)
{
DrawLine(dc, i, y0_, i, y1_);
}
private:
int y0_;
int y1_;
};
struct DrawBinderY
{
DrawBinderX(int x0, int x1) : x0_(x0), x1_(x1) {}
void operator()(HDC dc, int i)
{
DrawLine(dc, x0_, i, x1_, i);
}
private:
int x0_;
int x1_;
};
template< class Drawer >
void DrawScale(Drawer drawer, HDC dc, int from, int to, int step)
{
for (int i = from; i < to; i += step)
{
drawer(dc, i);
}
}
void DrawScaleX(HDC dc, int step, int x0, int x1, int y0, int y1)
{
DrawBindexX drawer(y0, y1);
DrawScale(drawer, dc, x0, x1, step);
}
void DrawScaleY(HDC dc, int step, int x0, int x1, int y0, int y1)
{
DrawBindexY drawer( x0, x1 );
DrawScale(drawer, dc, y0, y1, step);
}