REVIEWED: DrawLineDashed()

This commit is contained in:
Ray
2025-10-02 13:38:13 +02:00
parent 36d3c8acfb
commit 7e3b7cd349
3 changed files with 59 additions and 69 deletions

View File

@@ -16,8 +16,6 @@
********************************************************************************************/ ********************************************************************************************/
#include "raylib.h" #include "raylib.h"
#define RAYGUI_IMPLEMENTATION
#include "raygui.h" // Required for GUI controls
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Program main entry point // Program main entry point
@@ -42,6 +40,7 @@ int main(void)
int colorIndex = 0; int colorIndex = 0;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop // Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key while (!WindowShouldClose()) // Detect window close button or ESC key
@@ -50,8 +49,6 @@ int main(void)
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
lineEndPosition = GetMousePosition(); // Line endpoint follows the mouse lineEndPosition = GetMousePosition(); // Line endpoint follows the mouse
// --- Keyboard Controls ---
// Change Dash Length (UP/DOWN arrows) // Change Dash Length (UP/DOWN arrows)
if (IsKeyDown(KEY_UP)) dashLength += 1.0f; if (IsKeyDown(KEY_UP)) dashLength += 1.0f;
if (IsKeyDown(KEY_DOWN) && dashLength > 1.0f) dashLength -= 1.0f; if (IsKeyDown(KEY_DOWN) && dashLength > 1.0f) dashLength -= 1.0f;
@@ -61,11 +58,7 @@ int main(void)
if (IsKeyDown(KEY_LEFT) && blankLength > 1.0f) blankLength -= 1.0f; if (IsKeyDown(KEY_LEFT) && blankLength > 1.0f) blankLength -= 1.0f;
// Cycle through colors ('C' key) // Cycle through colors ('C' key)
if (IsKeyPressed(KEY_C)) if (IsKeyPressed(KEY_C)) colorIndex = (colorIndex + 1)%(sizeof(lineColors)/sizeof(Color));
{
colorIndex = (colorIndex + 1) % (sizeof(lineColors)/sizeof(Color));
}
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw

View File

@@ -1258,11 +1258,11 @@ RLAPI Rectangle GetShapesTextureRectangle(void); // Get texture source re
RLAPI void DrawPixel(int posX, int posY, Color color); // Draw a pixel using geometry [Can be slow, use with care] RLAPI void DrawPixel(int posX, int posY, Color color); // Draw a pixel using geometry [Can be slow, use with care]
RLAPI void DrawPixelV(Vector2 position, Color color); // Draw a pixel using geometry (Vector version) [Can be slow, use with care] RLAPI void DrawPixelV(Vector2 position, Color color); // Draw a pixel using geometry (Vector version) [Can be slow, use with care]
RLAPI void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color color); // Draw a line RLAPI void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color color); // Draw a line
RLAPI void DrawLineDashed(Vector2 startPos, Vector2 endPos, int dashSize, int whiteSpaceSize, Color color); // Draw a dashed line
RLAPI void DrawLineV(Vector2 startPos, Vector2 endPos, Color color); // Draw a line (using gl lines) RLAPI void DrawLineV(Vector2 startPos, Vector2 endPos, Color color); // Draw a line (using gl lines)
RLAPI void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color); // Draw a line (using triangles/quads) RLAPI void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color); // Draw a line (using triangles/quads)
RLAPI void DrawLineStrip(const Vector2 *points, int pointCount, Color color); // Draw lines sequence (using gl lines) RLAPI void DrawLineStrip(const Vector2 *points, int pointCount, Color color); // Draw lines sequence (using gl lines)
RLAPI void DrawLineBezier(Vector2 startPos, Vector2 endPos, float thick, Color color); // Draw line segment cubic-bezier in-out interpolation RLAPI void DrawLineBezier(Vector2 startPos, Vector2 endPos, float thick, Color color); // Draw line segment cubic-bezier in-out interpolation
RLAPI void DrawLineDashed(Vector2 startPos, Vector2 endPos, int dashSize, int spaceSize, Color color); // Draw a dashed line
RLAPI void DrawCircle(int centerX, int centerY, float radius, Color color); // Draw a color-filled circle RLAPI void DrawCircle(int centerX, int centerY, float radius, Color color); // Draw a color-filled circle
RLAPI void DrawCircleSector(Vector2 center, float radius, float startAngle, float endAngle, int segments, Color color); // Draw a piece of a circle RLAPI void DrawCircleSector(Vector2 center, float radius, float startAngle, float endAngle, int segments, Color color); // Draw a piece of a circle
RLAPI void DrawCircleSectorLines(Vector2 center, float radius, float startAngle, float endAngle, int segments, Color color); // Draw circle sector outline RLAPI void DrawCircleSectorLines(Vector2 center, float radius, float startAngle, float endAngle, int segments, Color color); // Draw circle sector outline

View File

@@ -182,53 +182,26 @@ void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color colo
rlEnd(); rlEnd();
} }
void DrawLineDashed(Vector2 startPos, Vector2 endPos, int dashSize, int whiteSpaceSize, Color color) // Draw a line defining thickness
void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color)
{ {
// Calculate the vector and length of the line Vector2 delta = { endPos.x - startPos.x, endPos.y - startPos.y };
float dx = endPos.x - startPos.x; float length = sqrtf(delta.x*delta.x + delta.y*delta.y);
float dy = endPos.y - startPos.y;
float lineLength = sqrtf(dx*dx + dy*dy);
// If the line is too short for dashing or dash size is invalid, draw a solid thick line if ((length > 0) && (thick > 0))
if (lineLength < (dashSize + whiteSpaceSize) || dashSize <= 0)
{ {
DrawLineV(startPos, endPos, color); float scale = thick/(2*length);
return;
Vector2 radius = { -scale*delta.y, scale*delta.x };
Vector2 strip[4] = {
{ startPos.x - radius.x, startPos.y - radius.y },
{ startPos.x + radius.x, startPos.y + radius.y },
{ endPos.x - radius.x, endPos.y - radius.y },
{ endPos.x + radius.x, endPos.y + radius.y }
};
DrawTriangleStrip(strip, 4, color);
} }
// Calculate the normalized direction vector of the line
float invLineLength = 1 / lineLength;
float dirX = dx * invLineLength;
float dirY = dy * invLineLength;
Vector2 currentPos = startPos;
float distanceTraveled = 0;
rlBegin(RL_LINES);
rlColor4ub(color.r, color.g, color.b, color.a);
while (distanceTraveled < lineLength)
{
// Calculate the end of the current dash
float dashEndDist = distanceTraveled + dashSize;
if (dashEndDist > lineLength)
{
dashEndDist = lineLength;
}
Vector2 dashEndPos = { startPos.x + dashEndDist * dirX, startPos.y + dashEndDist * dirY };
// Draw the dash segment
rlVertex2f(currentPos.x, currentPos.y);
rlVertex2f(dashEndPos.x, dashEndPos.y);
// Update the distance traveled and move the current position for the next dash
distanceTraveled = dashEndDist + whiteSpaceSize;
currentPos.x = startPos.x + distanceTraveled * dirX;
currentPos.y = startPos.y + distanceTraveled * dirY;
}
rlEnd();
} }
// Draw a line (using gl lines) // Draw a line (using gl lines)
@@ -295,26 +268,50 @@ void DrawLineBezier(Vector2 startPos, Vector2 endPos, float thick, Color color)
DrawTriangleStrip(points, 2*SPLINE_SEGMENT_DIVISIONS + 2, color); DrawTriangleStrip(points, 2*SPLINE_SEGMENT_DIVISIONS + 2, color);
} }
// Draw a line defining thickness // Draw a dashed line
void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color) void DrawLineDashed(Vector2 startPos, Vector2 endPos, int dashSize, int spaceSize, Color color)
{ {
Vector2 delta = { endPos.x - startPos.x, endPos.y - startPos.y }; // Calculate the vector and length of the line
float length = sqrtf(delta.x*delta.x + delta.y*delta.y); float dx = endPos.x - startPos.x;
float dy = endPos.y - startPos.y;
float lineLength = sqrtf(dx*dx + dy*dy);
if ((length > 0) && (thick > 0)) // If the line is too short for dashing or dash size is invalid, draw a solid thick line
if ((lineLength < (dashSize + spaceSize)) || (dashSize <= 0))
{ {
float scale = thick/(2*length); DrawLineV(startPos, endPos, color);
return;
Vector2 radius = { -scale*delta.y, scale*delta.x };
Vector2 strip[4] = {
{ startPos.x - radius.x, startPos.y - radius.y },
{ startPos.x + radius.x, startPos.y + radius.y },
{ endPos.x - radius.x, endPos.y - radius.y },
{ endPos.x + radius.x, endPos.y + radius.y }
};
DrawTriangleStrip(strip, 4, color);
} }
// Calculate the normalized direction vector of the line
float invLineLength = 1.0f/lineLength;
float dirX = dx*invLineLength;
float dirY = dy*invLineLength;
Vector2 currentPos = startPos;
float distanceTraveled = 0;
rlBegin(RL_LINES);
rlColor4ub(color.r, color.g, color.b, color.a);
while (distanceTraveled < lineLength)
{
// Calculate the end of the current dash
float dashEndDist = distanceTraveled + dashSize;
if (dashEndDist > lineLength) dashEndDist = lineLength;
Vector2 dashEndPos = { startPos.x + dashEndDist*dirX, startPos.y + dashEndDist*dirY };
// Draw the dash segment
rlVertex2f(currentPos.x, currentPos.y);
rlVertex2f(dashEndPos.x, dashEndPos.y);
// Update the distance traveled and move the current position for the next dash
distanceTraveled = dashEndDist + spaceSize;
currentPos.x = startPos.x + distanceTraveled*dirX;
currentPos.y = startPos.y + distanceTraveled*dirY;
}
rlEnd();
} }
// Draw a color-filled circle // Draw a color-filled circle