How to resolve the algorithm Draw a rotating cube step by step in the Ring programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Draw a rotating cube step by step in the Ring programming language

Table of Contents

Problem Statement

Draw a rotating cube. It should be oriented with one vertex pointing straight up, and its opposite vertex on the main diagonal (the one farthest away) straight down. It can be solid or wire-frame, and you can use ASCII art if your language doesn't have graphical capabilities. Perspective is optional.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Draw a rotating cube step by step in the Ring programming language

Source code in the ring programming language

#===================================================================#
# Based on Original Sample from RayLib (https://www.raylib.com/)
# Ported to RingRayLib by Ring Team
#===================================================================#

load "raylib.ring"

screenWidth = 800
screenHeight = 450

InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d picking")

camera = Camera3D(
	10, 10, 10,
	0, 0, 0 ,
	0, 1, 0 ,
	45,
	CAMERA_PERSPECTIVE
)

cubePosition = Vector3( 0, 1, 0 )
cubeSize = Vector3( 2, 2, 2 )

ray = Ray(0,0,0,0,0,0)

collision = false

SetCameraMode(camera, CAMERA_FREE) 

SetTargetFPS(60)

while !WindowShouldClose()

        UpdateCamera(camera)

        if IsMouseButtonPressed(MOUSE_LEFT_BUTTON)
            if !collision
                ray = GetMouseRay(GetMousePosition(), camera)

                collision = CheckCollisionRayBox(ray,
 		BoundingBox( cubePosition.x - cubeSize.x/2, cubePosition.y - cubeSize.y/2, cubePosition.z - cubeSize.z/2,
    		cubePosition.x + cubeSize.x/2, cubePosition.y + cubeSize.y/2, cubePosition.z + cubeSize.z/2 ) )
            else collision = false
 	    ok
	ok

        BeginDrawing()

            ClearBackground(RAYWHITE)

            BeginMode3D(camera)

                if collision
                    DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, RED)
                    DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, MAROON)

                    DrawCubeWires(cubePosition, cubeSize.x + 0.2f, cubeSize.y + 0.2f, cubeSize.z + 0.2f, GREEN)
                else
                    DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, GRAY)
                    DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, DARKGRAY)
                ok

                DrawRay(ray, MAROON)
                DrawGrid(10, 1)

            EndMode3D()

            DrawText("Try selecting the box with mouse!", 240, 10, 20, DARKGRAY)

            if collision  DrawText("BOX SELECTED", (screenWidth - MeasureText("BOX SELECTED", 30)) / 2, screenHeight * 0.1f, 30, GREEN) ok

            DrawFPS(10, 10)

        EndDrawing()
end

CloseWindow()

  

You may also check:How to resolve the algorithm N-queens problem step by step in the Draco programming language
You may also check:How to resolve the algorithm Function definition step by step in the SSEM programming language
You may also check:How to resolve the algorithm Perfect totient numbers step by step in the Cowgol programming language
You may also check:How to resolve the algorithm Web scraping step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Command-line arguments step by step in the Pop11 programming language