From f5107b72386381dbefe2b09833f9a47f5cb4c045 Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Tue, 19 Mar 2024 10:41:02 -0400 Subject: [PATCH] Broke some menu items up into interfaces. Happy with this so far. --- Base/Abstract/IDerivable.cs | 8 +++++++ Base/Abstract/IIntegrable.cs | 8 +++++++ Base/Forms/GraphForm.cs | 37 +++++++---------------------- Base/Graphables/Equation.cs | 10 ++++++-- Base/Graphables/IntegralEquation.cs | 16 +++++++++---- Testing/Program.cs | 3 +++ 6 files changed, 47 insertions(+), 35 deletions(-) create mode 100644 Base/Abstract/IDerivable.cs create mode 100644 Base/Abstract/IIntegrable.cs diff --git a/Base/Abstract/IDerivable.cs b/Base/Abstract/IDerivable.cs new file mode 100644 index 0000000..1d0a7e9 --- /dev/null +++ b/Base/Abstract/IDerivable.cs @@ -0,0 +1,8 @@ +using Graphing.Graphables; + +namespace Graphing.Abstract; + +public interface IDerivable +{ + public Equation Derive(); +} diff --git a/Base/Abstract/IIntegrable.cs b/Base/Abstract/IIntegrable.cs new file mode 100644 index 0000000..1a942a9 --- /dev/null +++ b/Base/Abstract/IIntegrable.cs @@ -0,0 +1,8 @@ +using Graphing.Graphables; + +namespace Graphing.Abstract; + +public interface IIntegrable +{ + public IntegralEquation Integrate(); +} diff --git a/Base/Forms/GraphForm.cs b/Base/Forms/GraphForm.cs index 2ea0c2f..839fb3e 100644 --- a/Base/Forms/GraphForm.cs +++ b/Base/Forms/GraphForm.cs @@ -1,4 +1,5 @@ -using Graphing.Graphables; +using Graphing.Abstract; +using Graphing.Graphables; using Graphing.Parts; using System; using System.Collections.Generic; @@ -362,22 +363,24 @@ public partial class GraphForm : Form colorItem.Click += (o, e) => GraphColorPickerButton_Click(able); MenuColors.DropDownItems.Add(colorItem); - if (able is Equation equ) + if (able is IDerivable derivable) { ToolStripMenuItem derivativeItem = new() { ForeColor = able.Color, Text = able.Name }; - derivativeItem.Click += (o, e) => EquationComputeDerivative_Click(equ); + derivativeItem.Click += (o, e) => Graph(derivable.Derive()); MenuEquationsDerivative.DropDownItems.Add(derivativeItem); - + } + if (able is IIntegrable integrable) + { ToolStripMenuItem integralItem = new() { ForeColor = able.Color, Text = able.Name }; - integralItem.Click += (o, e) => EquationComputeIntegral_Click(equ); + integralItem.Click += (o, e) => Graph(integrable.Integrate()); MenuEquationsIntegral.DropDownItems.Add(integralItem); } } @@ -409,30 +412,6 @@ public partial class GraphForm : Form Size = initialWindowSize; WindowState = FormWindowState.Normal; } - private void EquationComputeDerivative_Click(Equation equation) - { - EquationDelegate equ = equation.GetDelegate(); - string oldName = equation.Name, newName; - if (oldName.StartsWith("Derivative of ")) newName = "Second Derivative of " + oldName[14..]; - else if (oldName.StartsWith("Second Derivative of ")) newName = "Third Derivative of " + oldName[21..]; - else newName = "Derivative of " + oldName; - // TODO: anti-integrate (maybe). - - Graph(new Equation(DerivativeAtPoint(equ)) - { - Name = newName - }); - - static EquationDelegate DerivativeAtPoint(EquationDelegate e) - { - const double step = 1e-3; - return x => (e(x + step) - e(x)) / step; - } - } - private void EquationComputeIntegral_Click(Equation equation) - { - Graph(equation.Integrate()); - } private void MenuMiscCaches_Click(object? sender, EventArgs e) { diff --git a/Base/Graphables/Equation.cs b/Base/Graphables/Equation.cs index fa8859a..a32b961 100644 --- a/Base/Graphables/Equation.cs +++ b/Base/Graphables/Equation.cs @@ -1,11 +1,12 @@ -using Graphing.Forms; +using Graphing.Abstract; +using Graphing.Forms; using Graphing.Parts; using System; using System.Collections.Generic; namespace Graphing.Graphables; -public class Equation : Graphable +public class Equation : Graphable, IIntegrable, IDerivable { private static int equationNum; @@ -48,6 +49,11 @@ public class Equation : Graphable return lines; } + public Equation Derive() => new(x => + { + const double step = 1e-3; + return (equ(x + step) - equ(x)) / step; + }); public IntegralEquation Integrate() => new(this); public EquationDelegate GetDelegate() => equ; diff --git a/Base/Graphables/IntegralEquation.cs b/Base/Graphables/IntegralEquation.cs index 52ff821..0441800 100644 --- a/Base/Graphables/IntegralEquation.cs +++ b/Base/Graphables/IntegralEquation.cs @@ -1,11 +1,12 @@ -using Graphing.Forms; +using Graphing.Abstract; +using Graphing.Forms; using Graphing.Parts; using System; using System.Collections.Generic; namespace Graphing.Graphables; -public class IntegralEquation : Graphable +public class IntegralEquation : Graphable, IIntegrable, IDerivable { protected readonly Equation baseEqu; protected readonly EquationDelegate baseEquDel; @@ -113,11 +114,18 @@ public class IntegralEquation : Graphable return lines; } - public Equation AsEquation() => new(GetIntegralAtPoint); + public Equation AsEquation() => new(IntegralAtPoint) + { + Name = Name, + Color = Color + }; + + public Equation Derive() => (Equation)baseEqu.DeepCopy(); + public IntegralEquation Integrate() => AsEquation().Integrate(); // Standard integral method. // Inefficient for successive calls. - public double GetIntegralAtPoint(double x) + public double IntegralAtPoint(double x) { EquationDelegate equ = baseEqu.GetDelegate(); diff --git a/Testing/Program.cs b/Testing/Program.cs index 036d4ee..9638860 100644 --- a/Testing/Program.cs +++ b/Testing/Program.cs @@ -23,6 +23,9 @@ internal static class Program // Keep in mind this uses more memory than usual and can take // some time. + // Integrating equations is now much smoother and less intensive. + // Try it out! + Application.Run(graph); } }