Skip to content

Commit 2715fa4

Browse files
committed
More work
1 parent 17bb6d8 commit 2715fa4

File tree

1 file changed

+101
-97
lines changed

1 file changed

+101
-97
lines changed

modules/yup_graphics/primitives/yup_Path.cpp

Lines changed: 101 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -867,103 +867,6 @@ void handleEllipticalArc (String::CharPointerType& data, Path& path, float& curr
867867
}
868868
}
869869

870-
void addRoundedSubpath (Path& targetPath, const std::vector<Point<float>>& points, float cornerRadius, bool closed)
871-
{
872-
if (points.size() < 3)
873-
return;
874-
875-
bool first = true;
876-
877-
for (size_t i = 0; i < points.size(); ++i)
878-
{
879-
size_t prevIndex = (i == 0) ? (closed ? points.size() - 1 : 0) : i - 1;
880-
size_t nextIndex = (i == points.size() - 1) ? (closed ? 0 : i) : i + 1;
881-
882-
if (!closed && (i == 0 || i == points.size() - 1))
883-
{
884-
// Don't round first/last points in open paths
885-
if (first)
886-
{
887-
targetPath.moveTo (points[i]);
888-
first = false;
889-
}
890-
else
891-
{
892-
targetPath.lineTo (points[i]);
893-
}
894-
continue;
895-
}
896-
897-
Point<float> current = points[i];
898-
Point<float> prev = points[prevIndex];
899-
Point<float> next = points[nextIndex];
900-
901-
// Calculate vectors
902-
Point<float> toPrev = (prev - current).normalized();
903-
Point<float> toNext = (next - current).normalized();
904-
905-
// Calculate the angle between vectors
906-
float dot = toPrev.dotProduct (toNext);
907-
dot = jlimit (-1.0f, 1.0f, dot); // Clamp to avoid numerical issues
908-
909-
if (std::abs (dot + 1.0f) < 0.001f) // Vectors are opposite (180 degrees)
910-
{
911-
// Straight line, no rounding needed
912-
if (first)
913-
{
914-
targetPath.moveTo (current);
915-
first = false;
916-
}
917-
else
918-
{
919-
targetPath.lineTo (current);
920-
}
921-
continue;
922-
}
923-
924-
// Calculate distances to round corner
925-
float prevDist = current.distanceTo (prev);
926-
float nextDist = current.distanceTo (next);
927-
float maxRadius = jmin (cornerRadius, prevDist * 0.5f, nextDist * 0.5f);
928-
929-
if (maxRadius <= 0.0f)
930-
{
931-
if (first)
932-
{
933-
targetPath.moveTo (current);
934-
first = false;
935-
}
936-
else
937-
{
938-
targetPath.lineTo (current);
939-
}
940-
continue;
941-
}
942-
943-
// Calculate corner points
944-
Point<float> cornerStart = current + toPrev * maxRadius;
945-
Point<float> cornerEnd = current + toNext * maxRadius;
946-
947-
if (first)
948-
{
949-
targetPath.moveTo (cornerStart);
950-
first = false;
951-
}
952-
else
953-
{
954-
targetPath.lineTo (cornerStart);
955-
}
956-
957-
// Add rounded corner using quadratic curve
958-
targetPath.quadTo (cornerEnd.getX(), cornerEnd.getY(), current.getX(), current.getY());
959-
}
960-
961-
if (closed)
962-
{
963-
targetPath.close();
964-
}
965-
}
966-
967870
} // namespace
968871

969872
bool Path::parsePathData (const String& pathData)
@@ -1440,6 +1343,107 @@ Path Path::createStrokePolygon (float strokeWidth) const
14401343
}
14411344

14421345
//==============================================================================
1346+
namespace {
1347+
1348+
void addRoundedSubpath (Path& targetPath, const std::vector<Point<float>>& points, float cornerRadius, bool closed)
1349+
{
1350+
if (points.size() < 3)
1351+
return;
1352+
1353+
bool first = true;
1354+
1355+
for (size_t i = 0; i < points.size(); ++i)
1356+
{
1357+
size_t prevIndex = (i == 0) ? (closed ? points.size() - 1 : 0) : i - 1;
1358+
size_t nextIndex = (i == points.size() - 1) ? (closed ? 0 : i) : i + 1;
1359+
1360+
if (!closed && (i == 0 || i == points.size() - 1))
1361+
{
1362+
// Don't round first/last points in open paths
1363+
if (first)
1364+
{
1365+
targetPath.moveTo (points[i]);
1366+
first = false;
1367+
}
1368+
else
1369+
{
1370+
targetPath.lineTo (points[i]);
1371+
}
1372+
continue;
1373+
}
1374+
1375+
Point<float> current = points[i];
1376+
Point<float> prev = points[prevIndex];
1377+
Point<float> next = points[nextIndex];
1378+
1379+
// Calculate vectors
1380+
Point<float> toPrev = (prev - current).normalized();
1381+
Point<float> toNext = (next - current).normalized();
1382+
1383+
// Calculate the angle between vectors
1384+
float dot = toPrev.dotProduct (toNext);
1385+
dot = jlimit (-1.0f, 1.0f, dot); // Clamp to avoid numerical issues
1386+
1387+
if (std::abs (dot + 1.0f) < 0.001f) // Vectors are opposite (180 degrees)
1388+
{
1389+
// Straight line, no rounding needed
1390+
if (first)
1391+
{
1392+
targetPath.moveTo (current);
1393+
first = false;
1394+
}
1395+
else
1396+
{
1397+
targetPath.lineTo (current);
1398+
}
1399+
continue;
1400+
}
1401+
1402+
// Calculate distances to round corner
1403+
float prevDist = current.distanceTo (prev);
1404+
float nextDist = current.distanceTo (next);
1405+
float maxRadius = jmin (cornerRadius, prevDist * 0.5f, nextDist * 0.5f);
1406+
1407+
if (maxRadius <= 0.0f)
1408+
{
1409+
if (first)
1410+
{
1411+
targetPath.moveTo (current);
1412+
first = false;
1413+
}
1414+
else
1415+
{
1416+
targetPath.lineTo (current);
1417+
}
1418+
continue;
1419+
}
1420+
1421+
// Calculate corner points
1422+
Point<float> cornerStart = current + toPrev * maxRadius;
1423+
Point<float> cornerEnd = current + toNext * maxRadius;
1424+
1425+
if (first)
1426+
{
1427+
targetPath.moveTo (cornerStart);
1428+
first = false;
1429+
}
1430+
else
1431+
{
1432+
targetPath.lineTo (cornerStart);
1433+
}
1434+
1435+
// Add rounded corner using quadratic curve
1436+
targetPath.quadTo (cornerEnd.getX(), cornerEnd.getY(), current.getX(), current.getY());
1437+
}
1438+
1439+
if (closed)
1440+
{
1441+
targetPath.close();
1442+
}
1443+
}
1444+
1445+
} // namespace
1446+
14431447
Path Path::createPathWithRoundedCorners (const Path& originalPath, float cornerRadius)
14441448
{
14451449
if (cornerRadius <= 0.0f)

0 commit comments

Comments
 (0)