Skip to content

Commit 7e3b7cd

Browse files
committed
REVIEWED: DrawLineDashed()
1 parent 36d3c8a commit 7e3b7cd

File tree

3 files changed

+56
-66
lines changed

3 files changed

+56
-66
lines changed

examples/shapes/shapes_dashed_line.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
********************************************************************************************/
1717

1818
#include "raylib.h"
19-
#define RAYGUI_IMPLEMENTATION
20-
#include "raygui.h" // Required for GUI controls
2119

2220
//------------------------------------------------------------------------------------
2321
// Program main entry point
@@ -42,6 +40,7 @@ int main(void)
4240
int colorIndex = 0;
4341

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

4645
// Main game loop
4746
while (!WindowShouldClose()) // Detect window close button or ESC key
@@ -50,8 +49,6 @@ int main(void)
5049
//----------------------------------------------------------------------------------
5150
lineEndPosition = GetMousePosition(); // Line endpoint follows the mouse
5251

53-
// --- Keyboard Controls ---
54-
5552
// Change Dash Length (UP/DOWN arrows)
5653
if (IsKeyDown(KEY_UP)) dashLength += 1.0f;
5754
if (IsKeyDown(KEY_DOWN) && dashLength > 1.0f) dashLength -= 1.0f;
@@ -61,11 +58,7 @@ int main(void)
6158
if (IsKeyDown(KEY_LEFT) && blankLength > 1.0f) blankLength -= 1.0f;
6259

6360
// Cycle through colors ('C' key)
64-
if (IsKeyPressed(KEY_C))
65-
{
66-
colorIndex = (colorIndex + 1) % (sizeof(lineColors)/sizeof(Color));
67-
}
68-
61+
if (IsKeyPressed(KEY_C)) colorIndex = (colorIndex + 1)%(sizeof(lineColors)/sizeof(Color));
6962
//----------------------------------------------------------------------------------
7063

7164
// Draw

src/raylib.h

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

src/rshapes.c

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -182,53 +182,26 @@ void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color colo
182182
rlEnd();
183183
}
184184

185-
void DrawLineDashed(Vector2 startPos, Vector2 endPos, int dashSize, int whiteSpaceSize, Color color)
185+
// Draw a line defining thickness
186+
void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color)
186187
{
187-
// Calculate the vector and length of the line
188-
float dx = endPos.x - startPos.x;
189-
float dy = endPos.y - startPos.y;
190-
float lineLength = sqrtf(dx*dx + dy*dy);
191-
192-
// If the line is too short for dashing or dash size is invalid, draw a solid thick line
193-
if (lineLength < (dashSize + whiteSpaceSize) || dashSize <= 0)
194-
{
195-
DrawLineV(startPos, endPos, color);
196-
return;
197-
}
198-
199-
// Calculate the normalized direction vector of the line
200-
float invLineLength = 1 / lineLength;
201-
float dirX = dx * invLineLength;
202-
float dirY = dy * invLineLength;
203-
204-
Vector2 currentPos = startPos;
205-
float distanceTraveled = 0;
206-
207-
rlBegin(RL_LINES);
208-
rlColor4ub(color.r, color.g, color.b, color.a);
188+
Vector2 delta = { endPos.x - startPos.x, endPos.y - startPos.y };
189+
float length = sqrtf(delta.x*delta.x + delta.y*delta.y);
209190

210-
while (distanceTraveled < lineLength)
191+
if ((length > 0) && (thick > 0))
211192
{
212-
// Calculate the end of the current dash
213-
float dashEndDist = distanceTraveled + dashSize;
214-
if (dashEndDist > lineLength)
215-
{
216-
dashEndDist = lineLength;
217-
}
218-
219-
Vector2 dashEndPos = { startPos.x + dashEndDist * dirX, startPos.y + dashEndDist * dirY };
193+
float scale = thick/(2*length);
220194

221-
// Draw the dash segment
222-
rlVertex2f(currentPos.x, currentPos.y);
223-
rlVertex2f(dashEndPos.x, dashEndPos.y);
195+
Vector2 radius = { -scale*delta.y, scale*delta.x };
196+
Vector2 strip[4] = {
197+
{ startPos.x - radius.x, startPos.y - radius.y },
198+
{ startPos.x + radius.x, startPos.y + radius.y },
199+
{ endPos.x - radius.x, endPos.y - radius.y },
200+
{ endPos.x + radius.x, endPos.y + radius.y }
201+
};
224202

225-
// Update the distance traveled and move the current position for the next dash
226-
distanceTraveled = dashEndDist + whiteSpaceSize;
227-
currentPos.x = startPos.x + distanceTraveled * dirX;
228-
currentPos.y = startPos.y + distanceTraveled * dirY;
203+
DrawTriangleStrip(strip, 4, color);
229204
}
230-
231-
rlEnd();
232205
}
233206

234207
// Draw a line (using gl lines)
@@ -295,26 +268,50 @@ void DrawLineBezier(Vector2 startPos, Vector2 endPos, float thick, Color color)
295268
DrawTriangleStrip(points, 2*SPLINE_SEGMENT_DIVISIONS + 2, color);
296269
}
297270

298-
// Draw a line defining thickness
299-
void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color)
271+
// Draw a dashed line
272+
void DrawLineDashed(Vector2 startPos, Vector2 endPos, int dashSize, int spaceSize, Color color)
300273
{
301-
Vector2 delta = { endPos.x - startPos.x, endPos.y - startPos.y };
302-
float length = sqrtf(delta.x*delta.x + delta.y*delta.y);
274+
// Calculate the vector and length of the line
275+
float dx = endPos.x - startPos.x;
276+
float dy = endPos.y - startPos.y;
277+
float lineLength = sqrtf(dx*dx + dy*dy);
303278

304-
if ((length > 0) && (thick > 0))
279+
// If the line is too short for dashing or dash size is invalid, draw a solid thick line
280+
if ((lineLength < (dashSize + spaceSize)) || (dashSize <= 0))
305281
{
306-
float scale = thick/(2*length);
282+
DrawLineV(startPos, endPos, color);
283+
return;
284+
}
307285

308-
Vector2 radius = { -scale*delta.y, scale*delta.x };
309-
Vector2 strip[4] = {
310-
{ startPos.x - radius.x, startPos.y - radius.y },
311-
{ startPos.x + radius.x, startPos.y + radius.y },
312-
{ endPos.x - radius.x, endPos.y - radius.y },
313-
{ endPos.x + radius.x, endPos.y + radius.y }
314-
};
286+
// Calculate the normalized direction vector of the line
287+
float invLineLength = 1.0f/lineLength;
288+
float dirX = dx*invLineLength;
289+
float dirY = dy*invLineLength;
315290

316-
DrawTriangleStrip(strip, 4, color);
317-
}
291+
Vector2 currentPos = startPos;
292+
float distanceTraveled = 0;
293+
294+
rlBegin(RL_LINES);
295+
rlColor4ub(color.r, color.g, color.b, color.a);
296+
297+
while (distanceTraveled < lineLength)
298+
{
299+
// Calculate the end of the current dash
300+
float dashEndDist = distanceTraveled + dashSize;
301+
if (dashEndDist > lineLength) dashEndDist = lineLength;
302+
303+
Vector2 dashEndPos = { startPos.x + dashEndDist*dirX, startPos.y + dashEndDist*dirY };
304+
305+
// Draw the dash segment
306+
rlVertex2f(currentPos.x, currentPos.y);
307+
rlVertex2f(dashEndPos.x, dashEndPos.y);
308+
309+
// Update the distance traveled and move the current position for the next dash
310+
distanceTraveled = dashEndDist + spaceSize;
311+
currentPos.x = startPos.x + distanceTraveled*dirX;
312+
currentPos.y = startPos.y + distanceTraveled*dirY;
313+
}
314+
rlEnd();
318315
}
319316

320317
// Draw a color-filled circle

0 commit comments

Comments
 (0)