🎲

メモ - raylibで数学の規則性を3Dで可視化する!!

2024/11/17に公開

完成図

綺麗ですね

画像
for (int x = -20; x < 20; x++) {
            for (int z = -20; z < 20; z++) {
                DrawCube((Vector3){ static_cast<float>(x), 0.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, GREEN); // 草
                DrawCubeWires((Vector3){ static_cast<float>(x), 0.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, DARKGRAY);//枠
                DrawCube((Vector3){ static_cast<float>(x), -1.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, GRAY); //石
                DrawCubeWires((Vector3){ static_cast<float>(x), -1.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, DARKGRAY); //枠
                //if((x%3 == 0) && (z%4 == 0) && ((x + z)%6 == 0)){
                if((x*z)%5 == 0){
                    DrawCube((Vector3){ static_cast<float>(x), 7.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, WHITE); //雲
                    DrawCubeWires((Vector3){ static_cast<float>(x), 7.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, DARKGRAY); //枠
                }else{
                    if((x*z)%4 == 0){
                        DrawCube((Vector3){ static_cast<float>(x), 8.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, RED); //雲
                        DrawCubeWires((Vector3){ static_cast<float>(x), 8.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, DARKGRAY); //枠
                    }else{
                        if((x*z)%3 == 0){
                            DrawCube((Vector3){ static_cast<float>(x), 9.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, YELLOW); //雲 //全滅
                            DrawCubeWires((Vector3){ static_cast<float>(x), 9.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, DARKGRAY); //枠
                        }else{
                            DrawCube((Vector3){ static_cast<float>(x), 10.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, GRAY); //雲
                            DrawCubeWires((Vector3){ static_cast<float>(x), 10.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, DARKGRAY); //枠
                        }
                    }
                }
                //DrawCubeWires((Vector3){ static_cast<float>(x), -1.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, DARKGRAY); //雲なので枠はいらない
            }
        }


やってみる!

code
..
├── CMakeLists.txt
├── build/
└── main.cpp
1 directories, 2 files
main.cpp
#include "raylib.h"
#include <cmath>
#include <iostream>


const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const float MOVE_SPEED = 0.2f;
const float ROTATION_SPEED = 0.003f;

Vector3 Normalize(Vector3 v) {
    float length = sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
    if (length != 0) {
        v.x /= length;
        v.y /= length;
        v.z /= length;
    }
    return v;
}

int main() {
    InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Sum 3D World");

    Camera3D camera = { 0 };
    camera.position = (Vector3){ 0.0f, 5.0f, 10.0f };
    camera.target = (Vector3){ 0.0f, 5.0f, 0.0f }; //?
    camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
    camera.fovy = 45.0f;
    camera.projection = CAMERA_PERSPECTIVE;

    SetTargetFPS(60);
    DisableCursor();

    float yaw = 0.0f;
    float pitch = 0.0f;

    while (!WindowShouldClose()) {
        Vector2 mouseDelta = GetMouseDelta();
        yaw += mouseDelta.x * ROTATION_SPEED;
        pitch -= mouseDelta.y * ROTATION_SPEED;

        if (pitch > PI / 2.0f) pitch = PI / 2.0f;
        if (pitch < -PI / 2.0f) pitch = -PI / 2.0f;

        camera.target.x = camera.position.x + cosf(yaw) * cosf(pitch);
        camera.target.y = camera.position.y + sinf(pitch);
        camera.target.z = camera.position.z + sinf(yaw) * cosf(pitch);

        Vector3 forward = { camera.target.x - camera.position.x, 0.0f, camera.target.z - camera.position.z };
        Vector3 right = { -forward.z, 0.0f, forward.x };
        forward = Normalize(forward);
        right = Normalize(right);

        if (IsKeyDown(KEY_W)) {
            camera.position.x += forward.x * MOVE_SPEED;
            camera.position.z += forward.z * MOVE_SPEED;
        }
        if (IsKeyDown(KEY_S)) {
            camera.position.x -= forward.x * MOVE_SPEED;
            camera.position.z -= forward.z * MOVE_SPEED;
        }
        if (IsKeyDown(KEY_A)) {
            camera.position.x -= right.x * MOVE_SPEED;
            camera.position.z -= right.z * MOVE_SPEED;
        }
        if (IsKeyDown(KEY_D)) {
            camera.position.x += right.x * MOVE_SPEED;
            camera.position.z += right.z * MOVE_SPEED;
        }

        if (IsKeyDown(KEY_SPACE)) camera.position.y += MOVE_SPEED;
        if (IsKeyDown(KEY_LEFT_SHIFT)) camera.position.y -= MOVE_SPEED;

        BeginDrawing();
        ClearBackground(SKYBLUE);

        BeginMode3D(camera);

        //Color BROWN = { 165, 42, 42, 255 }; // RGB values for a dark brown
        Color brown = { 165, 42, 42, 255 }; // Define a brown color

        for (int x = -20; x < 20; x++) {
            for (int z = -20; z < 20; z++) {
                DrawCube((Vector3){ static_cast<float>(x), 0.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, GREEN); // 草
                DrawCubeWires((Vector3){ static_cast<float>(x), 0.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, DARKGRAY);//枠
                DrawCube((Vector3){ static_cast<float>(x), -1.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, GRAY); //石
                DrawCubeWires((Vector3){ static_cast<float>(x), -1.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, DARKGRAY); //枠
                //if((x%3 == 0) && (z%4 == 0) && ((x + z)%6 == 0)){
                if((x*z)%5 == 0){
                    DrawCube((Vector3){ static_cast<float>(x), 7.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, WHITE); //雲
                    DrawCubeWires((Vector3){ static_cast<float>(x), 7.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, DARKGRAY); //枠
                }else{
                    if((x*z)%4 == 0){
                        DrawCube((Vector3){ static_cast<float>(x), 8.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, RED); //雲
                        DrawCubeWires((Vector3){ static_cast<float>(x), 8.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, DARKGRAY); //枠
                    }else{
                        if((x*z)%3 == 0){
                            DrawCube((Vector3){ static_cast<float>(x), 9.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, YELLOW); //雲 //全滅
                            DrawCubeWires((Vector3){ static_cast<float>(x), 9.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, DARKGRAY); //枠
                        }else{
                            DrawCube((Vector3){ static_cast<float>(x), 10.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, GRAY); //雲
                            DrawCubeWires((Vector3){ static_cast<float>(x), 10.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, DARKGRAY); //枠
                        }
                    }
                }
                //DrawCubeWires((Vector3){ static_cast<float>(x), -1.0f, static_cast<float>(z) }, 1.0f, 1.0f, 1.0f, DARKGRAY); //雲なので枠はいらない
            }
        }

        EndMode3D();

        DrawText("by nyanchu-okabe Move with WASD, ascend with SPACE, descend with SHIFT", 10, 10, 20, DARKGRAY);

        EndDrawing();
    }

    CloseWindow();

    return 0;
}

Discussion