private void GenerateDataByte() { if (InterpolationSpline == null) { InterpolationSpline = Interpolation.CreateRational(new double[] { 0, 0.5, 1 }, new double[] { 1, FallOff, 0 }); } mDataByte = new byte[Width * Height]; PointF midPoint = new PointF(Width / 2.0f, Height / 2.0f); for (uint i = 0; i < Width; ++i) { for (uint j = 0; j < Height; ++j) { byte value = 0; PointF curPos = new PointF(i, j); double distance = Math.Sqrt(Math.Pow(curPos.X - midPoint.X, 2) + Math.Pow(curPos.Y - midPoint.Y, 2)); if (distance < InnerRadius) { double coeff = InterpolationSpline.Interpolate(distance / InnerRadius); if (coeff < 0) { coeff = 0; } int curValue = (int)(mRandom.Next(0, 255) * coeff); value = (byte)curValue; } mDataByte[j * Height + i] = value; } } }
public SplineControl() { InitializeComponent(); mSplineInterpolation = Interpolation.CreateRational(new double[] { 0, mMidPoint.X, 1 }, new double[] { 1, mMidPoint.Y, 0 }); Paint += new PaintEventHandler(paintControl); MouseDown += new MouseEventHandler(mousePressed); MouseUp += new MouseEventHandler(mouseReleased); MouseMove += new MouseEventHandler(mouseMoved); }
void mouseMoved(object sender, MouseEventArgs e) { if (IsLeftDown) { int x = Math.Min(Math.Max(1, e.X), Width - 1); int y = Math.Min(Math.Max(1, e.Y), Height - 1); mMidPoint = new PointF(x / (float)Width, (Height - y) / (float)Height); mSplineInterpolation = Interpolation.CreateRational(new double[] { 0, mMidPoint.X, 1 }, new double[] { 1, mMidPoint.Y, 0 }); Invalidate(); if (SplineChanged != null) { SplineChanged(); } } }
public void TestInterpolationMethod_RationalWithPoles() { double[] t = new double[] { 0, 1, 3, 4, 5 }; double[] x = new double[] { 0, 3, 1000, -1000, 3 }; RationalInterpolation method = new RationalInterpolation(); method.Init(t, x); for (int i = 0; i < t.Length; i++) { Assert.That(method.Interpolate(t[i]), Is.EqualTo(x[i]), "Exact Point " + i.ToString()); } // Maple: "with(CurveFitting);" // Maple: "evalf(subs({x=0.1},RationalInterpolation([[0,0],[1,3],[3,1000],[4,-1000], [5,3]], x)),20);" Assert.That(method.Interpolate(0.1), NumericIs.AlmostEqualTo(.19389203383553566255, 1e-14), "A 0.1"); Assert.That(method.Interpolate(0.4), NumericIs.AlmostEqualTo(.88132900698869875369, 1e-14), "A 0.4"); Assert.That(method.Interpolate(1.1), NumericIs.AlmostEqualTo(3.5057665681580626913, 1e-15), "A 1.1"); Assert.That(method.Interpolate(3.01), NumericIs.AlmostEqualTo(1548.7666642693586902, 1e-13), "A 3.01"); Assert.That(method.Interpolate(3.02), NumericIs.AlmostEqualTo(3362.2564334253633516, 1e-13), "A 3.02"); Assert.That(method.Interpolate(3.03), NumericIs.AlmostEqualTo(-22332.603641443806014, 1e-12), "A 3.03"); Assert.That(method.Interpolate(3.1), NumericIs.AlmostEqualTo(-440.30323769822443789, 1e-14), "A 3.1"); Assert.That(method.Interpolate(3.2), NumericIs.AlmostEqualTo(-202.42421196280566349, 1e-14), "A 3.2"); Assert.That(method.Interpolate(4.5), NumericIs.AlmostEqualTo(21.208249625210155439, 1e-14), "A 4.5"); Assert.That(method.Interpolate(10.0), NumericIs.AlmostEqualTo(-4.8936986959784751517, 1e-13), "A 10.0"); Assert.That(method.Interpolate(-10.0), NumericIs.AlmostEqualTo(-3.6017584308603731307, 1e-13), "A -10.0"); // Test Linear Case for (int k = 2; k < 6; k++) { double[] linx, liny, linxtest, linytest; BuildLinearCase(2, k, out linx, out liny, out linxtest, out linytest); IInterpolationMethod linearMethod = Interpolation.CreateRational(linx, liny); for (int i = 0; i < linxtest.Length; i++) { // very weak test, but rational with poles is incredibly bad in the linear case Assert.That(linearMethod.Interpolate(linxtest[i]), Is.Not.NaN, String.Format("Linear k={0} i={1}", k, i)); } } }