Skip to content

Commit 00ecd51

Browse files
committed
More tests
1 parent 42ba12d commit 00ecd51

File tree

4 files changed

+592
-11
lines changed

4 files changed

+592
-11
lines changed

justfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ clean:
1313
build CONFIG="Debug":
1414
cmake --build build --config {{CONFIG}}
1515

16+
[doc("execute unit tests using cmake")]
17+
test CONFIG="Debug":
18+
cmake -G Xcode -B build
19+
cmake --build build --target yup_tests --config {{CONFIG}}
20+
build/tests/{{CONFIG}}/yup_tests --gtest_filter=PathTests.*
21+
1622
[doc("generate and open project in macOS using Xcode")]
1723
osx PROFILING="OFF":
1824
cmake -G Xcode -B build -DYUP_ENABLE_PROFILING={{PROFILING}}

modules/yup_graphics/primitives/yup_Path.cpp

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ Path& Path::addLine (const Line<float>& line)
167167

168168
Path& Path::addRectangle (float x, float y, float width, float height)
169169
{
170+
width = jmax (0.0f, width);
171+
height = jmax (0.0f, height);
172+
170173
reserveSpace (size() + 5);
171174

172175
moveTo (x, y);
@@ -189,6 +192,9 @@ Path& Path::addRoundedRectangle (float x, float y, float width, float height, fl
189192
{
190193
reserveSpace (size() + 9);
191194

195+
width = jmax (0.0f, width);
196+
height = jmax (0.0f, height);
197+
192198
const float centerWidth = width * 0.5f;
193199
const float centerHeight = height * 0.5f;
194200
radiusTopLeft = jmin (radiusTopLeft, centerWidth, centerHeight);
@@ -245,6 +251,9 @@ Path& Path::addEllipse (float x, float y, float width, float height)
245251
{
246252
reserveSpace (size() + 6);
247253

254+
width = jmax (0.0f, width);
255+
height = jmax (0.0f, height);
256+
248257
const float rx = width * 0.5f;
249258
const float ry = height * 0.5f;
250259
const float cx = x + rx;
@@ -273,6 +282,9 @@ Path& Path::addCenteredEllipse (float centerX, float centerY, float radiusX, flo
273282
{
274283
reserveSpace (size() + 6);
275284

285+
radiusX = jmax (0.0f, radiusX);
286+
radiusY = jmax (0.0f, radiusY);
287+
276288
const float rx = radiusX;
277289
const float ry = radiusY;
278290
const float cx = centerX;
@@ -304,6 +316,9 @@ Path& Path::addCenteredEllipse (const Point<float>& center, const Size<float>& d
304316

305317
Path& Path::addArc (float x, float y, float width, float height, float fromRadians, float toRadians, bool startAsNewSubPath)
306318
{
319+
width = jmax (0.0f, width);
320+
height = jmax (0.0f, height);
321+
307322
const float radiusX = width * 0.5f;
308323
const float radiusY = height * 0.5f;
309324

@@ -329,6 +344,9 @@ Path& Path::addCenteredArc (float centerX, float centerY, float radiusX, float r
329344
const float sinTheta = std::sin (rotationOfEllipse);
330345

331346
// Initialize variables for the loop
347+
radiusX = jmax (0.0f, radiusX);
348+
radiusY = jmax (0.0f, radiusY);
349+
332350
float x = std::cos (fromRadians) * radiusX;
333351
float y = std::sin (fromRadians) * radiusY;
334352
float rotatedX = x * cosTheta - y * sinTheta + centerX;
@@ -369,14 +387,15 @@ Path& Path::addCenteredArc (const Point<float>& center, const Size<float>& diame
369387
}
370388

371389
//==============================================================================
372-
void Path::addPolygon (Point<float> centre, int numberOfSides, float radius, float startAngle)
390+
Path& Path::addPolygon (Point<float> centre, int numberOfSides, float radius, float startAngle)
373391
{
374-
if (numberOfSides < 3 || radius <= 0.0f)
375-
return;
392+
if (numberOfSides < 3)
393+
return *this;
376394

377395
reserveSpace (size() + numberOfSides + 1);
378396

379397
const float angleIncrement = MathConstants<float>::twoPi / numberOfSides;
398+
radius = jmax (0.0f, radius);
380399

381400
// Start with the first vertex
382401
float angle = startAngle;
@@ -395,17 +414,21 @@ void Path::addPolygon (Point<float> centre, int numberOfSides, float radius, flo
395414
}
396415

397416
close();
417+
418+
return *this;
398419
}
399420

400421
//==============================================================================
401-
void Path::addStar (Point<float> centre, int numberOfPoints, float innerRadius, float outerRadius, float startAngle)
422+
Path& Path::addStar (Point<float> centre, int numberOfPoints, float innerRadius, float outerRadius, float startAngle)
402423
{
403-
if (numberOfPoints < 3 || innerRadius <= 0.0f || outerRadius <= 0.0f)
404-
return;
424+
if (numberOfPoints < 3)
425+
return *this;
405426

406427
reserveSpace (size() + numberOfPoints * 2 + 1);
407428

408429
const float angleIncrement = MathConstants<float>::twoPi / (numberOfPoints * 2);
430+
innerRadius = jmax (0.0f, innerRadius);
431+
outerRadius = jmax (0.0f, outerRadius);
409432

410433
// Start with the first outer vertex
411434
float angle = startAngle;
@@ -425,13 +448,15 @@ void Path::addStar (Point<float> centre, int numberOfPoints, float innerRadius,
425448
}
426449

427450
close();
451+
452+
return *this;
428453
}
429454

430455
//==============================================================================
431-
void Path::addBubble (Rectangle<float> bodyArea, Rectangle<float> maximumArea, Point<float> arrowTipPosition, float cornerSize, float arrowBaseWidth)
456+
Path& Path::addBubble (Rectangle<float> bodyArea, Rectangle<float> maximumArea, Point<float> arrowTipPosition, float cornerSize, float arrowBaseWidth)
432457
{
433458
if (bodyArea.isEmpty() || maximumArea.isEmpty() || arrowBaseWidth <= 0.0f)
434-
return;
459+
return *this;
435460

436461
// Clamp corner size to reasonable bounds
437462
cornerSize = jmin (cornerSize, bodyArea.getWidth() * 0.5f, bodyArea.getHeight() * 0.5f);
@@ -609,6 +634,8 @@ void Path::addBubble (Rectangle<float> bodyArea, Rectangle<float> maximumArea, P
609634

610635
// Close the path
611636
close();
637+
638+
return *this;
612639
}
613640

614641
//==============================================================================

modules/yup_graphics/primitives/yup_Path.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ class YUP_API Path
438438
@param radius The radius from the center to each vertex.
439439
@param startAngle The starting angle in radians (0.0f starts at the right).
440440
*/
441-
void addPolygon (Point<float> centre, int numberOfSides, float radius, float startAngle = 0.0f);
441+
Path& addPolygon (Point<float> centre, int numberOfSides, float radius, float startAngle = 0.0f);
442442

443443
//==============================================================================
444444
/** Adds a star shape to the path.
@@ -452,7 +452,7 @@ class YUP_API Path
452452
@param outerRadius The radius from the center to the outer vertices.
453453
@param startAngle The starting angle in radians (0.0f starts at the right).
454454
*/
455-
void addStar (Point<float> centre, int numberOfPoints, float innerRadius, float outerRadius, float startAngle = 0.0f);
455+
Path& addStar (Point<float> centre, int numberOfPoints, float innerRadius, float outerRadius, float startAngle = 0.0f);
456456

457457
//==============================================================================
458458
/** Adds a speech bubble shape to the path.
@@ -466,7 +466,7 @@ class YUP_API Path
466466
@param cornerSize The radius of the rounded corners.
467467
@param arrowBaseWidth The width of the arrow at its base.
468468
*/
469-
void addBubble (Rectangle<float> bodyArea, Rectangle<float> maximumArea, Point<float> arrowTipPosition, float cornerSize, float arrowBaseWidth);
469+
Path& addBubble (Rectangle<float> bodyArea, Rectangle<float> maximumArea, Point<float> arrowTipPosition, float cornerSize, float arrowBaseWidth);
470470

471471
//==============================================================================
472472
/** Converts the path to a stroke polygon with specified width.

0 commit comments

Comments
 (0)