Dragon Curve

À̹øÀÇ ÇÁ·¢Å»ÀÇ À̸§Àº Dragon CurveÀÌ´Ù. ´ëÃæ ¿ë ºñ½ÁÇϴٴµ¥ º°·Î ´àÀº °Ç ¾ø´Â °Í °°Àºµ¥...
ÇÏ¿©°£ À̹øµµ ¿ª½Ã Koch ³ª C-Curve ¿Í °ÅÀÇ ºñ½ÁÇÑ Äڵ忡¼ ³ª¿Â ±×¸²ÀÌ´Ù. ´Ù¸¥ Á¡ÀÌ ÀÖ´Ù¸é Dragon Curve ´Â ¾ç¹æÇâ Àç±Í¼ºÀ̶ó´Â Á¡ÀÌ´Ù. DragonUp() ÇÔ¼ö ³»¿¡¼ DragonDown() ÇÔ¼ö¸¦ ºÎ¸£°í, DragonDown() ÇÔ¼ö³»¿¡¼ DragonUp() ÇÔ¼ö¸¦ ºÎ¸£°í, DragonUp() µµ Àç±ÍÀûÀ¸·Î µ¹°í, DragonDown()µµ Àç±ÍÀûÀ¸·Î µ·´Ù.
À§ÀÇ ±×¸²Àº n = 15 ÀÏ ¶§ÀÇ ±×¸²ÀÌ´Ù.
1. SDI ·Î ÇÁ·ÎÁ§Æ®¸¦ ¸¸µç´Ù. ¿©±â¼ ÇÁ·ÎÁ§Æ® À̸§Àº DragonÀ¸·Î Á¤Çß´Ù.
2. ´ÙÀ½Ã³·³ DragonUp(), Dragondown(), Linerel() ÀÌ·¸°Ô 3°³ÀÇ ÇÔ¼ö¸¦ ÀÛ¼ºÇÑ´Ù. ±»ÀÌ µÎ °³ÀÇ ÇÔ¼ö DragonUp()°ú DragonDown() ÇÔ¼ö¸¦ µû·Î ¸¸µéÁö ¾Ê°í ±×³É ÀÎÀÚ Çϳª¸¸ ´õ ÁÖ¾î ÇϳªÀÇ ÇÔ¼ö·Î ¸¸µé¾îµµ ¹«¹æÇϰڴÙ.
void CDragonView::DragonUp(CDC *pDC, float dir, float len, int n)
{
int
newx, newy;
if(n>0)
{
dir
+= 45;
DragonUp(pDC,
dir, len, n-1);
dir
-= 90;
DragonDown(pDC,
dir, len, n-1);
dir
+= 45;
} else {
newx
= int(len * cos(dir * PI / 180));
newy
= int(-len * sin(dir * PI / 180));
Linerel(pDC,
newx, newy);
}
}
void
CDragonView::DragonDown(CDC *pDC, float dir, float len, int n)
{
int
newx, newy;
if(n>0)
{
dir
-= 45;
DragonUp(pDC,
dir, len, n-1);
dir
+= 90;
DragonDown(pDC,
dir, len, n-1);
dir
-= 45;
} else {
newx
= int(len * cos(dir * PI / 180));
newy
= int(-len * sin(dir * PI / 180));
Linerel(pDC,
newx, newy);
}
}
void
CDragonView::Linerel(CDC *pDC, int dx, int dy)
{
pDC->LineTo(pDC->GetCurrentPosition().x
+ dx,
pDC->GetCurrentPosition().y
+ dy);
}
3. OnDraw() ÇÔ¼ö¸¦ ´ÙÀ½Ã³·³ ÀÛ¼ºÇÑ´Ù.
void CDragonView::OnDraw(CDC* pDC)
{
CDragonDoc*
pDoc = GetDocument();
ASSERT_VALID(pDoc);
//
TODO: add draw code for native data here
int
n = 15; // Â÷¼ö
CRect
rect;
GetClientRect(rect);
// lenÀÇ ÀûÀýÇÑ ±æÀ̸¦
°è»ê
float len = float(rect.Width()*0.5*pow(1/2.0
, n/2.0));
pDC->MoveTo(200,
300);
DragonUp(pDC, 0, len, n);
}
4. ºäÀÇ cpp ÆÄÀÏÀÇ ¾ÕºÎºÐ¿¡ ´ÙÀ½ µÎ ÁÙÀ» Ãß°¡ÇÑ´Ù. ¾Ë´Ù½ÃÇÇ math.h ´Â sin(), cos() ÇÔ¼ö¸¦ À§ÇÑ °ÍÀ̰í, Visual C++ ¿¡¼´Â PI °ªÀÌ Á¤ÇØÁ® ÀÖÁö ¾Ê±â ¶§¹®¿¡ »ç¿ëÀÚ°¡ ÇÊ¿ä½Ã Á¤ÇØÁÖ¾î¾ß ÇÑ´Ù.
#include <math.h>
#define PI 3.141592
- the end of this article -