Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@ohisama@github

vs2010でmfc その5

概要

vs2010でmfcやってみた。
コッホ曲線やってみた。

参考にしたページ

手順

  • mfcアプリケーション、ダイアログベースでプロジェクト名は、mfc0で作る。
  • IDD_MFC0_DIALOGを開く
  • OKボタンをクリック。
  • コードを書く。
  • 実行。

サンプルコード



typedef struct tagFPOINT {
    double x;
    double y;
} FPOINT;
void DrawLine(FPOINT p1, FPOINT p2, CDC * pDC)
{
    pDC->MoveTo((int)(p1.x + 0.5), (int)(p1.y + 0.5));
    pDC->LineTo((int)(p2.x + 0.5), (int)(p2.y + 0.5));
}
FPOINT RotateLine(FPOINT p1, FPOINT p2, double alpha)
{
    double x0 = p1.x;
    double y0 = p1.y;
    double x1 = p2.x - x0;
    double y1 = p2.y - y0;
    FPOINT pntRet;
    pntRet.x = x1 * cos(alpha) - y1 * sin(alpha) + x0;
    pntRet.y = x1 * sin(alpha) + y1 * cos(alpha) + y0;
    return pntRet;
}
FPOINT ShortenLine(FPOINT p1, FPOINT p2, double Ratio)
{
    FPOINT pntRet;
    if (p1.x == p2.x && p1.y == p2.y)
    {
        pntRet = p1;
    }
    if (p1.x == p2.x)
    {
        pntRet.x = p1.x;
        pntRet.y = p1.y + (p2.y - p1.y) * Ratio;
    }
    else if (p1.y == p2.y)
    {
        pntRet.y = p1.y;
        pntRet.x = p1.x + (p2.x - p1.x) * Ratio;
    }
    else
    {
        pntRet.x = p1.x + (p2.x - p1.x) * Ratio;
        pntRet.y = p1.y + (p2.y - p1.y) * Ratio;
    }
    return pntRet;
}
void KochCurve(int n, FPOINT p1, FPOINT p2, CDC * pDC)
{
    if (n == 1)
    {
        DrawLine(p1, p2, pDC);
    }
    else
    {
        FPOINT pc1 = ShortenLine(p1, p2, 1.0 / 3.0);
        FPOINT pc2 = ShortenLine(p1, p2, 2.0 / 3.0);
        FPOINT pc = RotateLine(pc1, pc2, -acos(1.0 / 2.0));
        KochCurve(n - 1, p1, pc1, pDC);
        KochCurve(n - 1, pc1, pc, pDC);
        KochCurve(n - 1, pc, pc2, pDC);
        KochCurve(n - 1, pc2, p2, pDC);
    }
}
void Cmfc0Dlg::OnBnClickedOk()
{
    CClientDC dc(this);
    FPOINT p1 = {
        200, 
        300
    };
    FPOINT p2 = {
        800, 
        300
    };
    KochCurve(6, p1, p2, &dc);
}

以上。

0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?