HIDARI日記(右)

そのときどき興味ある技術を中心にだらだら書いてます。内容は個人の見解であり、所属する企業を代表するものではありません。

SYSTEMATIC LOVEなソースコードの写経 #DIVAAC

プレイに集中できなかったので

先日「初音ミク Project DIVA Arcade Future Tone」に追加された「システマティック・ラヴ」(上が本家で下がArcadeのPV).

このPVの後ろに流れてるソースコードがプレイ中にちらちら見えてゲームに集中できなかったから写経してしまいました…これから反省します.

systematic_love.cpp

#include <iostream>
#include <math.h>

const int HEART_SIZE = 20;
const int HALF_SIZE = HEART_SIZE / 2;

bool is_in_love(int x, int y);

int main(void)
{
    std::string message = " SYSTEMATIC LOVE ";
    int message_indent = (HALF_SIZE - (message.length() / 4)) - 1;

    for (int y = 0; y < HEART_SIZE; ++y)
    {
        for (int x = 0; x < HEART_SIZE; ++x)
        {
            std::cout << ((is_in_love(x, y)) ? "vv" : "  ");

            if (y == HALF_SIZE - 1)
            {

                if (x == message_indent)
                {
                    std::cout << message.c_str();
                    x += (message.length() / 2);
                }
            }
        }

        std::cout << '\n';

    }

    return 0;
}

bool is_in_love(int x, int y)
{
    const float width = 2.2f;
    const float height = 3.0f;
    const float HEART_COEFFICIENT = 0.7f;

    float check_x = ((static_cast<float>(x) / static_cast<float>(HEART_SIZE)) - 0.5f) * width;
    float check_y = ((static_cast<float>(HEART_SIZE - y) / static_cast<float>(HEART_SIZE)) - 0.4f) * height;

    float top_y = 0.0f;
    float bottom_y = 0.0f;

    if (check_x >= 0)
    {
        top_y = sqrt(1 - (check_x * check_x)) + (HEART_COEFFICIENT * sqrt(check_x));
        bottom_y = -sqrt(1 - (check_x * check_x)) + (HEART_COEFFICIENT * sqrt(check_x));
    }
    else
    {
        top_y = sqrt(1 - (check_x * check_x)) + (HEART_COEFFICIENT * sqrt(-check_x));
        bottom_y = -sqrt(1 - (check_x * check_x)) + (HEART_COEFFICIENT * sqrt(-check_x));
    }

    if ((bottom_y <= check_y) && (check_y <= top_y))
    {
        return true;
    }
    else
    {
        return false;
    }
}

実行すると

f:id:hidari-yori:20140130231952j:plain

つまり,

f:id:hidari-yori:20140130232507p:plain

非常にわかりやすいですね.

おわりに

結婚の高速化と、使うプログラミング言語との相関性について勝手に考えてみた【連載:村上福之】 - エンジニアtype

プログラム勉強したい人はHello World.の次に書いてみたらいいのでは無いでしょうか(適当