すごくメモ帳

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

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

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;
        this -> height = width * 2 / 3.0;
        this -> r = (this -> height) * 3 / 10.0;
    }

    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;
    }

    void draw(){
        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 save(String save_image){
        imwrite(save_image, this -> flag_image);
    }
};


int main(){
    JapaneseFlag jpflag;
    jpflag.setWidth(300);
    jpflag.draw();
    jpflag.save("Japanese_Flag.png");

    return 0;
}

結果

f:id:muracchi3286:20200111014850p:plain