すごくメモ帳

すごくほぼメモ帳ぐらいなブログ

C++で旭日旗を描いてみた

C++で日章旗を描いてみたの続きです。

環境

言語: C++
コンパイラー: g++
依存: OpenCV4

Makefile

CC = g++
SOURCE = flag.cpp
PROGRAM = flag

$(PROGRAM): $(SOURCE)
    $(CC) $(SOURCE) `pkg-config opencv4 --cflags --libs` -o $(PROGRAM)

プログラム

#include <iostream>
#include <opencv2/opencv.hpp>

#define puts(x) cout << x << endl
#define print(x) cout << x << ends
#define PI 3.14159265359

using namespace std;
using namespace cv;

class JapaneseFlag{
    int width;
    int height;
    int r;
    Mat flag_image;

    public:
    void setWidth(int width){
        this -> width = width * 2;
    }

    bool sun(int x, int y){
        return (x - this -> height / 2) * (x - this -> height / 2) \
        + (y - this -> width / 2) * (y - this -> width / 2) \
        < this -> r * this -> r \
        ? true \
        : false;
    }

    bool radiation(int x, int y){
        double deg = pos2deg(this -> height / 2, this -> width / 2, x, y);
        return fmod(deg + 180, 22.5) < 11.25;
    }

    double pos2deg(double x1, double y1, double x2, double y2){
        return atan2(x1 - x2, y1 - y2) * 360 / (2 * PI);
    }

    void draw(){
        this -> height = this -> width * 2 / 3.0;
        this -> r = this -> height * 3 / 10.0;
        Mat flag(this -> height, this -> width, CV_8UC3);
        for(int i = 0; i < height; i++)
            for(int j = 0; j < width; j++)
                if(sun(i, j))
                    flag.at<Vec3b>(i, j) = Vec3b(0, 0, 255);
                else
                    flag.at<Vec3b>(i, j) = Vec3b(255, 255, 255);
        this -> flag_image = flag;
    }

    void drawRisingSun(){
        this -> height = this -> width * 2 / 3.0;
        this -> r = this -> height / 4;
        Mat flag(this -> height, this -> width, CV_8UC3);
        for(int i = 0; i < height; i++)
            for(int j = 0; j < width; j++)
                if(radiation(i, j + this -> height / 6) || sun(i, j + this -> height / 6))
                    flag.at<Vec3b>(i, j) = Vec3b(0, 0, 255);
                else
                    flag.at<Vec3b>(i, j) = Vec3b(255, 255, 255);
        this -> flag_image = flag;
    }

    void save(String save_image){
        resize(this -> flag_image, this -> flag_image, Size(), 0.5, 0.5);
        imwrite(save_image, this -> flag_image);
    }
};


int main(){
    JapaneseFlag jpflag;
    jpflag.setWidth(900);
    jpflag.drawRisingSun();
    jpflag.save("Rising_Sun_Flag.png");

    return 0;
}

結果

f:id:muracchi3286:20200111184018p:plain