Skip to content

Commit

Permalink
ScottPlot.Tools.Pad() to pad arrays with data on each side #255
Browse files Browse the repository at this point in the history
  • Loading branch information
swharden committed Apr 8, 2020
1 parent 37c5331 commit 9a655fc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions dev/changelog.md
Expand Up @@ -25,6 +25,10 @@ _ScottPlot uses [semantic](https://semver.org/) (major.minor.patch) versioning.
* Create a GDI rendering module which uses System.Drawing
* Create a SkiaSharp rendering module and user control (supporting OpenGL hardware acceleration)

## ScottPlot 4.0.29 (in development)
* `DataGen.Range()` now has `includeStop` argument to include the last value in the returned array.
* `Tools.Pad()` has been created to return a copy of a given array padded with data values on each side. (#255) _Thanks @ckovamees_

## ScottPlot 4.0.28
* `Ticks()` now has arguments for numericStringFormat (X and Y) to make it easy to customize formatting of tick labels (percentage, currency, scientific notation, etc.) using standard [numeric format strings](https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings). Example use is demonstrated in the cookbook. (#336) _Thanks @deiruch_
* The right-click menu can now be more easily customized by writing a custom menu to `FormsPlot.ContextMenuStrip` or `WpfPlot.ContextMenu`. Demonstrations of both are in the demo application. (#337) _Thanks @Antracik_
Expand Down
24 changes: 24 additions & 0 deletions src/ScottPlot/Tools.cs
Expand Up @@ -280,5 +280,29 @@ public static double[] Round(double[] data, int decimals = 2)
rounded[i] = Math.Round(data[i], decimals);
return rounded;
}

/// <summary>
/// return a copy of the given array padded with the given value at both sidees
/// </summary>
public static double[] Pad(double[] values, int padCount = 1, double padWithLeft = 0, double padWithRight = 0, bool cloneEdges = false)
{
double[] padded = new double[values.Length + padCount * 2];

Array.Copy(values, 0, padded, padCount, values.Length);

if (cloneEdges)
{
padWithLeft = values[0];
padWithRight = values[values.Length - 1];
}

for (int i = 0; i < padCount; i++)
{
padded[i] = padWithLeft;
padded[padded.Length - 1 - i] = padWithRight;
}

return padded;
}
}
}
37 changes: 37 additions & 0 deletions tests/Tools/Pad.cs
@@ -0,0 +1,37 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Text;

namespace ScottPlotTests.Tools
{
class Pad
{
[Test]
public void Test_Pad_NoArguments()
{
double[] values = { 1, 2, 3 };
double[] result = ScottPlot.Tools.Pad(values);

Assert.AreEqual(new double[] { 0, 1, 2, 3, 0 }, result);
}

[Test]
public void Test_Pad_MultipleDefined()
{
double[] values = { 1, 2, 3 };
double[] result = ScottPlot.Tools.Pad(values, 3, -1, -2);

Assert.AreEqual(new double[] { -1, -1, -1, 1, 2, 3, -2, -2, -2 }, result);
}

[Test]
public void Test_Pad_CloneEdges()
{
double[] values = { 1, 2, 3 };
double[] result = ScottPlot.Tools.Pad(values, 3, cloneEdges: true);

Assert.AreEqual(new double[] { 1, 1, 1, 1, 2, 3, 3, 3, 3 }, result);
}
}
}

0 comments on commit 9a655fc

Please sign in to comment.