Skip to content

Commit c6b6b9a

Browse files
committed
add some c# magic and simplify some code
1 parent 38ba13c commit c6b6b9a

1 file changed

Lines changed: 23 additions & 33 deletions

File tree

NickvisionCavalier.Shared/Models/Renderer.cs

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ private float FlipCoord(bool enabled, float screenDimension, float coordinate)
434434
var step = (direction < DrawingDirection.LeftRight ? width : height) / (sample.Length - 1);
435435
var path = new SKPath();
436436
var flipImage = false;
437-
var pointsArray = new Tuple<float, float>[sample.Length];
437+
var pointsArray = new (float x, float y)[sample.Length];
438438
var gradientsList = new float[sample.Length];
439439
switch (direction)
440440
{
@@ -443,7 +443,7 @@ private float FlipCoord(bool enabled, float screenDimension, float coordinate)
443443
// Create a list of point of where the the curve must pass through
444444
for (var i = 0; i < sample.Length; i++)
445445
{
446-
pointsArray[i] = Tuple.Create(step * i, height * (1 - sample[i]));
446+
pointsArray[i] = (step * i, height * (1 - sample[i]));
447447
}
448448
// Calculate gradient between the two neighbouring points for every point
449449
for (var i = 0; i < pointsArray.Length; i++)
@@ -452,27 +452,22 @@ private float FlipCoord(bool enabled, float screenDimension, float coordinate)
452452
// If there isn't one, use the current point
453453
var previousPoint = pointsArray[Math.Max(i - 1, 0)];
454454
var nextPoint = pointsArray[Math.Min(i + 1, pointsArray.Length - 1)];
455+
var gradient = nextPoint.y - previousPoint.y;
455456
// If using the current point (when at the edges)
456457
// then the run in rise/run = 1, otherwise a two step run exists
457-
if (i == 0 || i == pointsArray.Length - 1)
458-
{
459-
gradientsList[i] = nextPoint.Item2 - previousPoint.Item2;
460-
}
461-
else
462-
{
463-
gradientsList[i] = (nextPoint.Item2 - previousPoint.Item2) / 2;
464-
}
458+
gradientsList[i] = i == 0 || i == pointsArray.Length - 1 ? gradient : gradient / 2;
465459
}
466-
path.MoveTo(x + pointsArray[0].Item1, y + FlipCoord(flipImage, height, pointsArray[0].Item2) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2));
460+
var yOffset = y + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2);
461+
path.MoveTo(x + pointsArray[0].x, yOffset + FlipCoord(flipImage, height, pointsArray[0].y));
467462
for (var i = 0; i < pointsArray.Length - 1; i++)
468463
{
469464
path.CubicTo(
470-
x + pointsArray[i].Item1 + step * 0.5f,
471-
y + FlipCoord(flipImage, height, pointsArray[i].Item2 + gradientsList[i] * 0.5f) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
472-
x + pointsArray[i + 1].Item1 + step * -0.5f,
473-
y + FlipCoord(flipImage, height, pointsArray[i + 1].Item2 + gradientsList[i + 1] * -0.5f) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
474-
x + pointsArray[i + 1].Item1,
475-
y + FlipCoord(flipImage, height, pointsArray[i + 1].Item2) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2));
465+
x + pointsArray[i].x + step * 0.5f,
466+
yOffset + FlipCoord(flipImage, height, pointsArray[i].y + gradientsList[i] * 0.5f),
467+
x + pointsArray[i + 1].x + step * -0.5f,
468+
yOffset + FlipCoord(flipImage, height, pointsArray[i + 1].y + gradientsList[i + 1] * -0.5f),
469+
x + pointsArray[i + 1].x,
470+
yOffset + FlipCoord(flipImage, height, pointsArray[i + 1].y));
476471
}
477472
if (Configuration.Current.Filling)
478473
{
@@ -485,31 +480,26 @@ private float FlipCoord(bool enabled, float screenDimension, float coordinate)
485480
flipImage = direction == DrawingDirection.RightLeft;
486481
for (var i = 0; i < sample.Length; i++)
487482
{
488-
pointsArray[i] = Tuple.Create(width * sample[i], step * i);
483+
pointsArray[i] = (width * sample[i], step * i);
489484
}
490485
for (var i = 0; i < pointsArray.Length; i++)
491486
{
492487
var previousPoint = pointsArray[Math.Max(i - 1, 0)];
493488
var nextPoint = pointsArray[Math.Min(i + 1, pointsArray.Length - 1)];
494-
if (i == 0 || i == pointsArray.Length - 1)
495-
{
496-
gradientsList[i] = nextPoint.Item1 - previousPoint.Item1;
497-
}
498-
else
499-
{
500-
gradientsList[i] = (nextPoint.Item1 - previousPoint.Item1) / 2;
501-
}
489+
var gradient = nextPoint.x - previousPoint.x;
490+
gradientsList[i] = i == 0 || i == pointsArray.Length - 1 ? gradient : gradient / 2;
502491
}
503-
path.MoveTo(x + FlipCoord(flipImage, width, pointsArray[0].Item1) - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2), y + pointsArray[0].Item2);
492+
var xOffset = x - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2);
493+
path.MoveTo(xOffset + FlipCoord(flipImage, width, pointsArray[0].x), y + pointsArray[0].y);
504494
for (var i = 0; i < pointsArray.Length - 1; i++)
505495
{
506496
path.CubicTo(
507-
x + FlipCoord(flipImage, width, pointsArray[i].Item1 + gradientsList[i] * 0.5f) - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
508-
y + pointsArray[i].Item2 + step * 0.5f,
509-
x + FlipCoord(flipImage, width, pointsArray[i + 1].Item1 + gradientsList[i + 1] * -0.5f) - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
510-
y + pointsArray[i + 1].Item2 + step * -0.5f,
511-
x + FlipCoord(flipImage, width, pointsArray[i + 1].Item1) - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
512-
y + pointsArray[i + 1].Item2);
497+
xOffset + FlipCoord(flipImage, width, pointsArray[i].x + gradientsList[i] * 0.5f),
498+
y + pointsArray[i].y + step * 0.5f,
499+
xOffset + FlipCoord(flipImage, width, pointsArray[i + 1].x + gradientsList[i + 1] * -0.5f),
500+
y + pointsArray[i + 1].y + step * -0.5f,
501+
xOffset + FlipCoord(flipImage, width, pointsArray[i + 1].x),
502+
y + pointsArray[i + 1].y);
513503
}
514504
if (Configuration.Current.Filling)
515505
{

0 commit comments

Comments
 (0)