Logo

index : raylib-jai

---

  • summary
  • about
  • tree
  • log
  • branches
<< path: root/public/raylib-jai.git/html/Raylib/raylib/src/rshapes.c blob: 528a362d5e4f0842e2fb009aa034deaa1c48449e [raw] [clear marker]

        
0/**********************************************************************************************
1*
2* rshapes - Basic functions to draw 2d shapes and check collisions
3*
4* ADDITIONAL NOTES:
5* Shapes can be draw using 3 types of primitives: LINES, TRIANGLES and QUADS
6* Some functions implement two drawing options: TRIANGLES and QUADS, by default TRIANGLES
7* are used but QUADS implementation can be selected with SUPPORT_QUADS_DRAW_MODE define
8*
9* Some functions define texture coordinates (rlTexCoord2f()) for the shapes and use a
10* user-provided texture with SetShapesTexture(), the pourpouse of this implementation
11* is allowing to reduce draw calls when combined with a texture-atlas
12*
13* By default, raylib sets the default texture and rectangle at InitWindow()[rcore] to one
14* white character of default font [rtext], this way, raylib text and shapes can be draw with
15* a single draw call and it also allows users to configure it the same way with their own fonts
16*
17* CONFIGURATION:
18* #define SUPPORT_MODULE_RSHAPES
19* rshapes module is included in the build
20*
21* #define SUPPORT_QUADS_DRAW_MODE
22* Use QUADS instead of TRIANGLES for drawing when possible. Lines-based shapes still use LINES
23*
24*
25* LICENSE: zlib/libpng
26*
27* Copyright (c) 2013-2025 Ramon Santamaria (@raysan5)
28*
29* This software is provided "as-is", without any express or implied warranty. In no event
30* will the authors be held liable for any damages arising from the use of this software.
31*
32* Permission is granted to anyone to use this software for any purpose, including commercial
33* applications, and to alter it and redistribute it freely, subject to the following restrictions:
34*
35* 1. The origin of this software must not be misrepresented; you must not claim that you
36* wrote the original software. If you use this software in a product, an acknowledgment
37* in the product documentation would be appreciated but is not required.
38*
39* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
40* as being the original software.
41*
42* 3. This notice may not be removed or altered from any source distribution.
43*
44**********************************************************************************************/
45
46#include "raylib.h" // Declares module functions
47
48// Check if config flags have been externally provided on compilation line
49#if !defined(EXTERNAL_CONFIG_FLAGS)
50 #include "config.h" // Defines module configuration flags
51#endif
52
53#if defined(SUPPORT_MODULE_RSHAPES)
54
55#include "rlgl.h" // OpenGL abstraction layer to OpenGL 1.1, 2.1, 3.3+ or ES2
56
57#include <math.h> // Required for: sinf(), asinf(), cosf(), acosf(), sqrtf(), fabsf()
58#include <float.h> // Required for: FLT_EPSILON
59#include <stdlib.h> // Required for: RL_FREE
60
61//----------------------------------------------------------------------------------
62// Defines and Macros
63//----------------------------------------------------------------------------------
64// Error rate to calculate how many segments we need to draw a smooth circle,
65// taken from https://stackoverflow.com/a/2244088
66#ifndef SMOOTH_CIRCLE_ERROR_RATE
67 #define SMOOTH_CIRCLE_ERROR_RATE 0.5f // Circle error rate
68#endif
69#ifndef SPLINE_SEGMENT_DIVISIONS
70 #define SPLINE_SEGMENT_DIVISIONS 24 // Spline segment divisions
71#endif
72
73//----------------------------------------------------------------------------------
74// Types and Structures Definition
75//----------------------------------------------------------------------------------
76// Not here...
77
78//----------------------------------------------------------------------------------
79// Global Variables Definition
80//----------------------------------------------------------------------------------
81static Texture2D texShapes = { 1, 1, 1, 1, 7 }; // Texture used on shapes drawing (white pixel loaded by rlgl)
82static Rectangle texShapesRec = { 0.0f, 0.0f, 1.0f, 1.0f }; // Texture source rectangle used on shapes drawing
83
84//----------------------------------------------------------------------------------
85// Module Internal Functions Declaration
86//----------------------------------------------------------------------------------
87static float EaseCubicInOut(float t, float b, float c, float d); // Cubic easing
88
89//----------------------------------------------------------------------------------
90// Module Functions Definition
91//----------------------------------------------------------------------------------
92// Set texture and rectangle to be used on shapes drawing
93// NOTE: It can be useful when using basic shapes and one single font,
94// defining a font char white rectangle would allow drawing everything in a single draw call
95void SetShapesTexture(Texture2D texture, Rectangle source)
96{
97 // Reset texture to default pixel if required
98 // WARNING: Shapes texture should be probably better validated,
99 // it can break the rendering of all shapes if misused
100 if ((texture.id == 0) || (source.width == 0) || (source.height == 0))
101 {
102 texShapes = (Texture2D){ 1, 1, 1, 1, 7 };
103 texShapesRec = (Rectangle){ 0.0f, 0.0f, 1.0f, 1.0f };
104 }
105 else
106 {
107 texShapes = texture;
108 texShapesRec = source;
109 }
110}
111
112// Get texture that is used for shapes drawing
113Texture2D GetShapesTexture(void)
114{
115 return texShapes;
116}
117
118// Get texture source rectangle that is used for shapes drawing
119Rectangle GetShapesTextureRectangle(void)
120{
121 return texShapesRec;
122}
123
124// Draw a pixel
125void DrawPixel(int posX, int posY, Color color)
126{
127 DrawPixelV((Vector2){ (float)posX, (float)posY }, color);
128}
129
130// Draw a pixel (Vector version)
131void DrawPixelV(Vector2 position, Color color)
132{
133#if defined(SUPPORT_QUADS_DRAW_MODE)
134 rlSetTexture(GetShapesTexture().id);
135 Rectangle shapeRect = GetShapesTextureRectangle();
136
137 rlBegin(RL_QUADS);
138
139 rlNormal3f(0.0f, 0.0f, 1.0f);
140 rlColor4ub(color.r, color.g, color.b, color.a);
141
142 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
143 rlVertex2f(position.x, position.y);
144
145 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
146 rlVertex2f(position.x, position.y + 1);
147
148 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
149 rlVertex2f(position.x + 1, position.y + 1);
150
151 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
152 rlVertex2f(position.x + 1, position.y);
153
154 rlEnd();
155
156 rlSetTexture(0);
157#else
158 rlBegin(RL_TRIANGLES);
159
160 rlColor4ub(color.r, color.g, color.b, color.a);
161
162 rlVertex2f(position.x, position.y);
163 rlVertex2f(position.x, position.y + 1);
164 rlVertex2f(position.x + 1, position.y);
165
166 rlVertex2f(position.x + 1, position.y);
167 rlVertex2f(position.x, position.y + 1);
168 rlVertex2f(position.x + 1, position.y + 1);
169
170 rlEnd();
171#endif
172}
173
174// Draw a line (using gl lines)
175void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color color)
176{
177 rlBegin(RL_LINES);
178 rlColor4ub(color.r, color.g, color.b, color.a);
179 rlVertex2f((float)startPosX, (float)startPosY);
180 rlVertex2f((float)endPosX, (float)endPosY);
181 rlEnd();
182}
183
184// Draw a line defining thickness
185void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color)
186{
187 Vector2 delta = { endPos.x - startPos.x, endPos.y - startPos.y };
188 float length = sqrtf(delta.x*delta.x + delta.y*delta.y);
189
190 if ((length > 0) && (thick > 0))
191 {
192 float scale = thick/(2*length);
193
194 Vector2 radius = { -scale*delta.y, scale*delta.x };
195 Vector2 strip[4] = {
196 { startPos.x - radius.x, startPos.y - radius.y },
197 { startPos.x + radius.x, startPos.y + radius.y },
198 { endPos.x - radius.x, endPos.y - radius.y },
199 { endPos.x + radius.x, endPos.y + radius.y }
200 };
201
202 DrawTriangleStrip(strip, 4, color);
203 }
204}
205
206// Draw a line (using gl lines)
207void DrawLineV(Vector2 startPos, Vector2 endPos, Color color)
208{
209 rlBegin(RL_LINES);
210 rlColor4ub(color.r, color.g, color.b, color.a);
211 rlVertex2f(startPos.x, startPos.y);
212 rlVertex2f(endPos.x, endPos.y);
213 rlEnd();
214}
215
216// Draw lines sequuence (using gl lines)
217void DrawLineStrip(const Vector2 *points, int pointCount, Color color)
218{
219 if (pointCount < 2) return; // Security check
220
221 rlBegin(RL_LINES);
222 rlColor4ub(color.r, color.g, color.b, color.a);
223
224 for (int i = 0; i < pointCount - 1; i++)
225 {
226 rlVertex2f(points[i].x, points[i].y);
227 rlVertex2f(points[i + 1].x, points[i + 1].y);
228 }
229 rlEnd();
230}
231
232// Draw line using cubic-bezier spline, in-out interpolation, no control points
233void DrawLineBezier(Vector2 startPos, Vector2 endPos, float thick, Color color)
234{
235 Vector2 previous = startPos;
236 Vector2 current = { 0 };
237
238 Vector2 points[2*SPLINE_SEGMENT_DIVISIONS + 2] = { 0 };
239
240 for (int i = 1; i <= SPLINE_SEGMENT_DIVISIONS; i++)
241 {
242 // Cubic easing in-out
243 // NOTE: Easing is calculated only for y position value
244 current.y = EaseCubicInOut((float)i, startPos.y, endPos.y - startPos.y, (float)SPLINE_SEGMENT_DIVISIONS);
245 current.x = previous.x + (endPos.x - startPos.x)/(float)SPLINE_SEGMENT_DIVISIONS;
246
247 float dy = current.y - previous.y;
248 float dx = current.x - previous.x;
249 float size = 0.5f*thick/sqrtf(dx*dx+dy*dy);
250
251 if (i == 1)
252 {
253 points[0].x = previous.x + dy*size;
254 points[0].y = previous.y - dx*size;
255 points[1].x = previous.x - dy*size;
256 points[1].y = previous.y + dx*size;
257 }
258
259 points[2*i + 1].x = current.x - dy*size;
260 points[2*i + 1].y = current.y + dx*size;
261 points[2*i].x = current.x + dy*size;
262 points[2*i].y = current.y - dx*size;
263
264 previous = current;
265 }
266
267 DrawTriangleStrip(points, 2*SPLINE_SEGMENT_DIVISIONS + 2, color);
268}
269
270// Draw a dashed line
271void DrawLineDashed(Vector2 startPos, Vector2 endPos, int dashSize, int spaceSize, Color color)
272{
273 // Calculate the vector and length of the line
274 float dx = endPos.x - startPos.x;
275 float dy = endPos.y - startPos.y;
276 float lineLength = sqrtf(dx*dx + dy*dy);
277
278 // If the line is too short for dashing or dash size is invalid, draw a solid thick line
279 if ((lineLength < (dashSize + spaceSize)) || (dashSize <= 0))
280 {
281 DrawLineV(startPos, endPos, color);
282 return;
283 }
284
285 // Calculate the normalized direction vector of the line
286 float invLineLength = 1.0f/lineLength;
287 float dirX = dx*invLineLength;
288 float dirY = dy*invLineLength;
289
290 Vector2 currentPos = startPos;
291 float distanceTraveled = 0;
292
293 rlBegin(RL_LINES);
294 rlColor4ub(color.r, color.g, color.b, color.a);
295
296 while (distanceTraveled < lineLength)
297 {
298 // Calculate the end of the current dash
299 float dashEndDist = distanceTraveled + dashSize;
300 if (dashEndDist > lineLength) dashEndDist = lineLength;
301
302 Vector2 dashEndPos = { startPos.x + dashEndDist*dirX, startPos.y + dashEndDist*dirY };
303
304 // Draw the dash segment
305 rlVertex2f(currentPos.x, currentPos.y);
306 rlVertex2f(dashEndPos.x, dashEndPos.y);
307
308 // Update the distance traveled and move the current position for the next dash
309 distanceTraveled = dashEndDist + spaceSize;
310 currentPos.x = startPos.x + distanceTraveled*dirX;
311 currentPos.y = startPos.y + distanceTraveled*dirY;
312 }
313 rlEnd();
314}
315
316// Draw a color-filled circle
317void DrawCircle(int centerX, int centerY, float radius, Color color)
318{
319 DrawCircleV((Vector2){ (float)centerX, (float)centerY }, radius, color);
320}
321
322// Draw a color-filled circle (Vector version)
323// NOTE: On OpenGL 3.3 and ES2 we use QUADS to avoid drawing order issues
324void DrawCircleV(Vector2 center, float radius, Color color)
325{
326 DrawCircleSector(center, radius, 0, 360, 36, color);
327}
328
329// Draw a piece of a circle
330void DrawCircleSector(Vector2 center, float radius, float startAngle, float endAngle, int segments, Color color)
331{
332 if (startAngle == endAngle) return;
333 if (radius <= 0.0f) radius = 0.1f; // Avoid div by zero
334
335 // Function expects (endAngle > startAngle)
336 if (endAngle < startAngle)
337 {
338 // Swap values
339 float tmp = startAngle;
340 startAngle = endAngle;
341 endAngle = tmp;
342 }
343
344 int minSegments = (int)ceilf((endAngle - startAngle)/90);
345
346 if (segments < minSegments)
347 {
348 // Calculate the maximum angle between segments based on the error rate (usually 0.5f)
349 float th = acosf(2*powf(1 - SMOOTH_CIRCLE_ERROR_RATE/radius, 2) - 1);
350 segments = (int)((endAngle - startAngle)*ceilf(2*PI/th)/360);
351
352 if (segments <= 0) segments = minSegments;
353 }
354
355 float stepLength = (endAngle - startAngle)/(float)segments;
356 float angle = startAngle;
357
358#if defined(SUPPORT_QUADS_DRAW_MODE)
359 rlSetTexture(GetShapesTexture().id);
360 Rectangle shapeRect = GetShapesTextureRectangle();
361
362 rlBegin(RL_QUADS);
363
364 // NOTE: Every QUAD actually represents two segments
365 for (int i = 0; i < segments/2; i++)
366 {
367 rlColor4ub(color.r, color.g, color.b, color.a);
368
369 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
370 rlVertex2f(center.x, center.y);
371
372 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
373 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength*2.0f))*radius, center.y + sinf(DEG2RAD*(angle + stepLength*2.0f))*radius);
374
375 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
376 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*radius, center.y + sinf(DEG2RAD*(angle + stepLength))*radius);
377
378 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
379 rlVertex2f(center.x + cosf(DEG2RAD*angle)*radius, center.y + sinf(DEG2RAD*angle)*radius);
380
381 angle += (stepLength*2.0f);
382 }
383
384 // NOTE: In case number of segments is odd, we add one last piece to the cake
385 if ((((unsigned int)segments)%2) == 1)
386 {
387 rlColor4ub(color.r, color.g, color.b, color.a);
388
389 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
390 rlVertex2f(center.x, center.y);
391
392 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
393 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*radius, center.y + sinf(DEG2RAD*(angle + stepLength))*radius);
394
395 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
396 rlVertex2f(center.x + cosf(DEG2RAD*angle)*radius, center.y + sinf(DEG2RAD*angle)*radius);
397
398 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
399 rlVertex2f(center.x, center.y);
400 }
401
402 rlEnd();
403
404 rlSetTexture(0);
405#else
406 rlBegin(RL_TRIANGLES);
407 for (int i = 0; i < segments; i++)
408 {
409 rlColor4ub(color.r, color.g, color.b, color.a);
410
411 rlVertex2f(center.x, center.y);
412 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*radius, center.y + sinf(DEG2RAD*(angle + stepLength))*radius);
413 rlVertex2f(center.x + cosf(DEG2RAD*angle)*radius, center.y + sinf(DEG2RAD*angle)*radius);
414
415 angle += stepLength;
416 }
417 rlEnd();
418#endif
419}
420
421// Draw a piece of a circle outlines
422void DrawCircleSectorLines(Vector2 center, float radius, float startAngle, float endAngle, int segments, Color color)
423{
424 if (startAngle == endAngle) return;
425 if (radius <= 0.0f) radius = 0.1f; // Avoid div by zero issue
426
427 // Function expects (endAngle > startAngle)
428 if (endAngle < startAngle)
429 {
430 // Swap values
431 float tmp = startAngle;
432 startAngle = endAngle;
433 endAngle = tmp;
434 }
435
436 int minSegments = (int)ceilf((endAngle - startAngle)/90);
437
438 if (segments < minSegments)
439 {
440 // Calculate the maximum angle between segments based on the error rate (usually 0.5f)
441 float th = acosf(2*powf(1 - SMOOTH_CIRCLE_ERROR_RATE/radius, 2) - 1);
442 segments = (int)((endAngle - startAngle)*ceilf(2*PI/th)/360);
443
444 if (segments <= 0) segments = minSegments;
445 }
446
447 float stepLength = (endAngle - startAngle)/(float)segments;
448 float angle = startAngle;
449 bool showCapLines = true;
450
451 rlBegin(RL_LINES);
452 if (showCapLines)
453 {
454 rlColor4ub(color.r, color.g, color.b, color.a);
455 rlVertex2f(center.x, center.y);
456 rlVertex2f(center.x + cosf(DEG2RAD*angle)*radius, center.y + sinf(DEG2RAD*angle)*radius);
457 }
458
459 for (int i = 0; i < segments; i++)
460 {
461 rlColor4ub(color.r, color.g, color.b, color.a);
462
463 rlVertex2f(center.x + cosf(DEG2RAD*angle)*radius, center.y + sinf(DEG2RAD*angle)*radius);
464 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*radius, center.y + sinf(DEG2RAD*(angle + stepLength))*radius);
465
466 angle += stepLength;
467 }
468
469 if (showCapLines)
470 {
471 rlColor4ub(color.r, color.g, color.b, color.a);
472 rlVertex2f(center.x, center.y);
473 rlVertex2f(center.x + cosf(DEG2RAD*angle)*radius, center.y + sinf(DEG2RAD*angle)*radius);
474 }
475 rlEnd();
476}
477
478// Draw a gradient-filled circle
479void DrawCircleGradient(int centerX, int centerY, float radius, Color inner, Color outer)
480{
481 rlBegin(RL_TRIANGLES);
482 for (int i = 0; i < 360; i += 10)
483 {
484 rlColor4ub(inner.r, inner.g, inner.b, inner.a);
485 rlVertex2f((float)centerX, (float)centerY);
486 rlColor4ub(outer.r, outer.g, outer.b, outer.a);
487 rlVertex2f((float)centerX + cosf(DEG2RAD*(i + 10))*radius, (float)centerY + sinf(DEG2RAD*(i + 10))*radius);
488 rlColor4ub(outer.r, outer.g, outer.b, outer.a);
489 rlVertex2f((float)centerX + cosf(DEG2RAD*i)*radius, (float)centerY + sinf(DEG2RAD*i)*radius);
490 }
491 rlEnd();
492}
493
494// Draw circle outline
495void DrawCircleLines(int centerX, int centerY, float radius, Color color)
496{
497 DrawCircleLinesV((Vector2){ (float)centerX, (float)centerY }, radius, color);
498}
499
500// Draw circle outline (Vector version)
501void DrawCircleLinesV(Vector2 center, float radius, Color color)
502{
503 rlBegin(RL_LINES);
504 rlColor4ub(color.r, color.g, color.b, color.a);
505
506 // NOTE: Circle outline is drawn pixel by pixel every degree (0 to 360)
507 for (int i = 0; i < 360; i += 10)
508 {
509 rlVertex2f(center.x + cosf(DEG2RAD*i)*radius, center.y + sinf(DEG2RAD*i)*radius);
510 rlVertex2f(center.x + cosf(DEG2RAD*(i + 10))*radius, center.y + sinf(DEG2RAD*(i + 10))*radius);
511 }
512 rlEnd();
513}
514
515// Draw ellipse
516void DrawEllipse(int centerX, int centerY, float radiusH, float radiusV, Color color)
517{
518 DrawEllipseV((Vector2){ (float)centerX, (float)centerY }, radiusH, radiusV, color);
519}
520
521// Draw ellipse (Vector version)
522void DrawEllipseV(Vector2 center, float radiusH, float radiusV, Color color)
523{
524 rlBegin(RL_TRIANGLES);
525 for (int i = 0; i < 360; i += 10)
526 {
527 rlColor4ub(color.r, color.g, color.b, color.a);
528 rlVertex2f(center.x, center.y);
529 rlVertex2f(center.x + cosf(DEG2RAD*(i + 10))*radiusH, center.y + sinf(DEG2RAD*(i + 10))*radiusV);
530 rlVertex2f(center.x + cosf(DEG2RAD*i)*radiusH, center.y + sinf(DEG2RAD*i)*radiusV);
531 }
532 rlEnd();
533}
534
535// Draw ellipse outline
536void DrawEllipseLines(int centerX, int centerY, float radiusH, float radiusV, Color color)
537{
538 DrawEllipseLinesV((Vector2){ (float)centerX, (float)centerY }, radiusH, radiusV, color);
539}
540
541// Draw ellipse outline
542void DrawEllipseLinesV(Vector2 center, float radiusH, float radiusV, Color color)
543{
544 rlBegin(RL_LINES);
545 for (int i = 0; i < 360; i += 10)
546 {
547 rlColor4ub(color.r, color.g, color.b, color.a);
548 rlVertex2f(center.x + cosf(DEG2RAD*(i + 10))*radiusH, center.y + sinf(DEG2RAD*(i + 10))*radiusV);
549 rlVertex2f(center.x + cosf(DEG2RAD*i)*radiusH, center.y + sinf(DEG2RAD*i)*radiusV);
550 }
551 rlEnd();
552}
553
554// Draw ring
555void DrawRing(Vector2 center, float innerRadius, float outerRadius, float startAngle, float endAngle, int segments, Color color)
556{
557 if (startAngle == endAngle) return;
558
559 // Function expects (outerRadius > innerRadius)
560 if (outerRadius < innerRadius)
561 {
562 float tmp = outerRadius;
563 outerRadius = innerRadius;
564 innerRadius = tmp;
565
566 if (outerRadius <= 0.0f) outerRadius = 0.1f;
567 }
568
569 // Function expects (endAngle > startAngle)
570 if (endAngle < startAngle)
571 {
572 // Swap values
573 float tmp = startAngle;
574 startAngle = endAngle;
575 endAngle = tmp;
576 }
577
578 int minSegments = (int)ceilf((endAngle - startAngle)/90);
579
580 if (segments < minSegments)
581 {
582 // Calculate the maximum angle between segments based on the error rate (usually 0.5f)
583 float th = acosf(2*powf(1 - SMOOTH_CIRCLE_ERROR_RATE/outerRadius, 2) - 1);
584 segments = (int)((endAngle - startAngle)*ceilf(2*PI/th)/360);
585
586 if (segments <= 0) segments = minSegments;
587 }
588
589 // Not a ring
590 if (innerRadius <= 0.0f)
591 {
592 DrawCircleSector(center, outerRadius, startAngle, endAngle, segments, color);
593 return;
594 }
595
596 float stepLength = (endAngle - startAngle)/(float)segments;
597 float angle = startAngle;
598
599#if defined(SUPPORT_QUADS_DRAW_MODE)
600 rlSetTexture(GetShapesTexture().id);
601 Rectangle shapeRect = GetShapesTextureRectangle();
602
603 rlBegin(RL_QUADS);
604 for (int i = 0; i < segments; i++)
605 {
606 rlColor4ub(color.r, color.g, color.b, color.a);
607
608 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
609 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
610
611 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
612 rlVertex2f(center.x + cosf(DEG2RAD*angle)*innerRadius, center.y + sinf(DEG2RAD*angle)*innerRadius);
613
614 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
615 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*innerRadius);
616
617 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
618 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*outerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*outerRadius);
619
620 angle += stepLength;
621 }
622 rlEnd();
623
624 rlSetTexture(0);
625#else
626 rlBegin(RL_TRIANGLES);
627 for (int i = 0; i < segments; i++)
628 {
629 rlColor4ub(color.r, color.g, color.b, color.a);
630
631 rlVertex2f(center.x + cosf(DEG2RAD*angle)*innerRadius, center.y + sinf(DEG2RAD*angle)*innerRadius);
632 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*innerRadius);
633 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
634
635 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*innerRadius);
636 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*outerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*outerRadius);
637 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
638
639 angle += stepLength;
640 }
641 rlEnd();
642#endif
643}
644
645// Draw ring outline
646void DrawRingLines(Vector2 center, float innerRadius, float outerRadius, float startAngle, float endAngle, int segments, Color color)
647{
648 if (startAngle == endAngle) return;
649
650 // Function expects (outerRadius > innerRadius)
651 if (outerRadius < innerRadius)
652 {
653 float tmp = outerRadius;
654 outerRadius = innerRadius;
655 innerRadius = tmp;
656
657 if (outerRadius <= 0.0f) outerRadius = 0.1f;
658 }
659
660 // Function expects (endAngle > startAngle)
661 if (endAngle < startAngle)
662 {
663 // Swap values
664 float tmp = startAngle;
665 startAngle = endAngle;
666 endAngle = tmp;
667 }
668
669 int minSegments = (int)ceilf((endAngle - startAngle)/90);
670
671 if (segments < minSegments)
672 {
673 // Calculate the maximum angle between segments based on the error rate (usually 0.5f)
674 float th = acosf(2*powf(1 - SMOOTH_CIRCLE_ERROR_RATE/outerRadius, 2) - 1);
675 segments = (int)((endAngle - startAngle)*ceilf(2*PI/th)/360);
676
677 if (segments <= 0) segments = minSegments;
678 }
679
680 if (innerRadius <= 0.0f)
681 {
682 DrawCircleSectorLines(center, outerRadius, startAngle, endAngle, segments, color);
683 return;
684 }
685
686 float stepLength = (endAngle - startAngle)/(float)segments;
687 float angle = startAngle;
688 bool showCapLines = true;
689
690 rlBegin(RL_LINES);
691 if (showCapLines)
692 {
693 rlColor4ub(color.r, color.g, color.b, color.a);
694 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
695 rlVertex2f(center.x + cosf(DEG2RAD*angle)*innerRadius, center.y + sinf(DEG2RAD*angle)*innerRadius);
696 }
697
698 for (int i = 0; i < segments; i++)
699 {
700 rlColor4ub(color.r, color.g, color.b, color.a);
701
702 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
703 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*outerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*outerRadius);
704
705 rlVertex2f(center.x + cosf(DEG2RAD*angle)*innerRadius, center.y + sinf(DEG2RAD*angle)*innerRadius);
706 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*innerRadius);
707
708 angle += stepLength;
709 }
710
711 if (showCapLines)
712 {
713 rlColor4ub(color.r, color.g, color.b, color.a);
714 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
715 rlVertex2f(center.x + cosf(DEG2RAD*angle)*innerRadius, center.y + sinf(DEG2RAD*angle)*innerRadius);
716 }
717 rlEnd();
718}
719
720// Draw a color-filled rectangle
721void DrawRectangle(int posX, int posY, int width, int height, Color color)
722{
723 DrawRectangleV((Vector2){ (float)posX, (float)posY }, (Vector2){ (float)width, (float)height }, color);
724}
725
726// Draw a color-filled rectangle (Vector version)
727// NOTE: On OpenGL 3.3 and ES2 we use QUADS to avoid drawing order issues
728void DrawRectangleV(Vector2 position, Vector2 size, Color color)
729{
730 DrawRectanglePro((Rectangle){ position.x, position.y, size.x, size.y }, (Vector2){ 0.0f, 0.0f }, 0.0f, color);
731}
732
733// Draw a color-filled rectangle
734void DrawRectangleRec(Rectangle rec, Color color)
735{
736 DrawRectanglePro(rec, (Vector2){ 0.0f, 0.0f }, 0.0f, color);
737}
738
739// Draw a color-filled rectangle with pro parameters
740void DrawRectanglePro(Rectangle rec, Vector2 origin, float rotation, Color color)
741{
742 Vector2 topLeft = { 0 };
743 Vector2 topRight = { 0 };
744 Vector2 bottomLeft = { 0 };
745 Vector2 bottomRight = { 0 };
746
747 // Only calculate rotation if needed
748 if (rotation == 0.0f)
749 {
750 float x = rec.x - origin.x;
751 float y = rec.y - origin.y;
752 topLeft = (Vector2){ x, y };
753 topRight = (Vector2){ x + rec.width, y };
754 bottomLeft = (Vector2){ x, y + rec.height };
755 bottomRight = (Vector2){ x + rec.width, y + rec.height };
756 }
757 else
758 {
759 float sinRotation = sinf(rotation*DEG2RAD);
760 float cosRotation = cosf(rotation*DEG2RAD);
761 float x = rec.x;
762 float y = rec.y;
763 float dx = -origin.x;
764 float dy = -origin.y;
765
766 topLeft.x = x + dx*cosRotation - dy*sinRotation;
767 topLeft.y = y + dx*sinRotation + dy*cosRotation;
768
769 topRight.x = x + (dx + rec.width)*cosRotation - dy*sinRotation;
770 topRight.y = y + (dx + rec.width)*sinRotation + dy*cosRotation;
771
772 bottomLeft.x = x + dx*cosRotation - (dy + rec.height)*sinRotation;
773 bottomLeft.y = y + dx*sinRotation + (dy + rec.height)*cosRotation;
774
775 bottomRight.x = x + (dx + rec.width)*cosRotation - (dy + rec.height)*sinRotation;
776 bottomRight.y = y + (dx + rec.width)*sinRotation + (dy + rec.height)*cosRotation;
777 }
778
779#if defined(SUPPORT_QUADS_DRAW_MODE)
780 rlSetTexture(GetShapesTexture().id);
781 Rectangle shapeRect = GetShapesTextureRectangle();
782
783 rlBegin(RL_QUADS);
784
785 rlNormal3f(0.0f, 0.0f, 1.0f);
786 rlColor4ub(color.r, color.g, color.b, color.a);
787
788 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
789 rlVertex2f(topLeft.x, topLeft.y);
790
791 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
792 rlVertex2f(bottomLeft.x, bottomLeft.y);
793
794 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
795 rlVertex2f(bottomRight.x, bottomRight.y);
796
797 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
798 rlVertex2f(topRight.x, topRight.y);
799
800 rlEnd();
801
802 rlSetTexture(0);
803#else
804 rlBegin(RL_TRIANGLES);
805
806 rlColor4ub(color.r, color.g, color.b, color.a);
807
808 rlVertex2f(topLeft.x, topLeft.y);
809 rlVertex2f(bottomLeft.x, bottomLeft.y);
810 rlVertex2f(topRight.x, topRight.y);
811
812 rlVertex2f(topRight.x, topRight.y);
813 rlVertex2f(bottomLeft.x, bottomLeft.y);
814 rlVertex2f(bottomRight.x, bottomRight.y);
815
816 rlEnd();
817#endif
818}
819
820// Draw a vertical-gradient-filled rectangle
821void DrawRectangleGradientV(int posX, int posY, int width, int height, Color top, Color bottom)
822{
823 DrawRectangleGradientEx((Rectangle){ (float)posX, (float)posY, (float)width, (float)height }, top, bottom, bottom, top);
824}
825
826// Draw a horizontal-gradient-filled rectangle
827void DrawRectangleGradientH(int posX, int posY, int width, int height, Color left, Color right)
828{
829 DrawRectangleGradientEx((Rectangle){ (float)posX, (float)posY, (float)width, (float)height }, left, left, right, right);
830}
831
832// Draw a gradient-filled rectangle
833void DrawRectangleGradientEx(Rectangle rec, Color topLeft, Color bottomLeft, Color bottomRight, Color topRight)
834{
835 rlSetTexture(GetShapesTexture().id);
836 Rectangle shapeRect = GetShapesTextureRectangle();
837
838 rlBegin(RL_QUADS);
839 rlNormal3f(0.0f, 0.0f, 1.0f);
840
841 // NOTE: Default raylib font character 95 is a white square
842 rlColor4ub(topLeft.r, topLeft.g, topLeft.b, topLeft.a);
843 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
844 rlVertex2f(rec.x, rec.y);
845
846 rlColor4ub(bottomLeft.r, bottomLeft.g, bottomLeft.b, bottomLeft.a);
847 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
848 rlVertex2f(rec.x, rec.y + rec.height);
849
850 rlColor4ub(bottomRight.r, bottomRight.g, bottomRight.b, bottomRight.a);
851 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
852 rlVertex2f(rec.x + rec.width, rec.y + rec.height);
853
854 rlColor4ub(topRight.r, topRight.g, topRight.b, topRight.a);
855 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
856 rlVertex2f(rec.x + rec.width, rec.y);
857 rlEnd();
858
859 rlSetTexture(0);
860}
861
862// Draw rectangle outline
863// WARNING: All Draw*Lines() functions use RL_LINES for drawing,
864// it implies flushing the current batch and changing draw mode to RL_LINES
865// but it solves another issue: https://github.com/raysan5/raylib/issues/3884
866void DrawRectangleLines(int posX, int posY, int width, int height, Color color)
867{
868 Matrix mat = rlGetMatrixTransform();
869 float xOffset = 0.5f/mat.m0;
870 float yOffset = 0.5f/mat.m5;
871
872 rlBegin(RL_LINES);
873 rlColor4ub(color.r, color.g, color.b, color.a);
874 rlVertex2f((float)posX + xOffset, (float)posY + yOffset);
875 rlVertex2f((float)posX + (float)width - xOffset, (float)posY + yOffset);
876
877 rlVertex2f((float)posX + (float)width - xOffset, (float)posY + yOffset);
878 rlVertex2f((float)posX + (float)width - xOffset, (float)posY + (float)height - yOffset);
879
880 rlVertex2f((float)posX + (float)width - xOffset, (float)posY + (float)height - yOffset);
881 rlVertex2f((float)posX + xOffset, (float)posY + (float)height - yOffset);
882
883 rlVertex2f((float)posX + xOffset, (float)posY + (float)height - yOffset);
884 rlVertex2f((float)posX + xOffset, (float)posY + yOffset);
885 rlEnd();
886
887/*
888// Previous implementation, it has issues... but it does not require view matrix...
889#if defined(SUPPORT_QUADS_DRAW_MODE)
890 DrawRectangle(posX, posY, width, 1, color);
891 DrawRectangle(posX + width - 1, posY + 1, 1, height - 2, color);
892 DrawRectangle(posX, posY + height - 1, width, 1, color);
893 DrawRectangle(posX, posY + 1, 1, height - 2, color);
894#else
895 rlBegin(RL_LINES);
896 rlColor4ub(color.r, color.g, color.b, color.a);
897 rlVertex2f((float)posX, (float)posY);
898 rlVertex2f((float)posX + (float)width, (float)posY + 1);
899
900 rlVertex2f((float)posX + (float)width, (float)posY + 1);
901 rlVertex2f((float)posX + (float)width, (float)posY + (float)height);
902
903 rlVertex2f((float)posX + (float)width, (float)posY + (float)height);
904 rlVertex2f((float)posX + 1, (float)posY + (float)height);
905
906 rlVertex2f((float)posX + 1, (float)posY + (float)height);
907 rlVertex2f((float)posX + 1, (float)posY + 1);
908 rlEnd();
909#endif
910*/
911}
912
913// Draw rectangle outline with extended parameters
914void DrawRectangleLinesEx(Rectangle rec, float lineThick, Color color)
915{
916 if ((lineThick > rec.width) || (lineThick > rec.height))
917 {
918 if (rec.width >= rec.height) lineThick = rec.height/2;
919 else if (rec.width <= rec.height) lineThick = rec.width/2;
920 }
921
922 // When rec = { x, y, 8.0f, 6.0f } and lineThick = 2, the following
923 // four rectangles are drawn ([T]op, [B]ottom, [L]eft, [R]ight):
924 //
925 // TTTTTTTT
926 // TTTTTTTT
927 // LL RR
928 // LL RR
929 // BBBBBBBB
930 // BBBBBBBB
931 //
932
933 Rectangle top = { rec.x, rec.y, rec.width, lineThick };
934 Rectangle bottom = { rec.x, rec.y - lineThick + rec.height, rec.width, lineThick };
935 Rectangle left = { rec.x, rec.y + lineThick, lineThick, rec.height - lineThick*2.0f };
936 Rectangle right = { rec.x - lineThick + rec.width, rec.y + lineThick, lineThick, rec.height - lineThick*2.0f };
937
938 DrawRectangleRec(top, color);
939 DrawRectangleRec(bottom, color);
940 DrawRectangleRec(left, color);
941 DrawRectangleRec(right, color);
942}
943
944// Draw rectangle with rounded edges
945void DrawRectangleRounded(Rectangle rec, float roundness, int segments, Color color)
946{
947 // Not a rounded rectangle
948 if (roundness <= 0.0f)
949 {
950 DrawRectangleRec(rec, color);
951 return;
952 }
953
954 if (roundness >= 1.0f) roundness = 1.0f;
955
956 // Calculate corner radius
957 float radius = (rec.width > rec.height)? (rec.height*roundness)/2 : (rec.width*roundness)/2;
958 if (radius <= 0.0f) return;
959
960 // Calculate number of segments to use for the corners
961 if (segments < 4)
962 {
963 // Calculate the maximum angle between segments based on the error rate (usually 0.5f)
964 float th = acosf(2*powf(1 - SMOOTH_CIRCLE_ERROR_RATE/radius, 2) - 1);
965 segments = (int)(ceilf(2*PI/th)/4.0f);
966 if (segments <= 0) segments = 4;
967 }
968
969 float stepLength = 90.0f/(float)segments;
970
971 /*
972 Quick sketch to make sense of all of this,
973 there are 9 parts to draw, also mark the 12 points we'll use
974
975 P0____________________P1
976 /| |\
977 /1| 2 |3\
978 P7 /__|____________________|__\ P2
979 | |P8 P9| |
980 | 8 | 9 | 4 |
981 | __|____________________|__ |
982 P6 \ |P11 P10| / P3
983 \7| 6 |5/
984 \|____________________|/
985 P5 P4
986 */
987 // Coordinates of the 12 points that define the rounded rect
988 const Vector2 point[12] = {
989 {(float)rec.x + radius, rec.y}, {(float)(rec.x + rec.width) - radius, rec.y}, { rec.x + rec.width, (float)rec.y + radius }, // PO, P1, P2
990 {rec.x + rec.width, (float)(rec.y + rec.height) - radius}, {(float)(rec.x + rec.width) - radius, rec.y + rec.height}, // P3, P4
991 {(float)rec.x + radius, rec.y + rec.height}, { rec.x, (float)(rec.y + rec.height) - radius}, {rec.x, (float)rec.y + radius}, // P5, P6, P7
992 {(float)rec.x + radius, (float)rec.y + radius}, {(float)(rec.x + rec.width) - radius, (float)rec.y + radius}, // P8, P9
993 {(float)(rec.x + rec.width) - radius, (float)(rec.y + rec.height) - radius}, {(float)rec.x + radius, (float)(rec.y + rec.height) - radius} // P10, P11
994 };
995
996 const Vector2 centers[4] = { point[8], point[9], point[10], point[11] };
997 const float angles[4] = { 180.0f, 270.0f, 0.0f, 90.0f };
998
999#if defined(SUPPORT_QUADS_DRAW_MODE)
1000 rlSetTexture(GetShapesTexture().id);
1001 Rectangle shapeRect = GetShapesTextureRectangle();
1002
1003 rlBegin(RL_QUADS);
1004 // Draw all the 4 corners: [1] Upper Left Corner, [3] Upper Right Corner, [5] Lower Right Corner, [7] Lower Left Corner
1005 for (int k = 0; k < 4; ++k) // Hope the compiler is smart enough to unroll this loop
1006 {
1007 float angle = angles[k];
1008 const Vector2 center = centers[k];
1009
1010 // NOTE: Every QUAD actually represents two segments
1011 for (int i = 0; i < segments/2; i++)
1012 {
1013 rlColor4ub(color.r, color.g, color.b, color.a);
1014 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
1015 rlVertex2f(center.x, center.y);
1016
1017 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
1018 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength*2))*radius, center.y + sinf(DEG2RAD*(angle + stepLength*2))*radius);
1019
1020 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1021 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*radius, center.y + sinf(DEG2RAD*(angle + stepLength))*radius);
1022
1023 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1024 rlVertex2f(center.x + cosf(DEG2RAD*angle)*radius, center.y + sinf(DEG2RAD*angle)*radius);
1025
1026 angle += (stepLength*2);
1027 }
1028
1029 // NOTE: In case number of segments is odd, we add one last piece to the cake
1030 if (segments%2)
1031 {
1032 rlColor4ub(color.r, color.g, color.b, color.a);
1033 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
1034 rlVertex2f(center.x, center.y);
1035
1036 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1037 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*radius, center.y + sinf(DEG2RAD*(angle + stepLength))*radius);
1038
1039 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1040 rlVertex2f(center.x + cosf(DEG2RAD*angle)*radius, center.y + sinf(DEG2RAD*angle)*radius);
1041
1042 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
1043 rlVertex2f(center.x, center.y);
1044 }
1045 }
1046
1047 // [2] Upper Rectangle
1048 rlColor4ub(color.r, color.g, color.b, color.a);
1049 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
1050 rlVertex2f(point[0].x, point[0].y);
1051 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1052 rlVertex2f(point[8].x, point[8].y);
1053 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1054 rlVertex2f(point[9].x, point[9].y);
1055 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
1056 rlVertex2f(point[1].x, point[1].y);
1057
1058 // [4] Right Rectangle
1059 rlColor4ub(color.r, color.g, color.b, color.a);
1060 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
1061 rlVertex2f(point[2].x, point[2].y);
1062 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1063 rlVertex2f(point[9].x, point[9].y);
1064 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1065 rlVertex2f(point[10].x, point[10].y);
1066 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
1067 rlVertex2f(point[3].x, point[3].y);
1068
1069 // [6] Bottom Rectangle
1070 rlColor4ub(color.r, color.g, color.b, color.a);
1071 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
1072 rlVertex2f(point[11].x, point[11].y);
1073 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1074 rlVertex2f(point[5].x, point[5].y);
1075 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1076 rlVertex2f(point[4].x, point[4].y);
1077 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
1078 rlVertex2f(point[10].x, point[10].y);
1079
1080 // [8] Left Rectangle
1081 rlColor4ub(color.r, color.g, color.b, color.a);
1082 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
1083 rlVertex2f(point[7].x, point[7].y);
1084 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1085 rlVertex2f(point[6].x, point[6].y);
1086 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1087 rlVertex2f(point[11].x, point[11].y);
1088 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
1089 rlVertex2f(point[8].x, point[8].y);
1090
1091 // [9] Middle Rectangle
1092 rlColor4ub(color.r, color.g, color.b, color.a);
1093 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
1094 rlVertex2f(point[8].x, point[8].y);
1095 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1096 rlVertex2f(point[11].x, point[11].y);
1097 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1098 rlVertex2f(point[10].x, point[10].y);
1099 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
1100 rlVertex2f(point[9].x, point[9].y);
1101
1102 rlEnd();
1103 rlSetTexture(0);
1104#else
1105 rlBegin(RL_TRIANGLES);
1106
1107 // Draw all of the 4 corners: [1] Upper Left Corner, [3] Upper Right Corner, [5] Lower Right Corner, [7] Lower Left Corner
1108 for (int k = 0; k < 4; ++k) // Hope the compiler is smart enough to unroll this loop
1109 {
1110 float angle = angles[k];
1111 const Vector2 center = centers[k];
1112 for (int i = 0; i < segments; i++)
1113 {
1114 rlColor4ub(color.r, color.g, color.b, color.a);
1115 rlVertex2f(center.x, center.y);
1116 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*radius, center.y + sinf(DEG2RAD*(angle + stepLength))*radius);
1117 rlVertex2f(center.x + cosf(DEG2RAD*angle)*radius, center.y + sinf(DEG2RAD*angle)*radius);
1118 angle += stepLength;
1119 }
1120 }
1121
1122 // [2] Upper Rectangle
1123 rlColor4ub(color.r, color.g, color.b, color.a);
1124 rlVertex2f(point[0].x, point[0].y);
1125 rlVertex2f(point[8].x, point[8].y);
1126 rlVertex2f(point[9].x, point[9].y);
1127 rlVertex2f(point[1].x, point[1].y);
1128 rlVertex2f(point[0].x, point[0].y);
1129 rlVertex2f(point[9].x, point[9].y);
1130
1131 // [4] Right Rectangle
1132 rlColor4ub(color.r, color.g, color.b, color.a);
1133 rlVertex2f(point[9].x, point[9].y);
1134 rlVertex2f(point[10].x, point[10].y);
1135 rlVertex2f(point[3].x, point[3].y);
1136 rlVertex2f(point[2].x, point[2].y);
1137 rlVertex2f(point[9].x, point[9].y);
1138 rlVertex2f(point[3].x, point[3].y);
1139
1140 // [6] Bottom Rectangle
1141 rlColor4ub(color.r, color.g, color.b, color.a);
1142 rlVertex2f(point[11].x, point[11].y);
1143 rlVertex2f(point[5].x, point[5].y);
1144 rlVertex2f(point[4].x, point[4].y);
1145 rlVertex2f(point[10].x, point[10].y);
1146 rlVertex2f(point[11].x, point[11].y);
1147 rlVertex2f(point[4].x, point[4].y);
1148
1149 // [8] Left Rectangle
1150 rlColor4ub(color.r, color.g, color.b, color.a);
1151 rlVertex2f(point[7].x, point[7].y);
1152 rlVertex2f(point[6].x, point[6].y);
1153 rlVertex2f(point[11].x, point[11].y);
1154 rlVertex2f(point[8].x, point[8].y);
1155 rlVertex2f(point[7].x, point[7].y);
1156 rlVertex2f(point[11].x, point[11].y);
1157
1158 // [9] Middle Rectangle
1159 rlColor4ub(color.r, color.g, color.b, color.a);
1160 rlVertex2f(point[8].x, point[8].y);
1161 rlVertex2f(point[11].x, point[11].y);
1162 rlVertex2f(point[10].x, point[10].y);
1163 rlVertex2f(point[9].x, point[9].y);
1164 rlVertex2f(point[8].x, point[8].y);
1165 rlVertex2f(point[10].x, point[10].y);
1166 rlEnd();
1167#endif
1168}
1169
1170// Draw rectangle with rounded edges
1171void DrawRectangleRoundedLines(Rectangle rec, float roundness, int segments, Color color)
1172{
1173 // NOTE: For line thicknes <=1.0f we use RL_LINES, otherwise wee use RL_QUADS/RL_TRIANGLES
1174 DrawRectangleRoundedLinesEx(rec, roundness, segments, 1.0f, color);
1175}
1176
1177// Draw rectangle with rounded edges outline
1178void DrawRectangleRoundedLinesEx(Rectangle rec, float roundness, int segments, float lineThick, Color color)
1179{
1180 if (lineThick < 0) lineThick = 0;
1181
1182 // Not a rounded rectangle
1183 if (roundness <= 0.0f)
1184 {
1185 DrawRectangleLinesEx((Rectangle){rec.x-lineThick, rec.y-lineThick, rec.width+2*lineThick, rec.height+2*lineThick}, lineThick, color);
1186 return;
1187 }
1188
1189 if (roundness >= 1.0f) roundness = 1.0f;
1190
1191 // Calculate corner radius
1192 float radius = (rec.width > rec.height)? (rec.height*roundness)/2 : (rec.width*roundness)/2;
1193 if (radius <= 0.0f) return;
1194
1195 // Calculate number of segments to use for the corners
1196 if (segments < 4)
1197 {
1198 // Calculate the maximum angle between segments based on the error rate (usually 0.5f)
1199 float th = acosf(2*powf(1 - SMOOTH_CIRCLE_ERROR_RATE/radius, 2) - 1);
1200 segments = (int)(ceilf(2*PI/th)/2.0f);
1201 if (segments <= 0) segments = 4;
1202 }
1203
1204 float stepLength = 90.0f/(float)segments;
1205 const float outerRadius = radius + lineThick, innerRadius = radius;
1206
1207 /*
1208 Quick sketch to make sense of all of this,
1209 marks the 16 + 4(corner centers P16-19) points we'll use
1210
1211 P0 ================== P1
1212 // P8 P9 \\
1213 // \\
1214 P7 // P15 P10 \\ P2
1215 || *P16 P17* ||
1216 || ||
1217 || P14 P11 ||
1218 P6 \\ *P19 P18* // P3
1219 \\ //
1220 \\ P13 P12 //
1221 P5 ================== P4
1222 */
1223 const Vector2 point[16] = {
1224 {(float)rec.x + innerRadius + 0.5f, rec.y - lineThick + 0.5f},
1225 {(float)(rec.x + rec.width) - innerRadius - 0.5f, rec.y - lineThick + 0.5f},
1226 {rec.x + rec.width + lineThick - 0.5f, (float)rec.y + innerRadius + 0.5f}, // PO, P1, P2
1227 {rec.x + rec.width + lineThick - 0.5f, (float)(rec.y + rec.height) - innerRadius - 0.5f},
1228 {(float)(rec.x + rec.width) - innerRadius - 0.5f, rec.y + rec.height + lineThick - 0.5f}, // P3, P4
1229 {(float)rec.x + innerRadius + 0.5f, rec.y + rec.height + lineThick - 0.5f},
1230 {rec.x - lineThick + 0.5f, (float)(rec.y + rec.height) - innerRadius - 0.5f},
1231 {rec.x - lineThick + 0.5f, (float)rec.y + innerRadius + 0.5f}, // P5, P6, P7
1232 {(float)rec.x + innerRadius + 0.5f, rec.y + 0.5f},
1233 {(float)(rec.x + rec.width) - innerRadius - 0.5f, rec.y + 0.5f}, // P8, P9
1234 {rec.x + rec.width - 0.5f, (float)rec.y + innerRadius + 0.5f},
1235 {rec.x + rec.width - 0.5f, (float)(rec.y + rec.height) - innerRadius - 0.5f}, // P10, P11
1236 {(float)(rec.x + rec.width) - innerRadius - 0.5f, rec.y + rec.height - 0.5f},
1237 {(float)rec.x + innerRadius + 0.5f, rec.y + rec.height - 0.5f}, // P12, P13
1238 {rec.x + 0.5f, (float)(rec.y + rec.height) - innerRadius - 0.5f},
1239 {rec.x + 0.5f, (float)rec.y + innerRadius + 0.5f} // P14, P15
1240 };
1241
1242 const Vector2 centers[4] = {
1243 {(float)rec.x + innerRadius + 0.5f, (float)rec.y + innerRadius + 0.5f},
1244 {(float)(rec.x + rec.width) - innerRadius - 0.5f, (float)rec.y + innerRadius + 0.5f}, // P16, P17
1245 {(float)(rec.x + rec.width) - innerRadius - 0.5f, (float)(rec.y + rec.height) - innerRadius - 0.5f},
1246 {(float)rec.x + innerRadius + 0.5f, (float)(rec.y + rec.height) - innerRadius - 0.5f} // P18, P19
1247 };
1248
1249 const float angles[4] = { 180.0f, 270.0f, 0.0f, 90.0f };
1250
1251 if (lineThick > 1)
1252 {
1253#if defined(SUPPORT_QUADS_DRAW_MODE)
1254 rlSetTexture(GetShapesTexture().id);
1255 Rectangle shapeRect = GetShapesTextureRectangle();
1256
1257 rlBegin(RL_QUADS);
1258
1259 // Draw all the 4 corners first: Upper Left Corner, Upper Right Corner, Lower Right Corner, Lower Left Corner
1260 for (int k = 0; k < 4; ++k) // Hope the compiler is smart enough to unroll this loop
1261 {
1262 float angle = angles[k];
1263 const Vector2 center = centers[k];
1264 for (int i = 0; i < segments; i++)
1265 {
1266 rlColor4ub(color.r, color.g, color.b, color.a);
1267
1268 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
1269 rlVertex2f(center.x + cosf(DEG2RAD*angle)*innerRadius, center.y + sinf(DEG2RAD*angle)*innerRadius);
1270
1271 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
1272 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*innerRadius);
1273
1274 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1275 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*outerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*outerRadius);
1276
1277 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1278 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
1279
1280 angle += stepLength;
1281 }
1282 }
1283
1284 // Upper rectangle
1285 rlColor4ub(color.r, color.g, color.b, color.a);
1286 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
1287 rlVertex2f(point[0].x, point[0].y);
1288 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1289 rlVertex2f(point[8].x, point[8].y);
1290 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1291 rlVertex2f(point[9].x, point[9].y);
1292 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
1293 rlVertex2f(point[1].x, point[1].y);
1294
1295 // Right rectangle
1296 rlColor4ub(color.r, color.g, color.b, color.a);
1297 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
1298 rlVertex2f(point[2].x, point[2].y);
1299 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1300 rlVertex2f(point[10].x, point[10].y);
1301 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1302 rlVertex2f(point[11].x, point[11].y);
1303 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
1304 rlVertex2f(point[3].x, point[3].y);
1305
1306 // Lower rectangle
1307 rlColor4ub(color.r, color.g, color.b, color.a);
1308 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
1309 rlVertex2f(point[13].x, point[13].y);
1310 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1311 rlVertex2f(point[5].x, point[5].y);
1312 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1313 rlVertex2f(point[4].x, point[4].y);
1314 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
1315 rlVertex2f(point[12].x, point[12].y);
1316
1317 // Left rectangle
1318 rlColor4ub(color.r, color.g, color.b, color.a);
1319 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
1320 rlVertex2f(point[15].x, point[15].y);
1321 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1322 rlVertex2f(point[7].x, point[7].y);
1323 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1324 rlVertex2f(point[6].x, point[6].y);
1325 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
1326 rlVertex2f(point[14].x, point[14].y);
1327
1328 rlEnd();
1329 rlSetTexture(0);
1330#else
1331 rlBegin(RL_TRIANGLES);
1332
1333 // Draw all of the 4 corners first: Upper Left Corner, Upper Right Corner, Lower Right Corner, Lower Left Corner
1334 for (int k = 0; k < 4; ++k) // Hope the compiler is smart enough to unroll this loop
1335 {
1336 float angle = angles[k];
1337 const Vector2 center = centers[k];
1338
1339 for (int i = 0; i < segments; i++)
1340 {
1341 rlColor4ub(color.r, color.g, color.b, color.a);
1342
1343 rlVertex2f(center.x + cosf(DEG2RAD*angle)*innerRadius, center.y + sinf(DEG2RAD*angle)*innerRadius);
1344 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*innerRadius);
1345 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
1346
1347 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*innerRadius);
1348 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*outerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*outerRadius);
1349 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
1350
1351 angle += stepLength;
1352 }
1353 }
1354
1355 // Upper rectangle
1356 rlColor4ub(color.r, color.g, color.b, color.a);
1357 rlVertex2f(point[0].x, point[0].y);
1358 rlVertex2f(point[8].x, point[8].y);
1359 rlVertex2f(point[9].x, point[9].y);
1360 rlVertex2f(point[1].x, point[1].y);
1361 rlVertex2f(point[0].x, point[0].y);
1362 rlVertex2f(point[9].x, point[9].y);
1363
1364 // Right rectangle
1365 rlColor4ub(color.r, color.g, color.b, color.a);
1366 rlVertex2f(point[10].x, point[10].y);
1367 rlVertex2f(point[11].x, point[11].y);
1368 rlVertex2f(point[3].x, point[3].y);
1369 rlVertex2f(point[2].x, point[2].y);
1370 rlVertex2f(point[10].x, point[10].y);
1371 rlVertex2f(point[3].x, point[3].y);
1372
1373 // Lower rectangle
1374 rlColor4ub(color.r, color.g, color.b, color.a);
1375 rlVertex2f(point[13].x, point[13].y);
1376 rlVertex2f(point[5].x, point[5].y);
1377 rlVertex2f(point[4].x, point[4].y);
1378 rlVertex2f(point[12].x, point[12].y);
1379 rlVertex2f(point[13].x, point[13].y);
1380 rlVertex2f(point[4].x, point[4].y);
1381
1382 // Left rectangle
1383 rlColor4ub(color.r, color.g, color.b, color.a);
1384 rlVertex2f(point[7].x, point[7].y);
1385 rlVertex2f(point[6].x, point[6].y);
1386 rlVertex2f(point[14].x, point[14].y);
1387 rlVertex2f(point[15].x, point[15].y);
1388 rlVertex2f(point[7].x, point[7].y);
1389 rlVertex2f(point[14].x, point[14].y);
1390 rlEnd();
1391#endif
1392 }
1393 else
1394 {
1395 // Use LINES to draw the outline
1396 rlBegin(RL_LINES);
1397 // Draw all the 4 corners first: Upper Left Corner, Upper Right Corner, Lower Right Corner, Lower Left Corner
1398 for (int k = 0; k < 4; ++k) // Hope the compiler is smart enough to unroll this loop
1399 {
1400 float angle = angles[k];
1401 const Vector2 center = centers[k];
1402
1403 for (int i = 0; i < segments; i++)
1404 {
1405 rlColor4ub(color.r, color.g, color.b, color.a);
1406 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
1407 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*outerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*outerRadius);
1408 angle += stepLength;
1409 }
1410 }
1411
1412 // And now the remaining 4 lines
1413 for (int i = 0; i < 8; i += 2)
1414 {
1415 rlColor4ub(color.r, color.g, color.b, color.a);
1416 rlVertex2f(point[i].x, point[i].y);
1417 rlVertex2f(point[i + 1].x, point[i + 1].y);
1418 }
1419 rlEnd();
1420 }
1421}
1422
1423// Draw a triangle
1424// NOTE: Vertex must be provided in counter-clockwise order
1425void DrawTriangle(Vector2 v1, Vector2 v2, Vector2 v3, Color color)
1426{
1427#if defined(SUPPORT_QUADS_DRAW_MODE)
1428 rlSetTexture(GetShapesTexture().id);
1429 Rectangle shapeRect = GetShapesTextureRectangle();
1430
1431 rlBegin(RL_QUADS);
1432 rlNormal3f(0.0f, 0.0f, 1.0f);
1433 rlColor4ub(color.r, color.g, color.b, color.a);
1434
1435 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
1436 rlVertex2f(v1.x, v1.y);
1437
1438 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1439 rlVertex2f(v2.x, v2.y);
1440
1441 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1442 rlVertex2f(v3.x, v3.y);
1443
1444 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
1445 rlVertex2f(v3.x, v3.y);
1446 rlEnd();
1447
1448 rlSetTexture(0);
1449#else
1450 rlBegin(RL_TRIANGLES);
1451 rlColor4ub(color.r, color.g, color.b, color.a);
1452 rlVertex2f(v1.x, v1.y);
1453 rlVertex2f(v2.x, v2.y);
1454 rlVertex2f(v3.x, v3.y);
1455 rlEnd();
1456#endif
1457}
1458
1459// Draw a triangle using lines
1460// NOTE: Vertex must be provided in counter-clockwise order
1461void DrawTriangleLines(Vector2 v1, Vector2 v2, Vector2 v3, Color color)
1462{
1463 rlBegin(RL_LINES);
1464 rlColor4ub(color.r, color.g, color.b, color.a);
1465 rlVertex2f(v1.x, v1.y);
1466 rlVertex2f(v2.x, v2.y);
1467
1468 rlVertex2f(v2.x, v2.y);
1469 rlVertex2f(v3.x, v3.y);
1470
1471 rlVertex2f(v3.x, v3.y);
1472 rlVertex2f(v1.x, v1.y);
1473 rlEnd();
1474}
1475
1476// Draw a triangle fan defined by points
1477// NOTE: First vertex provided is the center, shared by all triangles
1478// By default, following vertex should be provided in counter-clockwise order
1479void DrawTriangleFan(const Vector2 *points, int pointCount, Color color)
1480{
1481 if (pointCount >= 3)
1482 {
1483 rlSetTexture(GetShapesTexture().id);
1484 Rectangle shapeRect = GetShapesTextureRectangle();
1485
1486 rlBegin(RL_QUADS);
1487 rlColor4ub(color.r, color.g, color.b, color.a);
1488
1489 for (int i = 1; i < pointCount - 1; i++)
1490 {
1491 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
1492 rlVertex2f(points[0].x, points[0].y);
1493
1494 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1495 rlVertex2f(points[i].x, points[i].y);
1496
1497 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1498 rlVertex2f(points[i + 1].x, points[i + 1].y);
1499
1500 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
1501 rlVertex2f(points[i + 1].x, points[i + 1].y);
1502 }
1503 rlEnd();
1504 rlSetTexture(0);
1505 }
1506}
1507
1508// Draw a triangle strip defined by points
1509// NOTE: Every new vertex connects with previous two
1510void DrawTriangleStrip(const Vector2 *points, int pointCount, Color color)
1511{
1512 if (pointCount >= 3)
1513 {
1514 rlBegin(RL_TRIANGLES);
1515 rlColor4ub(color.r, color.g, color.b, color.a);
1516
1517 for (int i = 2; i < pointCount; i++)
1518 {
1519 if ((i%2) == 0)
1520 {
1521 rlVertex2f(points[i].x, points[i].y);
1522 rlVertex2f(points[i - 2].x, points[i - 2].y);
1523 rlVertex2f(points[i - 1].x, points[i - 1].y);
1524 }
1525 else
1526 {
1527 rlVertex2f(points[i].x, points[i].y);
1528 rlVertex2f(points[i - 1].x, points[i - 1].y);
1529 rlVertex2f(points[i - 2].x, points[i - 2].y);
1530 }
1531 }
1532 rlEnd();
1533 }
1534}
1535
1536// Draw a regular polygon of n sides (Vector version)
1537void DrawPoly(Vector2 center, int sides, float radius, float rotation, Color color)
1538{
1539 if (sides < 3) sides = 3;
1540 float centralAngle = rotation*DEG2RAD;
1541 float angleStep = 360.0f/(float)sides*DEG2RAD;
1542
1543#if defined(SUPPORT_QUADS_DRAW_MODE)
1544 rlSetTexture(GetShapesTexture().id);
1545 Rectangle shapeRect = GetShapesTextureRectangle();
1546
1547 rlBegin(RL_QUADS);
1548 for (int i = 0; i < sides; i++)
1549 {
1550 rlColor4ub(color.r, color.g, color.b, color.a);
1551 float nextAngle = centralAngle + angleStep;
1552
1553 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
1554 rlVertex2f(center.x, center.y);
1555
1556 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1557 rlVertex2f(center.x + cosf(centralAngle)*radius, center.y + sinf(centralAngle)*radius);
1558
1559 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
1560 rlVertex2f(center.x + cosf(nextAngle)*radius, center.y + sinf(nextAngle)*radius);
1561
1562 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1563 rlVertex2f(center.x + cosf(centralAngle)*radius, center.y + sinf(centralAngle)*radius);
1564
1565 centralAngle = nextAngle;
1566 }
1567 rlEnd();
1568 rlSetTexture(0);
1569#else
1570 rlBegin(RL_TRIANGLES);
1571 for (int i = 0; i < sides; i++)
1572 {
1573 rlColor4ub(color.r, color.g, color.b, color.a);
1574
1575 rlVertex2f(center.x, center.y);
1576 rlVertex2f(center.x + cosf(centralAngle + angleStep)*radius, center.y + sinf(centralAngle + angleStep)*radius);
1577 rlVertex2f(center.x + cosf(centralAngle)*radius, center.y + sinf(centralAngle)*radius);
1578
1579 centralAngle += angleStep;
1580 }
1581 rlEnd();
1582#endif
1583}
1584
1585// Draw a polygon outline of n sides
1586void DrawPolyLines(Vector2 center, int sides, float radius, float rotation, Color color)
1587{
1588 if (sides < 3) sides = 3;
1589 float centralAngle = rotation*DEG2RAD;
1590 float angleStep = 360.0f/(float)sides*DEG2RAD;
1591
1592 rlBegin(RL_LINES);
1593 for (int i = 0; i < sides; i++)
1594 {
1595 rlColor4ub(color.r, color.g, color.b, color.a);
1596
1597 rlVertex2f(center.x + cosf(centralAngle)*radius, center.y + sinf(centralAngle)*radius);
1598 rlVertex2f(center.x + cosf(centralAngle + angleStep)*radius, center.y + sinf(centralAngle + angleStep)*radius);
1599
1600 centralAngle += angleStep;
1601 }
1602 rlEnd();
1603}
1604
1605void DrawPolyLinesEx(Vector2 center, int sides, float radius, float rotation, float lineThick, Color color)
1606{
1607 if (sides < 3) sides = 3;
1608 float centralAngle = rotation*DEG2RAD;
1609 float exteriorAngle = 360.0f/(float)sides*DEG2RAD;
1610 float innerRadius = radius - (lineThick*cosf(DEG2RAD*exteriorAngle/2.0f));
1611
1612#if defined(SUPPORT_QUADS_DRAW_MODE)
1613 rlSetTexture(GetShapesTexture().id);
1614 Rectangle shapeRect = GetShapesTextureRectangle();
1615
1616 rlBegin(RL_QUADS);
1617 for (int i = 0; i < sides; i++)
1618 {
1619 rlColor4ub(color.r, color.g, color.b, color.a);
1620 float nextAngle = centralAngle + exteriorAngle;
1621
1622 rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1623 rlVertex2f(center.x + cosf(centralAngle)*radius, center.y + sinf(centralAngle)*radius);
1624
1625 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
1626 rlVertex2f(center.x + cosf(centralAngle)*innerRadius, center.y + sinf(centralAngle)*innerRadius);
1627
1628 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height);
1629 rlVertex2f(center.x + cosf(nextAngle)*innerRadius, center.y + sinf(nextAngle)*innerRadius);
1630
1631 rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height);
1632 rlVertex2f(center.x + cosf(nextAngle)*radius, center.y + sinf(nextAngle)*radius);
1633
1634 centralAngle = nextAngle;
1635 }
1636 rlEnd();
1637 rlSetTexture(0);
1638#else
1639 rlBegin(RL_TRIANGLES);
1640 for (int i = 0; i < sides; i++)
1641 {
1642 rlColor4ub(color.r, color.g, color.b, color.a);
1643 float nextAngle = centralAngle + exteriorAngle;
1644
1645 rlVertex2f(center.x + cosf(nextAngle)*radius, center.y + sinf(nextAngle)*radius);
1646 rlVertex2f(center.x + cosf(centralAngle)*radius, center.y + sinf(centralAngle)*radius);
1647 rlVertex2f(center.x + cosf(centralAngle)*innerRadius, center.y + sinf(centralAngle)*innerRadius);
1648
1649 rlVertex2f(center.x + cosf(centralAngle)*innerRadius, center.y + sinf(centralAngle)*innerRadius);
1650 rlVertex2f(center.x + cosf(nextAngle)*innerRadius, center.y + sinf(nextAngle)*innerRadius);
1651 rlVertex2f(center.x + cosf(nextAngle)*radius, center.y + sinf(nextAngle)*radius);
1652
1653 centralAngle = nextAngle;
1654 }
1655 rlEnd();
1656#endif
1657}
1658
1659//----------------------------------------------------------------------------------
1660// Module Functions Definition - Splines functions
1661//----------------------------------------------------------------------------------
1662
1663// Draw spline: linear, minimum 2 points
1664void DrawSplineLinear(const Vector2 *points, int pointCount, float thick, Color color)
1665{
1666 if (pointCount < 2) return;
1667
1668#if defined(SUPPORT_SPLINE_MITERS)
1669 Vector2 prevNormal = (Vector2){-(points[1].y - points[0].y), (points[1].x - points[0].x)};
1670 float prevLength = sqrtf(prevNormal.x*prevNormal.x + prevNormal.y*prevNormal.y);
1671
1672 if (prevLength > 0.0f)
1673 {
1674 prevNormal.x /= prevLength;
1675 prevNormal.y /= prevLength;
1676 }
1677 else
1678 {
1679 prevNormal.x = 0.0f;
1680 prevNormal.y = 0.0f;
1681 }
1682
1683 Vector2 prevRadius = { 0.5f*thick*prevNormal.x, 0.5f*thick*prevNormal.y };
1684
1685 for (int i = 0; i < pointCount - 1; i++)
1686 {
1687 Vector2 normal = { 0 };
1688
1689 if (i < pointCount - 2)
1690 {
1691 normal = (Vector2){-(points[i + 2].y - points[i + 1].y), (points[i + 2].x - points[i + 1].x)};
1692 float normalLength = sqrtf(normal.x*normal.x + normal.y*normal.y);
1693
1694 if (normalLength > 0.0f)
1695 {
1696 normal.x /= normalLength;
1697 normal.y /= normalLength;
1698 }
1699 else
1700 {
1701 normal.x = 0.0f;
1702 normal.y = 0.0f;
1703 }
1704 }
1705 else
1706 {
1707 normal = prevNormal;
1708 }
1709
1710 Vector2 radius = { prevNormal.x + normal.x, prevNormal.y + normal.y };
1711 float radiusLength = sqrtf(radius.x*radius.x + radius.y*radius.y);
1712
1713 if (radiusLength > 0.0f)
1714 {
1715 radius.x /= radiusLength;
1716 radius.y /= radiusLength;
1717 }
1718 else
1719 {
1720 radius.x = 0.0f;
1721 radius.y = 0.0f;
1722 }
1723
1724 float cosTheta = radius.x*normal.x + radius.y*normal.y;
1725
1726 if (cosTheta != 0.0f)
1727 {
1728 radius.x *= (thick*0.5f/cosTheta);
1729 radius.y *= (thick*0.5f/cosTheta);
1730 }
1731 else
1732 {
1733 radius.x = 0.0f;
1734 radius.y = 0.0f;
1735 }
1736
1737 Vector2 strip[4] = {
1738 { points[i].x - prevRadius.x, points[i].y - prevRadius.y },
1739 { points[i].x + prevRadius.x, points[i].y + prevRadius.y },
1740 { points[i + 1].x - radius.x, points[i + 1].y - radius.y },
1741 { points[i + 1].x + radius.x, points[i + 1].y + radius.y }
1742 };
1743
1744 DrawTriangleStrip(strip, 4, color);
1745
1746 prevRadius = radius;
1747 prevNormal = normal;
1748 }
1749
1750#else // !SUPPORT_SPLINE_MITERS
1751
1752 Vector2 delta = { 0 };
1753 float length = 0.0f;
1754 float scale = 0.0f;
1755
1756 for (int i = 0; i < pointCount - 1; i++)
1757 {
1758 delta = (Vector2){ points[i + 1].x - points[i].x, points[i + 1].y - points[i].y };
1759 length = sqrtf(delta.x*delta.x + delta.y*delta.y);
1760
1761 if (length > 0) scale = thick/(2*length);
1762
1763 Vector2 radius = { -scale*delta.y, scale*delta.x };
1764 Vector2 strip[4] = {
1765 { points[i].x - radius.x, points[i].y - radius.y },
1766 { points[i].x + radius.x, points[i].y + radius.y },
1767 { points[i + 1].x - radius.x, points[i + 1].y - radius.y },
1768 { points[i + 1].x + radius.x, points[i + 1].y + radius.y }
1769 };
1770
1771 DrawTriangleStrip(strip, 4, color);
1772 }
1773#endif
1774
1775#if defined(SUPPORT_SPLINE_SEGMENT_CAPS)
1776 // TODO: Add spline segment rounded caps at the begin/end of the spline
1777#endif
1778}
1779
1780// Draw spline: B-Spline, minimum 4 points
1781void DrawSplineBasis(const Vector2 *points, int pointCount, float thick, Color color)
1782{
1783 if (pointCount < 4) return;
1784
1785 float a[4] = { 0 };
1786 float b[4] = { 0 };
1787 float dy = 0.0f;
1788 float dx = 0.0f;
1789 float size = 0.0f;
1790
1791 Vector2 currentPoint = { 0 };
1792 Vector2 nextPoint = { 0 };
1793 Vector2 vertices[2*SPLINE_SEGMENT_DIVISIONS + 2] = { 0 };
1794
1795 for (int i = 0; i < (pointCount - 3); i++)
1796 {
1797 float t = 0.0f;
1798 Vector2 p1 = points[i], p2 = points[i + 1], p3 = points[i + 2], p4 = points[i + 3];
1799
1800 a[0] = (-p1.x + 3.0f*p2.x - 3.0f*p3.x + p4.x)/6.0f;
1801 a[1] = (3.0f*p1.x - 6.0f*p2.x + 3.0f*p3.x)/6.0f;
1802 a[2] = (-3.0f*p1.x + 3.0f*p3.x)/6.0f;
1803 a[3] = (p1.x + 4.0f*p2.x + p3.x)/6.0f;
1804
1805 b[0] = (-p1.y + 3.0f*p2.y - 3.0f*p3.y + p4.y)/6.0f;
1806 b[1] = (3.0f*p1.y - 6.0f*p2.y + 3.0f*p3.y)/6.0f;
1807 b[2] = (-3.0f*p1.y + 3.0f*p3.y)/6.0f;
1808 b[3] = (p1.y + 4.0f*p2.y + p3.y)/6.0f;
1809
1810 currentPoint.x = a[3];
1811 currentPoint.y = b[3];
1812
1813 if (i == 0) DrawCircleV(currentPoint, thick/2.0f, color); // Draw init line circle-cap
1814
1815 if (i > 0)
1816 {
1817 vertices[0].x = currentPoint.x + dy*size;
1818 vertices[0].y = currentPoint.y - dx*size;
1819 vertices[1].x = currentPoint.x - dy*size;
1820 vertices[1].y = currentPoint.y + dx*size;
1821 }
1822
1823 for (int j = 1; j <= SPLINE_SEGMENT_DIVISIONS; j++)
1824 {
1825 t = ((float)j)/((float)SPLINE_SEGMENT_DIVISIONS);
1826
1827 nextPoint.x = a[3] + t*(a[2] + t*(a[1] + t*a[0]));
1828 nextPoint.y = b[3] + t*(b[2] + t*(b[1] + t*b[0]));
1829
1830 dy = nextPoint.y - currentPoint.y;
1831 dx = nextPoint.x - currentPoint.x;
1832 size = 0.5f*thick/sqrtf(dx*dx+dy*dy);
1833
1834 if ((i == 0) && (j == 1))
1835 {
1836 vertices[0].x = currentPoint.x + dy*size;
1837 vertices[0].y = currentPoint.y - dx*size;
1838 vertices[1].x = currentPoint.x - dy*size;
1839 vertices[1].y = currentPoint.y + dx*size;
1840 }
1841
1842 vertices[2*j + 1].x = nextPoint.x - dy*size;
1843 vertices[2*j + 1].y = nextPoint.y + dx*size;
1844 vertices[2*j].x = nextPoint.x + dy*size;
1845 vertices[2*j].y = nextPoint.y - dx*size;
1846
1847 currentPoint = nextPoint;
1848 }
1849
1850 DrawTriangleStrip(vertices, 2*SPLINE_SEGMENT_DIVISIONS + 2, color);
1851 }
1852
1853 // Cap circle drawing at the end of every segment
1854 DrawCircleV(currentPoint, thick/2.0f, color);
1855}
1856
1857// Draw spline: Catmull-Rom, minimum 4 points
1858void DrawSplineCatmullRom(const Vector2 *points, int pointCount, float thick, Color color)
1859{
1860 if (pointCount < 4) return;
1861
1862 float dy = 0.0f;
1863 float dx = 0.0f;
1864 float size = 0.0f;
1865
1866 Vector2 currentPoint = points[1];
1867 Vector2 nextPoint = { 0 };
1868 Vector2 vertices[2*SPLINE_SEGMENT_DIVISIONS + 2] = { 0 };
1869
1870 DrawCircleV(currentPoint, thick/2.0f, color); // Draw init line circle-cap
1871
1872 for (int i = 0; i < (pointCount - 3); i++)
1873 {
1874 float t = 0.0f;
1875 Vector2 p1 = points[i], p2 = points[i + 1], p3 = points[i + 2], p4 = points[i + 3];
1876
1877 if (i > 0)
1878 {
1879 vertices[0].x = currentPoint.x + dy*size;
1880 vertices[0].y = currentPoint.y - dx*size;
1881 vertices[1].x = currentPoint.x - dy*size;
1882 vertices[1].y = currentPoint.y + dx*size;
1883 }
1884
1885 for (int j = 1; j <= SPLINE_SEGMENT_DIVISIONS; j++)
1886 {
1887 t = ((float)j)/((float)SPLINE_SEGMENT_DIVISIONS);
1888
1889 float q0 = (-1.0f*t*t*t) + (2.0f*t*t) + (-1.0f*t);
1890 float q1 = (3.0f*t*t*t) + (-5.0f*t*t) + 2.0f;
1891 float q2 = (-3.0f*t*t*t) + (4.0f*t*t) + t;
1892 float q3 = t*t*t - t*t;
1893
1894 nextPoint.x = 0.5f*((p1.x*q0) + (p2.x*q1) + (p3.x*q2) + (p4.x*q3));
1895 nextPoint.y = 0.5f*((p1.y*q0) + (p2.y*q1) + (p3.y*q2) + (p4.y*q3));
1896
1897 dy = nextPoint.y - currentPoint.y;
1898 dx = nextPoint.x - currentPoint.x;
1899 size = (0.5f*thick)/sqrtf(dx*dx + dy*dy);
1900
1901 if ((i == 0) && (j == 1))
1902 {
1903 vertices[0].x = currentPoint.x + dy*size;
1904 vertices[0].y = currentPoint.y - dx*size;
1905 vertices[1].x = currentPoint.x - dy*size;
1906 vertices[1].y = currentPoint.y + dx*size;
1907 }
1908
1909 vertices[2*j + 1].x = nextPoint.x - dy*size;
1910 vertices[2*j + 1].y = nextPoint.y + dx*size;
1911 vertices[2*j].x = nextPoint.x + dy*size;
1912 vertices[2*j].y = nextPoint.y - dx*size;
1913
1914 currentPoint = nextPoint;
1915 }
1916
1917 DrawTriangleStrip(vertices, 2*SPLINE_SEGMENT_DIVISIONS + 2, color);
1918 }
1919
1920 // Cap circle drawing at the end of every segment
1921 DrawCircleV(currentPoint, thick/2.0f, color);
1922}
1923
1924// Draw spline: Quadratic Bezier, minimum 3 points (1 control point): [p1, c2, p3, c4...]
1925void DrawSplineBezierQuadratic(const Vector2 *points, int pointCount, float thick, Color color)
1926{
1927 if (pointCount >= 3)
1928 {
1929 for (int i = 0; i < pointCount - 2; i += 2) DrawSplineSegmentBezierQuadratic(points[i], points[i + 1], points[i + 2], thick, color);
1930
1931 // Cap circle drawing at the end of every segment
1932 //for (int i = 2; i < pointCount - 2; i += 2) DrawCircleV(points[i], thick/2.0f, color);
1933 }
1934}
1935
1936// Draw spline: Cubic Bezier, minimum 4 points (2 control points): [p1, c2, c3, p4, c5, c6...]
1937void DrawSplineBezierCubic(const Vector2 *points, int pointCount, float thick, Color color)
1938{
1939 if (pointCount >= 4)
1940 {
1941 for (int i = 0; i < pointCount - 3; i += 3) DrawSplineSegmentBezierCubic(points[i], points[i + 1], points[i + 2], points[i + 3], thick, color);
1942
1943 // Cap circle drawing at the end of every segment
1944 //for (int i = 3; i < pointCount - 3; i += 3) DrawCircleV(points[i], thick/2.0f, color);
1945 }
1946}
1947
1948// Draw spline segment: Linear, 2 points
1949void DrawSplineSegmentLinear(Vector2 p1, Vector2 p2, float thick, Color color)
1950{
1951 // NOTE: For the linear spline we don't use subdivisions, just a single quad
1952
1953 Vector2 delta = { p2.x - p1.x, p2.y - p1.y };
1954 float length = sqrtf(delta.x*delta.x + delta.y*delta.y);
1955
1956 if ((length > 0) && (thick > 0))
1957 {
1958 float scale = thick/(2*length);
1959
1960 Vector2 radius = { -scale*delta.y, scale*delta.x };
1961 Vector2 strip[4] = {
1962 { p1.x - radius.x, p1.y - radius.y },
1963 { p1.x + radius.x, p1.y + radius.y },
1964 { p2.x - radius.x, p2.y - radius.y },
1965 { p2.x + radius.x, p2.y + radius.y }
1966 };
1967
1968 DrawTriangleStrip(strip, 4, color);
1969 }
1970}
1971
1972// Draw spline segment: B-Spline, 4 points
1973void DrawSplineSegmentBasis(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, float thick, Color color)
1974{
1975 const float step = 1.0f/SPLINE_SEGMENT_DIVISIONS;
1976
1977 Vector2 currentPoint = { 0 };
1978 Vector2 nextPoint = { 0 };
1979 float t = 0.0f;
1980
1981 Vector2 points[2*SPLINE_SEGMENT_DIVISIONS + 2] = { 0 };
1982
1983 float a[4] = { 0 };
1984 float b[4] = { 0 };
1985
1986 a[0] = (-p1.x + 3*p2.x - 3*p3.x + p4.x)/6.0f;
1987 a[1] = (3*p1.x - 6*p2.x + 3*p3.x)/6.0f;
1988 a[2] = (-3*p1.x + 3*p3.x)/6.0f;
1989 a[3] = (p1.x + 4*p2.x + p3.x)/6.0f;
1990
1991 b[0] = (-p1.y + 3*p2.y - 3*p3.y + p4.y)/6.0f;
1992 b[1] = (3*p1.y - 6*p2.y + 3*p3.y)/6.0f;
1993 b[2] = (-3*p1.y + 3*p3.y)/6.0f;
1994 b[3] = (p1.y + 4*p2.y + p3.y)/6.0f;
1995
1996 currentPoint.x = a[3];
1997 currentPoint.y = b[3];
1998
1999 for (int i = 0; i <= SPLINE_SEGMENT_DIVISIONS; i++)
2000 {
2001 t = step*(float)i;
2002
2003 nextPoint.x = a[3] + t*(a[2] + t*(a[1] + t*a[0]));
2004 nextPoint.y = b[3] + t*(b[2] + t*(b[1] + t*b[0]));
2005
2006 float dy = nextPoint.y - currentPoint.y;
2007 float dx = nextPoint.x - currentPoint.x;
2008 float size = (0.5f*thick)/sqrtf(dx*dx + dy*dy);
2009
2010 if (i == 1)
2011 {
2012 points[0].x = currentPoint.x + dy*size;
2013 points[0].y = currentPoint.y - dx*size;
2014 points[1].x = currentPoint.x - dy*size;
2015 points[1].y = currentPoint.y + dx*size;
2016 }
2017
2018 points[2*i + 1].x = nextPoint.x - dy*size;
2019 points[2*i + 1].y = nextPoint.y + dx*size;
2020 points[2*i].x = nextPoint.x + dy*size;
2021 points[2*i].y = nextPoint.y - dx*size;
2022
2023 currentPoint = nextPoint;
2024 }
2025
2026 DrawTriangleStrip(points, 2*SPLINE_SEGMENT_DIVISIONS+2, color);
2027}
2028
2029// Draw spline segment: Catmull-Rom, 4 points
2030void DrawSplineSegmentCatmullRom(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, float thick, Color color)
2031{
2032 const float step = 1.0f/SPLINE_SEGMENT_DIVISIONS;
2033
2034 Vector2 currentPoint = p1;
2035 Vector2 nextPoint = { 0 };
2036 float t = 0.0f;
2037
2038 Vector2 points[2*SPLINE_SEGMENT_DIVISIONS + 2] = { 0 };
2039
2040 for (int i = 0; i <= SPLINE_SEGMENT_DIVISIONS; i++)
2041 {
2042 t = step*(float)i;
2043
2044 float q0 = (-1*t*t*t) + (2*t*t) + (-1*t);
2045 float q1 = (3*t*t*t) + (-5*t*t) + 2;
2046 float q2 = (-3*t*t*t) + (4*t*t) + t;
2047 float q3 = t*t*t - t*t;
2048
2049 nextPoint.x = 0.5f*((p1.x*q0) + (p2.x*q1) + (p3.x*q2) + (p4.x*q3));
2050 nextPoint.y = 0.5f*((p1.y*q0) + (p2.y*q1) + (p3.y*q2) + (p4.y*q3));
2051
2052 float dy = nextPoint.y - currentPoint.y;
2053 float dx = nextPoint.x - currentPoint.x;
2054 float size = (0.5f*thick)/sqrtf(dx*dx + dy*dy);
2055
2056 if (i == 1)
2057 {
2058 points[0].x = currentPoint.x + dy*size;
2059 points[0].y = currentPoint.y - dx*size;
2060 points[1].x = currentPoint.x - dy*size;
2061 points[1].y = currentPoint.y + dx*size;
2062 }
2063
2064 points[2*i + 1].x = nextPoint.x - dy*size;
2065 points[2*i + 1].y = nextPoint.y + dx*size;
2066 points[2*i].x = nextPoint.x + dy*size;
2067 points[2*i].y = nextPoint.y - dx*size;
2068
2069 currentPoint = nextPoint;
2070 }
2071
2072 DrawTriangleStrip(points, 2*SPLINE_SEGMENT_DIVISIONS + 2, color);
2073}
2074
2075// Draw spline segment: Quadratic Bezier, 2 points, 1 control point
2076void DrawSplineSegmentBezierQuadratic(Vector2 p1, Vector2 c2, Vector2 p3, float thick, Color color)
2077{
2078 const float step = 1.0f/SPLINE_SEGMENT_DIVISIONS;
2079
2080 Vector2 previous = p1;
2081 Vector2 current = { 0 };
2082 float t = 0.0f;
2083
2084 Vector2 points[2*SPLINE_SEGMENT_DIVISIONS + 2] = { 0 };
2085
2086 for (int i = 1; i <= SPLINE_SEGMENT_DIVISIONS; i++)
2087 {
2088 t = step*(float)i;
2089
2090 float a = powf(1.0f - t, 2);
2091 float b = 2.0f*(1.0f - t)*t;
2092 float c = powf(t, 2);
2093
2094 // NOTE: The easing functions aren't suitable here because they don't take a control point
2095 current.y = a*p1.y + b*c2.y + c*p3.y;
2096 current.x = a*p1.x + b*c2.x + c*p3.x;
2097
2098 float dy = current.y - previous.y;
2099 float dx = current.x - previous.x;
2100 float size = 0.5f*thick/sqrtf(dx*dx+dy*dy);
2101
2102 if (i == 1)
2103 {
2104 points[0].x = previous.x + dy*size;
2105 points[0].y = previous.y - dx*size;
2106 points[1].x = previous.x - dy*size;
2107 points[1].y = previous.y + dx*size;
2108 }
2109
2110 points[2*i + 1].x = current.x - dy*size;
2111 points[2*i + 1].y = current.y + dx*size;
2112 points[2*i].x = current.x + dy*size;
2113 points[2*i].y = current.y - dx*size;
2114
2115 previous = current;
2116 }
2117
2118 DrawTriangleStrip(points, 2*SPLINE_SEGMENT_DIVISIONS + 2, color);
2119}
2120
2121// Draw spline segment: Cubic Bezier, 2 points, 2 control points
2122void DrawSplineSegmentBezierCubic(Vector2 p1, Vector2 c2, Vector2 c3, Vector2 p4, float thick, Color color)
2123{
2124 const float step = 1.0f/SPLINE_SEGMENT_DIVISIONS;
2125
2126 Vector2 previous = p1;
2127 Vector2 current = { 0 };
2128 float t = 0.0f;
2129
2130 Vector2 points[2*SPLINE_SEGMENT_DIVISIONS + 2] = { 0 };
2131
2132 for (int i = 1; i <= SPLINE_SEGMENT_DIVISIONS; i++)
2133 {
2134 t = step*(float)i;
2135
2136 float a = powf(1.0f - t, 3);
2137 float b = 3.0f*powf(1.0f - t, 2)*t;
2138 float c = 3.0f*(1.0f - t)*powf(t, 2);
2139 float d = powf(t, 3);
2140
2141 current.y = a*p1.y + b*c2.y + c*c3.y + d*p4.y;
2142 current.x = a*p1.x + b*c2.x + c*c3.x + d*p4.x;
2143
2144 float dy = current.y - previous.y;
2145 float dx = current.x - previous.x;
2146 float size = 0.5f*thick/sqrtf(dx*dx+dy*dy);
2147
2148 if (i == 1)
2149 {
2150 points[0].x = previous.x + dy*size;
2151 points[0].y = previous.y - dx*size;
2152 points[1].x = previous.x - dy*size;
2153 points[1].y = previous.y + dx*size;
2154 }
2155
2156 points[2*i + 1].x = current.x - dy*size;
2157 points[2*i + 1].y = current.y + dx*size;
2158 points[2*i].x = current.x + dy*size;
2159 points[2*i].y = current.y - dx*size;
2160
2161 previous = current;
2162 }
2163
2164 DrawTriangleStrip(points, 2*SPLINE_SEGMENT_DIVISIONS + 2, color);
2165}
2166
2167// Get spline point for a given t [0.0f .. 1.0f], Linear
2168Vector2 GetSplinePointLinear(Vector2 startPos, Vector2 endPos, float t)
2169{
2170 Vector2 point = { 0 };
2171
2172 point.x = startPos.x*(1.0f - t) + endPos.x*t;
2173 point.y = startPos.y*(1.0f - t) + endPos.y*t;
2174
2175 return point;
2176}
2177
2178// Get spline point for a given t [0.0f .. 1.0f], B-Spline
2179Vector2 GetSplinePointBasis(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, float t)
2180{
2181 Vector2 point = { 0 };
2182
2183 float a[4] = { 0 };
2184 float b[4] = { 0 };
2185
2186 a[0] = (-p1.x + 3*p2.x - 3*p3.x + p4.x)/6.0f;
2187 a[1] = (3*p1.x - 6*p2.x + 3*p3.x)/6.0f;
2188 a[2] = (-3*p1.x + 3*p3.x)/6.0f;
2189 a[3] = (p1.x + 4*p2.x + p3.x)/6.0f;
2190
2191 b[0] = (-p1.y + 3*p2.y - 3*p3.y + p4.y)/6.0f;
2192 b[1] = (3*p1.y - 6*p2.y + 3*p3.y)/6.0f;
2193 b[2] = (-3*p1.y + 3*p3.y)/6.0f;
2194 b[3] = (p1.y + 4*p2.y + p3.y)/6.0f;
2195
2196 point.x = a[3] + t*(a[2] + t*(a[1] + t*a[0]));
2197 point.y = b[3] + t*(b[2] + t*(b[1] + t*b[0]));
2198
2199 return point;
2200}
2201
2202// Get spline point for a given t [0.0f .. 1.0f], Catmull-Rom
2203Vector2 GetSplinePointCatmullRom(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, float t)
2204{
2205 Vector2 point = { 0 };
2206
2207 float q0 = (-1*t*t*t) + (2*t*t) + (-1*t);
2208 float q1 = (3*t*t*t) + (-5*t*t) + 2;
2209 float q2 = (-3*t*t*t) + (4*t*t) + t;
2210 float q3 = t*t*t - t*t;
2211
2212 point.x = 0.5f*((p1.x*q0) + (p2.x*q1) + (p3.x*q2) + (p4.x*q3));
2213 point.y = 0.5f*((p1.y*q0) + (p2.y*q1) + (p3.y*q2) + (p4.y*q3));
2214
2215 return point;
2216}
2217
2218// Get spline point for a given t [0.0f .. 1.0f], Quadratic Bezier
2219Vector2 GetSplinePointBezierQuad(Vector2 startPos, Vector2 controlPos, Vector2 endPos, float t)
2220{
2221 Vector2 point = { 0 };
2222
2223 float a = powf(1.0f - t, 2);
2224 float b = 2.0f*(1.0f - t)*t;
2225 float c = powf(t, 2);
2226
2227 point.y = a*startPos.y + b*controlPos.y + c*endPos.y;
2228 point.x = a*startPos.x + b*controlPos.x + c*endPos.x;
2229
2230 return point;
2231}
2232
2233// Get spline point for a given t [0.0f .. 1.0f], Cubic Bezier
2234Vector2 GetSplinePointBezierCubic(Vector2 startPos, Vector2 startControlPos, Vector2 endControlPos, Vector2 endPos, float t)
2235{
2236 Vector2 point = { 0 };
2237
2238 float a = powf(1.0f - t, 3);
2239 float b = 3.0f*powf(1.0f - t, 2)*t;
2240 float c = 3.0f*(1.0f - t)*powf(t, 2);
2241 float d = powf(t, 3);
2242
2243 point.y = a*startPos.y + b*startControlPos.y + c*endControlPos.y + d*endPos.y;
2244 point.x = a*startPos.x + b*startControlPos.x + c*endControlPos.x + d*endPos.x;
2245
2246 return point;
2247}
2248
2249//----------------------------------------------------------------------------------
2250// Module Functions Definition - Collision Detection functions
2251//----------------------------------------------------------------------------------
2252
2253// Check if point is inside rectangle
2254bool CheckCollisionPointRec(Vector2 point, Rectangle rec)
2255{
2256 bool collision = false;
2257
2258 if ((point.x >= rec.x) && (point.x < (rec.x + rec.width)) && (point.y >= rec.y) && (point.y < (rec.y + rec.height))) collision = true;
2259
2260 return collision;
2261}
2262
2263// Check if point is inside circle
2264bool CheckCollisionPointCircle(Vector2 point, Vector2 center, float radius)
2265{
2266 bool collision = false;
2267
2268 float distanceSquared = (point.x - center.x)*(point.x - center.x) + (point.y - center.y)*(point.y - center.y);
2269
2270 if (distanceSquared <= radius*radius) collision = true;
2271
2272 return collision;
2273}
2274
2275// Check if point is inside a triangle defined by three points (p1, p2, p3)
2276bool CheckCollisionPointTriangle(Vector2 point, Vector2 p1, Vector2 p2, Vector2 p3)
2277{
2278 bool collision = false;
2279
2280 float alpha = ((p2.y - p3.y)*(point.x - p3.x) + (p3.x - p2.x)*(point.y - p3.y)) /
2281 ((p2.y - p3.y)*(p1.x - p3.x) + (p3.x - p2.x)*(p1.y - p3.y));
2282
2283 float beta = ((p3.y - p1.y)*(point.x - p3.x) + (p1.x - p3.x)*(point.y - p3.y)) /
2284 ((p2.y - p3.y)*(p1.x - p3.x) + (p3.x - p2.x)*(p1.y - p3.y));
2285
2286 float gamma = 1.0f - alpha - beta;
2287
2288 if ((alpha > 0) && (beta > 0) && (gamma > 0)) collision = true;
2289
2290 return collision;
2291}
2292
2293// Check if point is within a polygon described by array of vertices
2294// NOTE: Based on http://jeffreythompson.org/collision-detection/poly-point.php
2295bool CheckCollisionPointPoly(Vector2 point, const Vector2 *points, int pointCount)
2296{
2297 bool collision = false;
2298
2299 if (pointCount > 2)
2300 {
2301 for (int i = 0, j = pointCount - 1; i < pointCount; j = i++)
2302 {
2303 if ((points[i].y > point.y) != (points[j].y > point.y) &&
2304 (point.x < (points[j].x - points[i].x)*(point.y - points[i].y)/(points[j].y - points[i].y) + points[i].x))
2305 {
2306 collision = !collision;
2307 }
2308 }
2309 }
2310
2311 return collision;
2312}
2313
2314// Check collision between two rectangles
2315bool CheckCollisionRecs(Rectangle rec1, Rectangle rec2)
2316{
2317 bool collision = false;
2318
2319 if ((rec1.x < (rec2.x + rec2.width) && (rec1.x + rec1.width) > rec2.x) &&
2320 (rec1.y < (rec2.y + rec2.height) && (rec1.y + rec1.height) > rec2.y)) collision = true;
2321
2322 return collision;
2323}
2324
2325// Check collision between two circles
2326bool CheckCollisionCircles(Vector2 center1, float radius1, Vector2 center2, float radius2)
2327{
2328 bool collision = false;
2329
2330 float dx = center2.x - center1.x; // X distance between centers
2331 float dy = center2.y - center1.y; // Y distance between centers
2332
2333 float distanceSquared = dx*dx + dy*dy; // Distance between centers squared
2334 float radiusSum = radius1 + radius2;
2335
2336 collision = (distanceSquared <= (radiusSum*radiusSum));
2337
2338 return collision;
2339}
2340
2341// Check collision between circle and rectangle
2342// NOTE: Reviewed version to take into account corner limit case
2343bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec)
2344{
2345 bool collision = false;
2346
2347 float recCenterX = rec.x + rec.width/2.0f;
2348 float recCenterY = rec.y + rec.height/2.0f;
2349
2350 float dx = fabsf(center.x - recCenterX);
2351 float dy = fabsf(center.y - recCenterY);
2352
2353 if (dx > (rec.width/2.0f + radius)) { return false; }
2354 if (dy > (rec.height/2.0f + radius)) { return false; }
2355
2356 if (dx <= (rec.width/2.0f)) { return true; }
2357 if (dy <= (rec.height/2.0f)) { return true; }
2358
2359 float cornerDistanceSq = (dx - rec.width/2.0f)*(dx - rec.width/2.0f) +
2360 (dy - rec.height/2.0f)*(dy - rec.height/2.0f);
2361
2362 collision = (cornerDistanceSq <= (radius*radius));
2363
2364 return collision;
2365}
2366
2367// Check the collision between two lines defined by two points each, returns collision point by reference
2368bool CheckCollisionLines(Vector2 startPos1, Vector2 endPos1, Vector2 startPos2, Vector2 endPos2, Vector2 *collisionPoint)
2369{
2370 bool collision = false;
2371
2372 float div = (endPos2.y - startPos2.y)*(endPos1.x - startPos1.x) - (endPos2.x - startPos2.x)*(endPos1.y - startPos1.y);
2373
2374 if (fabsf(div) >= FLT_EPSILON)
2375 {
2376 collision = true;
2377
2378 float xi = ((startPos2.x - endPos2.x)*(startPos1.x*endPos1.y - startPos1.y*endPos1.x) - (startPos1.x - endPos1.x)*(startPos2.x*endPos2.y - startPos2.y*endPos2.x))/div;
2379 float yi = ((startPos2.y - endPos2.y)*(startPos1.x*endPos1.y - startPos1.y*endPos1.x) - (startPos1.y - endPos1.y)*(startPos2.x*endPos2.y - startPos2.y*endPos2.x))/div;
2380
2381 if (((fabsf(startPos1.x - endPos1.x) > FLT_EPSILON) && (xi < fminf(startPos1.x, endPos1.x) || (xi > fmaxf(startPos1.x, endPos1.x)))) ||
2382 ((fabsf(startPos2.x - endPos2.x) > FLT_EPSILON) && (xi < fminf(startPos2.x, endPos2.x) || (xi > fmaxf(startPos2.x, endPos2.x)))) ||
2383 ((fabsf(startPos1.y - endPos1.y) > FLT_EPSILON) && (yi < fminf(startPos1.y, endPos1.y) || (yi > fmaxf(startPos1.y, endPos1.y)))) ||
2384 ((fabsf(startPos2.y - endPos2.y) > FLT_EPSILON) && (yi < fminf(startPos2.y, endPos2.y) || (yi > fmaxf(startPos2.y, endPos2.y))))) collision = false;
2385
2386 if (collision && (collisionPoint != 0))
2387 {
2388 collisionPoint->x = xi;
2389 collisionPoint->y = yi;
2390 }
2391 }
2392
2393 return collision;
2394}
2395
2396// Check if point belongs to line created between two points [p1] and [p2] with defined margin in pixels [threshold]
2397bool CheckCollisionPointLine(Vector2 point, Vector2 p1, Vector2 p2, int threshold)
2398{
2399 bool collision = false;
2400
2401 float dxc = point.x - p1.x;
2402 float dyc = point.y - p1.y;
2403 float dxl = p2.x - p1.x;
2404 float dyl = p2.y - p1.y;
2405 float cross = dxc*dyl - dyc*dxl;
2406
2407 if (fabsf(cross) < (threshold*fmaxf(fabsf(dxl), fabsf(dyl))))
2408 {
2409 if (fabsf(dxl) >= fabsf(dyl)) collision = (dxl > 0)? ((p1.x <= point.x) && (point.x <= p2.x)) : ((p2.x <= point.x) && (point.x <= p1.x));
2410 else collision = (dyl > 0)? ((p1.y <= point.y) && (point.y <= p2.y)) : ((p2.y <= point.y) && (point.y <= p1.y));
2411 }
2412
2413 return collision;
2414}
2415
2416// Check if circle collides with a line created between two points [p1] and [p2]
2417bool CheckCollisionCircleLine(Vector2 center, float radius, Vector2 p1, Vector2 p2)
2418{
2419 float dx = p1.x - p2.x;
2420 float dy = p1.y - p2.y;
2421
2422 if ((fabsf(dx) + fabsf(dy)) <= FLT_EPSILON)
2423 {
2424 return CheckCollisionCircles(p1, 0, center, radius);
2425 }
2426
2427 float lengthSQ = ((dx*dx) + (dy*dy));
2428 float dotProduct = (((center.x - p1.x)*(p2.x - p1.x)) + ((center.y - p1.y)*(p2.y - p1.y)))/(lengthSQ);
2429
2430 if (dotProduct > 1.0f) dotProduct = 1.0f;
2431 else if (dotProduct < 0.0f) dotProduct = 0.0f;
2432
2433 float dx2 = (p1.x - (dotProduct*(dx))) - center.x;
2434 float dy2 = (p1.y - (dotProduct*(dy))) - center.y;
2435 float distanceSQ = ((dx2*dx2) + (dy2*dy2));
2436
2437 return (distanceSQ <= radius*radius);
2438}
2439
2440// Get collision rectangle for two rectangles collision
2441Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2)
2442{
2443 Rectangle overlap = { 0 };
2444
2445 float left = (rec1.x > rec2.x)? rec1.x : rec2.x;
2446 float right1 = rec1.x + rec1.width;
2447 float right2 = rec2.x + rec2.width;
2448 float right = (right1 < right2)? right1 : right2;
2449 float top = (rec1.y > rec2.y)? rec1.y : rec2.y;
2450 float bottom1 = rec1.y + rec1.height;
2451 float bottom2 = rec2.y + rec2.height;
2452 float bottom = (bottom1 < bottom2)? bottom1 : bottom2;
2453
2454 if ((left < right) && (top < bottom))
2455 {
2456 overlap.x = left;
2457 overlap.y = top;
2458 overlap.width = right - left;
2459 overlap.height = bottom - top;
2460 }
2461
2462 return overlap;
2463}
2464
2465//----------------------------------------------------------------------------------
2466// Module Internal Functions Definition
2467//----------------------------------------------------------------------------------
2468
2469// Cubic easing in-out
2470// NOTE: Used by DrawLineBezier() only
2471static float EaseCubicInOut(float t, float b, float c, float d)
2472{
2473 float result = 0.0f;
2474
2475 if ((t /= 0.5f*d) < 1) result = 0.5f*c*t*t*t + b;
2476 else
2477 {
2478 t -= 2;
2479 result = 0.5f*c*(t*t*t + 2.0f) + b;
2480 }
2481
2482 return result;
2483}
2484
2485#endif // SUPPORT_MODULE_RSHAPES
2486
Copyright 2026  E766CB298A6D1E64 | Git-Thing heavily inspired by cgit