Debian Codegraph @

src/rshapes.c

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