From 3b6ebc7b9977e0b2f8afa3a1e3bacd03260cc3b1 Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Tue, 2 Apr 2024 09:53:42 -0400 Subject: [PATCH] Some quick parametrics. See how fast it is to homebrew this? I know I haven't completed a bunch of stuff but I'll get to it soon. --- Base/Graphables/ParametricEquation.cs | 63 +++++++++++++++++++++++++++ Testing/Program.cs | 3 +- 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 Base/Graphables/ParametricEquation.cs diff --git a/Base/Graphables/ParametricEquation.cs b/Base/Graphables/ParametricEquation.cs new file mode 100644 index 0000000..0d92743 --- /dev/null +++ b/Base/Graphables/ParametricEquation.cs @@ -0,0 +1,63 @@ +using Graphing.Abstract; +using Graphing.Forms; +using Graphing.Parts; +using System; +using System.Collections.Generic; + +namespace Graphing.Graphables; + +public class ParametricEquation : Graphable, ITranslatableXY +{ + private static int equationNum; + + public double OffsetX { get; set; } + public double OffsetY { get; set; } + + public double InitialT { get; set; } + public double FinalT { get; set; } + + protected readonly ParametricDelegate equX, equY; + + public ParametricEquation(double initialT, double finalT, + ParametricDelegate equX, ParametricDelegate equY) + { + equationNum++; + Name = $"Parametric Equation {equationNum}"; + + InitialT = initialT; + FinalT = finalT; + + this.equX = equX; + this.equY = equY; + } + + public override Graphable ShallowCopy() => new ParametricEquation(InitialT, FinalT, equX, equY); + + public override IEnumerable GetItemsToRender(in GraphForm graph) + { + const int step = 10; + + double epsilon = Math.Abs(graph.ScreenSpaceToGraphSpace(new Int2(0, 0)).x + - graph.ScreenSpaceToGraphSpace(new Int2(step / 2, 0)).x) / 5; + epsilon *= graph.DpiFloat / 192; + + List lines = []; + + Float2 previousPoint = GetPointAt(InitialT); + for (double t = InitialT; t <= FinalT; t += epsilon) + { + Float2 currentPoint = GetPointAt(t); + lines.Add(new GraphLine(previousPoint, currentPoint)); + previousPoint = currentPoint; + } + + return lines; + } + + public Float2 GetPointAt(double t) + { + return new(equX(t) + OffsetX, equY(t) + OffsetY); + } +} + +public delegate double ParametricDelegate(double t); diff --git a/Testing/Program.cs b/Testing/Program.cs index 2e256f7..333cacb 100644 --- a/Testing/Program.cs +++ b/Testing/Program.cs @@ -19,7 +19,8 @@ internal static class Program Equation equA = new(Math.Sin), equB = new(Math.Cos); EquationDifference diff = new(2, equA, equB); - graph.Graph(equA, equB, diff); + ParametricEquation equC = new(0, 4, t => t * t - 1, t => Math.Sqrt(t) + t + 1); + graph.Graph(equA, equB, diff, equC); Application.Run(graph); }