Broke some menu items up into interfaces. Happy with this so far.

This commit is contained in:
That_One_Nerd 2024-03-19 10:41:02 -04:00
parent 855a90b452
commit f5107b7238
6 changed files with 47 additions and 35 deletions

View File

@ -0,0 +1,8 @@
using Graphing.Graphables;
namespace Graphing.Abstract;
public interface IDerivable
{
public Equation Derive();
}

View File

@ -0,0 +1,8 @@
using Graphing.Graphables;
namespace Graphing.Abstract;
public interface IIntegrable
{
public IntegralEquation Integrate();
}

View File

@ -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)
{

View File

@ -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;

View File

@ -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();

View File

@ -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);
}
}