math angle rotation
There is no browser build of this example yet.
Is it the same as C?
- Desktop:verified The Zig version renders a final frame that is byte-identical to the C example.
- Browser:verified In headless Chrome, the Zig version gives a final frame byte-identical to the C example built for the web.
//! raylib [shapes] example - math angle rotation
//!
//! Zig-API translation of examples/shapes/shapes_math_angle_rotation.c (raylib 6.2).
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2025 Kris (@krispy-snacc)
const rl = @import("raylib");
/// Predefined angles for fixed lines, with a gradient color green -> orange -> blue -> magenta.
const fixed_lines = [_]struct { angle: i32, color: rl.Color }{
.{ .angle = 0, .color = .green },
.{ .angle = 30, .color = .orange },
.{ .angle = 60, .color = .blue },
.{ .angle = 90, .color = .magenta },
};
pub fn main() void {
// Initialization
const screen_width = 720;
const screen_height = 400;
rl.initWindow(screen_width, screen_height, "raylib [shapes] example - math angle rotation");
defer rl.closeWindow();
rl.setTargetFPS(60);
const center: rl.Vector2 = .{ .x = screen_width / 2.0, .y = screen_height / 2.0 };
const line_length: f32 = 150.0;
var total_angle: f32 = 0.0; // Animated rotation angle
// Main game loop
while (!rl.windowShouldClose()) {
// Update
total_angle += 1.0; // degrees per frame
if (total_angle >= 360.0) total_angle -= 360.0;
// Draw
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(.white);
rl.drawText("Fixed angles + rotating line", 10, 10, 20, .light_gray);
// Draw fixed-angle lines with colorful gradient
for (fixed_lines) |line| {
const rad = @as(f32, @floatFromInt(line.angle)) * rl.deg2rad;
const end: rl.Vector2 = .{
.x = center.x + @cos(rad) * line_length,
.y = center.y + @sin(rad) * line_length,
};
rl.drawLineEx(center, end, 5.0, line.color);
// Draw angle label slightly offset along the line
const text_pos: rl.Vector2 = .{
.x = center.x + @cos(rad) * (line_length + 20),
.y = center.y + @sin(rad) * (line_length + 20),
};
rl.drawText(rl.textFormat("{d}°", .{line.angle}), @intFromFloat(text_pos.x), @intFromFloat(text_pos.y), 20, line.color);
}
// Draw animated rotating line with changing color
const anim_rad = total_angle * rl.deg2rad;
const anim_end: rl.Vector2 = .{
.x = center.x + @cos(anim_rad) * line_length,
.y = center.y + @sin(anim_rad) * line_length,
};
// Cycle through HSV colors for animated line
const anim_col = rl.colorFromHSV(@rem(total_angle, 360.0), 0.8, 0.9);
rl.drawLineEx(center, anim_end, 5.0, anim_col);
}
}/*******************************************************************************************
*
* raylib [shapes] example - math angle rotation
*
* Example complexity rating: [★☆☆☆] 1/4
*
* Example originally created with raylib 6.0, last time updated with raylib 5.6
*
* Example contributed by Kris (@krispy-snacc) and reviewed by Ramon Santamaria (@raysan5)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2025 Kris (@krispy-snacc)
*
********************************************************************************************/
#include "raylib.h"
#include "raymath.h" // for DEG2RAD
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 720;
const int screenHeight = 400;
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - math angle rotation");
SetTargetFPS(60);
Vector2 center = { screenWidth/2.0f, screenHeight/2.0f };
const float lineLength = 150.0f;
// Predefined angles for fixed lines
int angles[] = { 0, 30, 60, 90 };
int numAngles = sizeof(angles)/sizeof(angles[0]);
float totalAngle = 0.0f; // Animated rotation angle
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
totalAngle += 1.0f; // degrees per frame
if (totalAngle >= 360.0f) totalAngle -= 360.0f;
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(WHITE);
DrawText("Fixed angles + rotating line", 10, 10, 20, LIGHTGRAY);
// Draw fixed-angle lines with colorful gradient
for (int i = 0; i < numAngles; i++)
{
float rad = angles[i]*DEG2RAD;
Vector2 end = { center.x + cosf(rad)*lineLength,
center.y + sinf(rad)*lineLength };
// Gradient color from green → cyan → blue → magenta
Color col;
switch(i)
{
case 0: col = GREEN; break;
case 1: col = ORANGE; break;
case 2: col = BLUE; break;
case 3: col = MAGENTA; break;
default: col = WHITE; break;
}
DrawLineEx(center, end, 5.0f, col);
// Draw angle label slightly offset along the line
Vector2 textPos = { center.x + cosf(rad)*(lineLength + 20),
center.y + sinf(rad)*(lineLength + 20) };
DrawText(TextFormat("%d°", angles[i]), (int)textPos.x, (int)textPos.y, 20, col);
}
// Draw animated rotating line with changing color
float animRad = totalAngle*DEG2RAD;
Vector2 animEnd = { center.x + cosf(animRad)*lineLength,
center.y + sinf(animRad)*lineLength };
// Cycle through HSV colors for animated line
Color animCol = ColorFromHSV(fmodf(totalAngle, 360.0f), 0.8f, 0.9f);
DrawLineEx(center, animEnd, 5.0f, animCol);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}From raylib 6.2. The original on GitHub.