From 5679fe85f832b5037da3a1e62f77a772b95e5a99 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 2 Oct 2025 17:36:12 +0200 Subject: [PATCH 1/2] adding example with translation, rotation and scale for 2D Rectangle. #5209 --- examples/Makefile | 3 +- examples/Makefile.Web | 3 +- .../shapes_translation_rotation_scale.c | 96 +++++++++++++++++++ 3 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 examples/shapes/shapes_translation_rotation_scale.c diff --git a/examples/Makefile b/examples/Makefile index fb33b4894759..ec0d2e696bc6 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -563,7 +563,8 @@ SHAPES = \ shapes/shapes_rounded_rectangle_drawing \ shapes/shapes_splines_drawing \ shapes/shapes_top_down_lights \ - shapes/shapes_vector_angle + shapes/shapes_vector_angle \ + shapes/shapes_translation_rotation_scale TEXTURES = \ textures/textures_background_scrolling \ diff --git a/examples/Makefile.Web b/examples/Makefile.Web index 0dbefadf2672..3817c421d612 100644 --- a/examples/Makefile.Web +++ b/examples/Makefile.Web @@ -563,7 +563,8 @@ SHAPES = \ shapes/shapes_rounded_rectangle_drawing \ shapes/shapes_splines_drawing \ shapes/shapes_top_down_lights \ - shapes/shapes_vector_angle + shapes/shapes_vector_angle \ + shapes/shapes_translation_rotation_scale TEXTURES = \ textures/textures_background_scrolling \ diff --git a/examples/shapes/shapes_translation_rotation_scale.c b/examples/shapes/shapes_translation_rotation_scale.c new file mode 100644 index 000000000000..0b7f151327d7 --- /dev/null +++ b/examples/shapes/shapes_translation_rotation_scale.c @@ -0,0 +1,96 @@ +/******************************************************************************************* +* +* raylib [shape] example - Translation, rotation and scaling +* +* Example complexity rating: [★☆☆☆] 1/4 +* +* Example originally created with raylib 5.5, last time updated with raylib 5.6 +* +* Example contributed by zerasul (@zerasul) 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 zerasul (@zerasul) - All rights reserved. +* +********************************************************************************************/ + +#include "raylib.h" + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib shape example - Translation, rotation and scaling"); + + // Rectangle variables + int rectWidth = 100; + int rectHeight = 100; + Rectangle rec = { 300,400, rectWidth, rectHeight }; + Vector2 origin = { rectWidth/2.0f, rectHeight/2.0f }; + //rotation angle and scale factors + float rotation = 0.0f; + float scale = 1.0f; + + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + + //if an arrow key is pressed, move the rectangle in the corresponding direction + if(IsKeyDown(KEY_RIGHT)) rec.x += 2.0f; + if(IsKeyDown(KEY_LEFT)) rec.x -= 2.0f; + if(IsKeyDown(KEY_UP)) rec.y -= 2.0f; + if(IsKeyDown(KEY_DOWN)) rec.y += 2.0f; + + //if R or T keys are pressed, rotate the rectangle + if(IsKeyDown(KEY_R)) rotation += 2.0f; + if(IsKeyDown(KEY_T)) rotation -= 2.0f; + + //if + or - keys are pressed, scale the rectangle + if(IsKeyDown(KEY_KP_ADD)) scale += 0.02f; + if(IsKeyDown(KEY_KP_SUBTRACT)) scale -= 0.02f; + + //scale limits + if(scale < 0.1f) scale = 0.1f; + if(scale > 3.0f) scale = 3.0f; + + //recalcuate rectangle size and origin based on scale + rec.width = rectWidth * scale; + rec.height = rectHeight * scale; + origin = (Vector2){ rec.width/2.0f, rec.height/2.0f }; + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + DrawText("Translation, Rotation and Scale Example", 10, 10, 30, LIGHTGRAY); + DrawText("Use arrow keys to move rectangle", 10, 50, 20, DARKGRAY); + DrawText("Use R and T keys to rotate rectangle", 10, 80, 20, DARKGRAY); + DrawText("Use + and - keys to scale rectangle", 10, 110, 20, DARKGRAY); + + DrawRectanglePro(rec, origin, rotation, RED); // Rotation around rectangle center + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + + // De-Initialization + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} \ No newline at end of file From 3697e27f5447699b85b2534ff18f62cb40c7d76e Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 15 Oct 2025 20:45:19 +0200 Subject: [PATCH 2/2] adding screenshot --- .../shapes_translation_rotation_scale.png | Bin 0 -> 24393 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 examples/shapes/shapes_translation_rotation_scale.png diff --git a/examples/shapes/shapes_translation_rotation_scale.png b/examples/shapes/shapes_translation_rotation_scale.png new file mode 100644 index 0000000000000000000000000000000000000000..e17bb7f506d84c95549ec10848155d14d40a1d83 GIT binary patch literal 24393 zcmeHPd03L^7AJL33yGXEmlVm$%C*2Oz3vKbX`wA+>ZnsnZ!hec!2Lb@Aq=fdC&Qs-#H&K zD_jT)lT|0fU@!#-ds`w5Ce4Duq!!D;fp6Asd8Pt`{fKg~wQ}F?{nOw;cN>+q(oj{3 zjuG>cjw!{da>RO}@)SZ{@1P7|vkIcuN7 zNb?g2g2){L`$z3BCCTo_slGls`6!ZJWM$`+Z3_(@>W3c1)CKr~WR&fO?_=_c0qbUj`olh?a>!6Fkwc3sOklGF$|=Q51N4AG2Z8rRZ1|oh?@M z8wGoyEyIQ3II-9ofZ>yQFq|S*^cw|xps^VQ!$p>xl1Bg#3>S$QB#!_f7%mDid;|ml z!EjM9;3FV_uM37xQu1pI?(f`i#&pe^HQR90P%2V)nq1q0=@t6nB_a89HOMW~JStxK z97tkNj0O*ylC#d1o!pi)V^ieiz*+K57RTFuVHDSr+(wBv*fKsGE|(-6w(L^e=9lOb z*W6M~&s9;BqC0ipnIh{Eh*5FbxE1FWcT&+GcV2eMc|tk0a0!}U2=OCd1n>c%5T{F& zsLDqqu1#MbW6*i{Y7-f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