← ziglang.inraylib-zigGitHub

vector angle

★★☆☆by Ramon Santamaria (@raysan5)

There is no browser build of this example yet.

Is it the same as C?

//! raylib [shapes] example - vector angle
//!
//! Zig-API translation of examples/shapes/shapes_vector_angle.c (raylib 6.2).
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2023-2025 Ramon Santamaria (@raysan5)

const rl = @import("raylib");
const rm = rl.math;

const AngleMode = enum {
    between_vectors, // Vector2Angle()
    line_to_horizontal, // Vector2LineAngle()
};

pub fn main() void {
    // Initialization
    const screen_width = 800;
    const screen_height = 450;

    rl.initWindow(screen_width, screen_height, "raylib [shapes] example - vector angle");
    defer rl.closeWindow(); // Close window and OpenGL context

    const v0: rl.Vector2 = .{ .x = screen_width / 2.0, .y = screen_height / 2.0 };
    var v1 = rm.vector2Add(v0, .{ .x = 100.0, .y = 80.0 });

    var angle: f32 = 0.0; // Angle in degrees
    var angle_mode: AngleMode = .between_vectors;

    rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second

    // Main game loop
    while (!rl.windowShouldClose()) { // Detect window close button or ESC key
        // Update
        const start_angle: f32 = switch (angle_mode) {
            .between_vectors => -rm.vector2LineAngle(v0, v1) * rl.rad2deg,
            .line_to_horizontal => 0.0,
        };

        const v2 = rl.getMousePosition();

        if (rl.isKeyPressed(.space)) angle_mode = switch (angle_mode) {
            .between_vectors => .line_to_horizontal,
            .line_to_horizontal => .between_vectors,
        };

        if (angle_mode == .between_vectors and rl.isMouseButtonDown(.right)) v1 = rl.getMousePosition();

        switch (angle_mode) {
            .between_vectors => {
                // Calculate angle between two vectors, considering a common origin (v0)
                const v1_normal = rm.vector2Normalize(rm.vector2Subtract(v1, v0));
                const v2_normal = rm.vector2Normalize(rm.vector2Subtract(v2, v0));

                angle = rm.vector2Angle(v1_normal, v2_normal) * rl.rad2deg;
            },
            .line_to_horizontal => {
                // Calculate angle defined by a two vectors line, in reference to horizontal line
                angle = rm.vector2LineAngle(v0, v2) * rl.rad2deg;
            },
        }

        // Draw
        rl.beginDrawing();
        defer rl.endDrawing();

        rl.clearBackground(.ray_white);

        switch (angle_mode) {
            .between_vectors => {
                rl.drawText("MODE 0: Angle between V1 and V2", 10, 10, 20, .black);
                rl.drawText("Right Click to Move V2", 10, 30, 20, .dark_gray);

                rl.drawLineEx(v0, v1, 2.0, .black);
                rl.drawLineEx(v0, v2, 2.0, .red);

                rl.drawCircleSector(v0, 40.0, start_angle, start_angle + angle, 32, rl.fade(.green, 0.6));
            },
            .line_to_horizontal => {
                rl.drawText("MODE 1: Angle formed by line V1 to V2", 10, 10, 20, .black);

                rl.drawLine(0, screen_height / 2, screen_width, screen_height / 2, .light_gray);
                rl.drawLineEx(v0, v2, 2.0, .red);

                rl.drawCircleSector(v0, 40.0, start_angle, start_angle - angle, 32, rl.fade(.green, 0.6));
            },
        }

        const v0_x: i32 = @intFromFloat(v0.x);
        const v0_y: i32 = @intFromFloat(v0.y);
        rl.drawText("v0", v0_x, v0_y, 10, .dark_gray);

        switch (angle_mode) {
            .between_vectors => {
                // If the line from v0 to v1 would overlap the text, move it's position up 10
                const dy = rm.vector2Subtract(v0, v1).y;
                const v1_x: i32 = @intFromFloat(v1.x);
                const v1_y: i32 = @intFromFloat(v1.y);
                if (dy > 0.0) rl.drawText("v1", v1_x, v1_y - 10, 10, .dark_gray);
                if (dy < 0.0) rl.drawText("v1", v1_x, v1_y, 10, .dark_gray);
            },
            // Use v1 to emphasize the horizontal line
            .line_to_horizontal => rl.drawText("v1", v0_x + 40, v0_y, 10, .dark_gray),
        }

        // position adjusted by -10 so it isn't hidden by cursor
        rl.drawText("v2", @as(i32, @intFromFloat(v2.x)) - 10, @as(i32, @intFromFloat(v2.y)) - 10, 10, .dark_gray);

        rl.drawText("Press SPACE to change MODE", 460, 10, 20, .dark_gray);
        rl.drawText(rl.textFormat("ANGLE: {d:.2}", .{angle}), 10, 70, 20, .lime);
    }
}
/*******************************************************************************************
*
*   raylib [shapes] example - vector angle
*
*   Example complexity rating: [★★☆☆] 2/4
*
*   Example originally created with raylib 1.0, last time updated with raylib 5.0
*
*   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) 2023-2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#include "raylib.h"

#include "raymath.h"        // Required for: Vector2LineAngle()

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
    // Initialization
    //--------------------------------------------------------------------------------------
    const int screenWidth = 800;

    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [shapes] example - vector angle");

    Vector2 v0 = { screenWidth/2.0f, screenHeight/2.0f };
    Vector2 v1 = Vector2Add(v0, (Vector2){ 100.0f, 80.0f });
    Vector2 v2 = { 0 };             // Updated with mouse position

    float angle = 0.0f;             // Angle in degrees
    int angleMode = 0;              // 0-Vector2Angle(), 1-Vector2LineAngle()

    SetTargetFPS(60);               // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())    // Detect window close button or ESC key
    {
        // Update
        //----------------------------------------------------------------------------------
        float startangle = 0.0f;

        if (angleMode == 0) startangle = -Vector2LineAngle(v0, v1)*RAD2DEG;
        if (angleMode == 1) startangle = 0.0f;

        v2 = GetMousePosition();

        if (IsKeyPressed(KEY_SPACE)) angleMode = !angleMode;

        if ((angleMode == 0) && IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) v1 = GetMousePosition();

        if (angleMode == 0)
        {
            // Calculate angle between two vectors, considering a common origin (v0)
            Vector2 v1Normal = Vector2Normalize(Vector2Subtract(v1, v0));
            Vector2 v2Normal = Vector2Normalize(Vector2Subtract(v2, v0));

            angle = Vector2Angle(v1Normal, v2Normal)*RAD2DEG;
        }
        else if (angleMode == 1)
        {
            // Calculate angle defined by a two vectors line, in reference to horizontal line
            angle = Vector2LineAngle(v0, v2)*RAD2DEG;
        }
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();

            ClearBackground(RAYWHITE);

            if (angleMode == 0)
            {
                DrawText("MODE 0: Angle between V1 and V2", 10, 10, 20, BLACK);
                DrawText("Right Click to Move V2", 10, 30, 20, DARKGRAY);

                DrawLineEx(v0, v1, 2.0f, BLACK);
                DrawLineEx(v0, v2, 2.0f, RED);

                DrawCircleSector(v0, 40.0f, startangle, startangle + angle, 32, Fade(GREEN, 0.6f));
            }
            else if (angleMode == 1)
            {
                DrawText("MODE 1: Angle formed by line V1 to V2", 10, 10, 20, BLACK);

                DrawLine(0, screenHeight/2, screenWidth, screenHeight/2, LIGHTGRAY);
                DrawLineEx(v0, v2, 2.0f, RED);

                DrawCircleSector(v0, 40.0f, startangle, startangle - angle, 32, Fade(GREEN, 0.6f));
            }

            DrawText("v0", (int)v0.x, (int)v0.y, 10, DARKGRAY);

            // If the line from v0 to v1 would overlap the text, move it's position up 10
            if (angleMode == 0 && Vector2Subtract(v0, v1).y > 0.0f) DrawText("v1", (int)v1.x, (int)v1.y-10, 10, DARKGRAY);
            if (angleMode == 0 && Vector2Subtract(v0, v1).y < 0.0f) DrawText("v1", (int)v1.x, (int)v1.y, 10, DARKGRAY);

            // If angle mode 1, use v1 to emphasize the horizontal line
            if (angleMode == 1) DrawText("v1", (int)v0.x + 40, (int)v0.y, 10, DARKGRAY);

            // position adjusted by -10 so it isn't hidden by cursor
            DrawText("v2", (int)v2.x-10, (int)v2.y-10, 10, DARKGRAY);

            DrawText("Press SPACE to change MODE", 460, 10, 20, DARKGRAY);
            DrawText(TextFormat("ANGLE: %2.2f", angle), 10, 70, 20, LIME);

        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    CloseWindow();        // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}

From raylib 6.2. The original on GitHub.