Refactoring

This commit is contained in:
Isaac 2024-05-18 20:13:30 +04:00
parent 6f63e9a624
commit 6bebe70b4a
2 changed files with 62 additions and 62 deletions

View File

@ -7,7 +7,7 @@ BezierPath makeEllipseBezierPath(
Vector2D const &center, Vector2D const &center,
PathDirection direction PathDirection direction
) { ) {
const double ControlPointConstant = 0.55228; const float ControlPointConstant = 0.55228;
Vector2D half = size * 0.5; Vector2D half = size * 0.5;
if (direction == PathDirection::CounterClockwise) { if (direction == PathDirection::CounterClockwise) {
@ -51,13 +51,13 @@ BezierPath makeEllipseBezierPath(
BezierPath makeRectangleBezierPath( BezierPath makeRectangleBezierPath(
Vector2D const &position, Vector2D const &position,
Vector2D const &inputSize, Vector2D const &inputSize,
double cornerRadius, float cornerRadius,
PathDirection direction PathDirection direction
) { ) {
const double ControlPointConstant = 0.55228; const float ControlPointConstant = 0.55228;
Vector2D size = inputSize * 0.5; Vector2D size = inputSize * 0.5;
double radius = std::min(std::min(cornerRadius, (double)size.x), (double)size.y); float radius = std::min(std::min(cornerRadius, (float)size.x), (float)size.y);
BezierPath bezierPath; BezierPath bezierPath;
std::vector<CurveVertex> points; std::vector<CurveVertex> points;
@ -97,7 +97,7 @@ BezierPath makeRectangleBezierPath(
.translated(position) .translated(position)
}; };
} else { } else {
double controlPoint = radius * ControlPointConstant; float controlPoint = radius * ControlPointConstant;
points = { points = {
/// Lead In /// Lead In
CurveVertex::absolute( CurveVertex::absolute(
@ -184,28 +184,28 @@ BezierPath makeRectangleBezierPath(
} }
/// Magic number needed for building path data /// Magic number needed for building path data
static constexpr double StarNodePolystarConstant = 0.47829; static constexpr float StarNodePolystarConstant = 0.47829;
BezierPath makeStarBezierPath( BezierPath makeStarBezierPath(
Vector2D const &position, Vector2D const &position,
double outerRadius, float outerRadius,
double innerRadius, float innerRadius,
double inputOuterRoundedness, float inputOuterRoundedness,
double inputInnerRoundedness, float inputInnerRoundedness,
double numberOfPoints, float numberOfPoints,
double rotation, float rotation,
PathDirection direction PathDirection direction
) { ) {
double currentAngle = degreesToRadians(rotation - 90.0); float currentAngle = degreesToRadians(rotation - 90.0);
double anglePerPoint = (2.0 * M_PI) / numberOfPoints; float anglePerPoint = (2.0 * M_PI) / numberOfPoints;
double halfAnglePerPoint = anglePerPoint / 2.0; float halfAnglePerPoint = anglePerPoint / 2.0;
double partialPointAmount = numberOfPoints - floor(numberOfPoints); float partialPointAmount = numberOfPoints - floor(numberOfPoints);
double outerRoundedness = inputOuterRoundedness * 0.01; float outerRoundedness = inputOuterRoundedness * 0.01;
double innerRoundedness = inputInnerRoundedness * 0.01; float innerRoundedness = inputInnerRoundedness * 0.01;
Vector2D point = Vector2D::Zero(); Vector2D point = Vector2D::Zero();
double partialPointRadius = 0.0; float partialPointRadius = 0.0;
if (partialPointAmount != 0.0) { if (partialPointAmount != 0.0) {
currentAngle += halfAnglePerPoint * (1 - partialPointAmount); currentAngle += halfAnglePerPoint * (1 - partialPointAmount);
partialPointRadius = innerRadius + partialPointAmount * (outerRadius - innerRadius); partialPointRadius = innerRadius + partialPointAmount * (outerRadius - innerRadius);
@ -225,8 +225,8 @@ BezierPath makeStarBezierPath(
bool longSegment = false; bool longSegment = false;
int numPoints = (int)(ceil(numberOfPoints) * 2.0); int numPoints = (int)(ceil(numberOfPoints) * 2.0);
for (int i = 0; i < numPoints; i++) { for (int i = 0; i < numPoints; i++) {
double radius = longSegment ? outerRadius : innerRadius; float radius = longSegment ? outerRadius : innerRadius;
double dTheta = halfAnglePerPoint; float dTheta = halfAnglePerPoint;
if (partialPointRadius != 0.0 && i == numPoints - 2) { if (partialPointRadius != 0.0 && i == numPoints - 2) {
dTheta = anglePerPoint * partialPointAmount / 2; dTheta = anglePerPoint * partialPointAmount / 2;
} }
@ -240,18 +240,18 @@ BezierPath makeStarBezierPath(
if (innerRoundedness == 0.0 && outerRoundedness == 0.0) { if (innerRoundedness == 0.0 && outerRoundedness == 0.0) {
vertices.push_back(CurveVertex::relative(point + position, Vector2D::Zero(), Vector2D::Zero())); vertices.push_back(CurveVertex::relative(point + position, Vector2D::Zero(), Vector2D::Zero()));
} else { } else {
double cp1Theta = (atan2(previousPoint.y, previousPoint.x) - M_PI / 2.0); float cp1Theta = (atan2(previousPoint.y, previousPoint.x) - M_PI / 2.0);
double cp1Dx = cos(cp1Theta); float cp1Dx = cos(cp1Theta);
double cp1Dy = sin(cp1Theta); float cp1Dy = sin(cp1Theta);
double cp2Theta = (atan2(point.y, point.x) - M_PI / 2.0); float cp2Theta = (atan2(point.y, point.x) - M_PI / 2.0);
double cp2Dx = cos(cp2Theta); float cp2Dx = cos(cp2Theta);
double cp2Dy = sin(cp2Theta); float cp2Dy = sin(cp2Theta);
double cp1Roundedness = longSegment ? innerRoundedness : outerRoundedness; float cp1Roundedness = longSegment ? innerRoundedness : outerRoundedness;
double cp2Roundedness = longSegment ? outerRoundedness : innerRoundedness; float cp2Roundedness = longSegment ? outerRoundedness : innerRoundedness;
double cp1Radius = longSegment ? innerRadius : outerRadius; float cp1Radius = longSegment ? innerRadius : outerRadius;
double cp2Radius = longSegment ? outerRadius : innerRadius; float cp2Radius = longSegment ? outerRadius : innerRadius;
Vector2D cp1( Vector2D cp1(
cp1Radius * cp1Roundedness * StarNodePolystarConstant * cp1Dx, cp1Radius * cp1Roundedness * StarNodePolystarConstant * cp1Dx,
@ -295,7 +295,7 @@ BezierPath makeStarBezierPath(
return path; return path;
} }
CompoundBezierPath trimCompoundPath(CompoundBezierPath sourcePath, double start, double end, double offset, TrimType type) { CompoundBezierPath trimCompoundPath(CompoundBezierPath sourcePath, float start, float end, float offset, TrimType type) {
/// No need to trim, it's a full path /// No need to trim, it's a full path
if (start == 0.0 && end == 1.0) { if (start == 0.0 && end == 1.0) {
return sourcePath; return sourcePath;
@ -328,8 +328,8 @@ CompoundBezierPath trimCompoundPath(CompoundBezierPath sourcePath, double start,
/// Brace yourself for the below code. /// Brace yourself for the below code.
/// Normalize lengths with offset. /// Normalize lengths with offset.
double startPosition = fmod(start + offset, 1.0); float startPosition = fmod(start + offset, 1.0);
double endPosition = fmod(end + offset, 1.0); float endPosition = fmod(end + offset, 1.0);
if (startPosition < 0.0) { if (startPosition < 0.0) {
startPosition = 1.0 + startPosition; startPosition = 1.0 + startPosition;
@ -346,15 +346,15 @@ CompoundBezierPath trimCompoundPath(CompoundBezierPath sourcePath, double start,
} }
/// First get the total length of all paths. /// First get the total length of all paths.
double totalLength = 0.0; float totalLength = 0.0;
for (auto &upstreamPath : sourcePath.paths) { for (auto &upstreamPath : sourcePath.paths) {
totalLength += upstreamPath.length(); totalLength += upstreamPath.length();
} }
/// Now determine the start and end cut lengths /// Now determine the start and end cut lengths
double startLength = startPosition * totalLength; float startLength = startPosition * totalLength;
double endLength = endPosition * totalLength; float endLength = endPosition * totalLength;
double pathStart = 0.0; float pathStart = 0.0;
CompoundBezierPath result; CompoundBezierPath result;
@ -367,12 +367,12 @@ CompoundBezierPath trimCompoundPath(CompoundBezierPath sourcePath, double start,
// pathStart|=======E----------------------|pathEnd // pathStart|=======E----------------------|pathEnd
// Cut path components, removing after end. // Cut path components, removing after end.
double pathCutLength = endLength - pathStart; float pathCutLength = endLength - pathStart;
double subpathStart = 0.0; float subpathStart = 0.0;
double subpathEnd = subpathStart + pathContainer.length(); float subpathEnd = subpathStart + pathContainer.length();
if (pathCutLength < subpathEnd) { if (pathCutLength < subpathEnd) {
/// This is the subpath that needs to be cut. /// This is the subpath that needs to be cut.
double cutLength = pathCutLength - subpathStart; float cutLength = pathCutLength - subpathStart;
CompoundBezierPath tempPath; CompoundBezierPath tempPath;
tempPath.appendPath(pathContainer); tempPath.appendPath(pathContainer);
@ -395,14 +395,14 @@ CompoundBezierPath trimCompoundPath(CompoundBezierPath sourcePath, double start,
// //
// Cut path components, removing before beginning. // Cut path components, removing before beginning.
double pathCutLength = startLength - pathStart; float pathCutLength = startLength - pathStart;
// Clear paths from container // Clear paths from container
double subpathStart = 0.0; float subpathStart = 0.0;
double subpathEnd = subpathStart + pathContainer.length(); float subpathEnd = subpathStart + pathContainer.length();
if (subpathStart < pathCutLength && pathCutLength < subpathEnd) { if (subpathStart < pathCutLength && pathCutLength < subpathEnd) {
/// This is the subpath that needs to be cut. /// This is the subpath that needs to be cut.
double cutLength = pathCutLength - subpathStart; float cutLength = pathCutLength - subpathStart;
CompoundBezierPath tempPath; CompoundBezierPath tempPath;
tempPath.appendPath(pathContainer); tempPath.appendPath(pathContainer);
auto newPaths = tempPath.trim(cutLength / pathContainer.length(), 1, 0); auto newPaths = tempPath.trim(cutLength / pathContainer.length(), 1, 0);
@ -420,12 +420,12 @@ CompoundBezierPath trimCompoundPath(CompoundBezierPath sourcePath, double start,
// trim from path beginning to endLength. // trim from path beginning to endLength.
// Cut path components, removing before beginnings. // Cut path components, removing before beginnings.
double startCutLength = startLength - pathStart; float startCutLength = startLength - pathStart;
double endCutLength = endLength - pathStart; float endCutLength = endLength - pathStart;
double subpathStart = 0.0; float subpathStart = 0.0;
double subpathEnd = subpathStart + pathContainer.length(); float subpathEnd = subpathStart + pathContainer.length();
if (!isInRange(startCutLength, subpathStart, subpathEnd) && if (!isInRange(startCutLength, subpathStart, subpathEnd) &&
!isInRange(endCutLength, subpathStart, subpathEnd)) !isInRange(endCutLength, subpathStart, subpathEnd))
@ -437,7 +437,7 @@ CompoundBezierPath trimCompoundPath(CompoundBezierPath sourcePath, double start,
!isInRange(endCutLength, subpathStart, subpathEnd)) { !isInRange(endCutLength, subpathStart, subpathEnd)) {
/// The start of the path needs to be trimmed /// The start of the path needs to be trimmed
// |-------S======================|E // |-------S======================|E
double cutLength = startCutLength - subpathStart; float cutLength = startCutLength - subpathStart;
CompoundBezierPath tempPath; CompoundBezierPath tempPath;
tempPath.appendPath(pathContainer); tempPath.appendPath(pathContainer);
auto newPaths = tempPath.trim(cutLength / pathContainer.length(), 1, 0); auto newPaths = tempPath.trim(cutLength / pathContainer.length(), 1, 0);
@ -447,7 +447,7 @@ CompoundBezierPath trimCompoundPath(CompoundBezierPath sourcePath, double start,
} else if (!isInRange(startCutLength, subpathStart, subpathEnd) && } else if (!isInRange(startCutLength, subpathStart, subpathEnd) &&
isInRange(endCutLength, subpathStart, subpathEnd)) { isInRange(endCutLength, subpathStart, subpathEnd)) {
// S|=======E----------------------| // S|=======E----------------------|
double cutLength = endCutLength - subpathStart; float cutLength = endCutLength - subpathStart;
CompoundBezierPath tempPath; CompoundBezierPath tempPath;
tempPath.appendPath(pathContainer); tempPath.appendPath(pathContainer);
auto newPaths = tempPath.trim(0, cutLength / pathContainer.length(), 0); auto newPaths = tempPath.trim(0, cutLength / pathContainer.length(), 0);
@ -457,8 +457,8 @@ CompoundBezierPath trimCompoundPath(CompoundBezierPath sourcePath, double start,
} else if (isInRange(startCutLength, subpathStart, subpathEnd) && } else if (isInRange(startCutLength, subpathStart, subpathEnd) &&
isInRange(endCutLength, subpathStart, subpathEnd)) { isInRange(endCutLength, subpathStart, subpathEnd)) {
// |-------S============E---------| // |-------S============E---------|
double cutFromLength = startCutLength - subpathStart; float cutFromLength = startCutLength - subpathStart;
double cutToLength = endCutLength - subpathStart; float cutToLength = endCutLength - subpathStart;
CompoundBezierPath tempPath; CompoundBezierPath tempPath;
tempPath.appendPath(pathContainer); tempPath.appendPath(pathContainer);
auto newPaths = tempPath.trim( auto newPaths = tempPath.trim(

View File

@ -17,22 +17,22 @@ BezierPath makeEllipseBezierPath(
BezierPath makeRectangleBezierPath( BezierPath makeRectangleBezierPath(
Vector2D const &position, Vector2D const &position,
Vector2D const &inputSize, Vector2D const &inputSize,
double cornerRadius, float cornerRadius,
PathDirection direction PathDirection direction
); );
BezierPath makeStarBezierPath( BezierPath makeStarBezierPath(
Vector2D const &position, Vector2D const &position,
double outerRadius, float outerRadius,
double innerRadius, float innerRadius,
double inputOuterRoundedness, float inputOuterRoundedness,
double inputInnerRoundedness, float inputInnerRoundedness,
double numberOfPoints, float numberOfPoints,
double rotation, float rotation,
PathDirection direction PathDirection direction
); );
CompoundBezierPath trimCompoundPath(CompoundBezierPath sourcePath, double start, double end, double offset, TrimType type); CompoundBezierPath trimCompoundPath(CompoundBezierPath sourcePath, float start, float end, float offset, TrimType type);
} }