diff --git a/StructureHelper/Windows/MainGraph/GraphView.xaml b/StructureHelper/Windows/MainGraph/GraphView.xaml index 9d351fb..b6196f1 100644 --- a/StructureHelper/Windows/MainGraph/GraphView.xaml +++ b/StructureHelper/Windows/MainGraph/GraphView.xaml @@ -102,7 +102,7 @@ + Zoom="Xy"> diff --git a/StructureHelper/Windows/MainGraph/GraphViewModel.cs b/StructureHelper/Windows/MainGraph/GraphViewModel.cs index 4724035..8d3a99e 100644 --- a/StructureHelper/Windows/MainGraph/GraphViewModel.cs +++ b/StructureHelper/Windows/MainGraph/GraphViewModel.cs @@ -97,8 +97,12 @@ namespace StructureHelper.Windows.MainGraph f1.Name = "Табличная системная функция"; f1.Table = new List() { - new GraphPoint(1, 0), - new GraphPoint(0, 1), + new GraphPoint(1, 1), + new GraphPoint(2, 2), + new GraphPoint(3, 3), + new GraphPoint(4, 4), + new GraphPoint(5, 5), + new GraphPoint(6, 6), }; f1.IsUser = false; f1.Description = "Описание табличной системной функции"; diff --git a/StructureHelper/Windows/MainGraph/TableViewModel.cs b/StructureHelper/Windows/MainGraph/TableViewModel.cs index 2ce0520..f8a772a 100644 --- a/StructureHelper/Windows/MainGraph/TableViewModel.cs +++ b/StructureHelper/Windows/MainGraph/TableViewModel.cs @@ -107,7 +107,7 @@ namespace StructureHelper.Windows.MainGraph Function.Name = Name; Function.Description = Description; Function.IsUser = true; - (Function as TableFunction).Table = Table.ToList(); + (Function as TableFunction).Table = Table.OrderBy(x => x.X).ToList(); var window = parameter as Window; window.DialogResult = true; window.Close(); diff --git a/StructureHelper/Windows/TreeGraph/TreeGraphView.xaml b/StructureHelper/Windows/TreeGraph/TreeGraphView.xaml index be47681..9b47ad3 100644 --- a/StructureHelper/Windows/TreeGraph/TreeGraphView.xaml +++ b/StructureHelper/Windows/TreeGraph/TreeGraphView.xaml @@ -78,7 +78,7 @@ + Zoom="Xy"> diff --git a/StructureHelperCommon/FunctionDecorator.cd b/StructureHelperCommon/FunctionDecorator.cd index a903c04..cb33486 100644 --- a/StructureHelperCommon/FunctionDecorator.cd +++ b/StructureHelperCommon/FunctionDecorator.cd @@ -1,25 +1,61 @@  - + - BAACgEgAAAAgACIAAAAAQAYAAAAMAAAAAQAAAAAAgBA= + BAACgEgAAAAgBCIAAAAAQAYAAAAMAAAAAQAAgAAAgBA= Models\Functions\TableFunction.cs - + - BAACgEgAAAAgACJAAgAAQAYAAAAIBAAAIQAAAAAAgBA= + BAACgEgAAAAgBCIAAAAAAAYAAAAIBAAAIQAAgAAAgBA= Models\Functions\FormulaFunction.cs - - + + - BAAAgEgAAAAgACAAAAAAQAYAAAAIAAAAAQAAAAAAgAA= + AAAAAAgAAAAARAAAAAAAAAAAAAAIAAAAAAAAAAAAAAA= + Models\Functions\Decorator\ScaleYDecorator.cs + + + + + + AAAAAAgAAAAARAAAAAAAAAAAAAAIAAAAAAAAAAAAAAA= + Models\Functions\Decorator\ScaleXDecorator.cs + + + + + + AAAAAQgAAAAABAAAAAAAAAAAAAAIAAAAAAAAAAAAAAg= + Models\Functions\Decorator\LimYDecorator.cs + + + + + + AAAgAAgAAAAABAAAAAAAAAAAAAAIAAAAAAAAEAAAAAA= + Models\Functions\Decorator\LimXDecorator.cs + + + + + + BAACgEgAAAAgBAAAAgAAQAYAAAAIAAAAAQAAgAAAgBA= + Infrastructures\Interfaces\FunctionDecorator.cs + + + + + + + BAAAgEgAAAAgBCAAAAAAAAYAAAAIAAAAAQAAgAAAgAA= Infrastructures\Interfaces\IOneVariableFunction.cs diff --git a/StructureHelperCommon/Models/Functions/Decorator/LimXDecorator.cs b/StructureHelperCommon/Models/Functions/Decorator/LimXDecorator.cs index 794e083..ab6fac9 100644 --- a/StructureHelperCommon/Models/Functions/Decorator/LimXDecorator.cs +++ b/StructureHelperCommon/Models/Functions/Decorator/LimXDecorator.cs @@ -35,6 +35,9 @@ namespace StructureHelperCommon.Models.Functions.Decorator { var graphSettings = base.GetGraphSettings(); var graphLimitGraphPoint = new List(); + var leftPoint = new GraphPoint(leftBound, GetByX(leftBound)); + var rightPoint = new GraphPoint(rightBound, GetByX(rightBound)); + graphLimitGraphPoint.Add(leftPoint); foreach (GraphPoint point in graphSettings.GraphPoints) { if (point.X > leftBound && point.X < rightBound) @@ -42,6 +45,7 @@ namespace StructureHelperCommon.Models.Functions.Decorator graphLimitGraphPoint.Add(point); } } + graphLimitGraphPoint.Add(rightPoint); graphSettings.GraphPoints = graphLimitGraphPoint; return graphSettings; } diff --git a/StructureHelperCommon/Models/Functions/Decorator/LimYDecorator.cs b/StructureHelperCommon/Models/Functions/Decorator/LimYDecorator.cs index 22e15d2..1ed9b8b 100644 --- a/StructureHelperCommon/Models/Functions/Decorator/LimYDecorator.cs +++ b/StructureHelperCommon/Models/Functions/Decorator/LimYDecorator.cs @@ -36,6 +36,9 @@ namespace StructureHelperCommon.Models.Functions.Decorator { var graphSettings = base.GetGraphSettings(); var graphLimitGraphPoint = new List(); + var downPoint = new GraphPoint(downBound, GetByX(downBound)); + var upPoint = new GraphPoint(upBound, GetByX(upBound)); + graphLimitGraphPoint.Add(downPoint); foreach (GraphPoint point in graphSettings.GraphPoints) { if (point.Y > downBound && point.Y < upBound) @@ -43,6 +46,7 @@ namespace StructureHelperCommon.Models.Functions.Decorator graphLimitGraphPoint.Add(point); } } + graphLimitGraphPoint.Add(upPoint); graphSettings.GraphPoints = graphLimitGraphPoint; return graphSettings; } diff --git a/StructureHelperCommon/Models/Functions/FormulaFunction.cs b/StructureHelperCommon/Models/Functions/FormulaFunction.cs index bb08994..4a3c283 100644 --- a/StructureHelperCommon/Models/Functions/FormulaFunction.cs +++ b/StructureHelperCommon/Models/Functions/FormulaFunction.cs @@ -26,10 +26,8 @@ namespace StructureHelperCommon.Models.Functions public string Name { get; set; } public string Description { get ; set; } public int Step { get; set; } - public string Formula { get; set; } - + public string Formula { get; set; } public Guid Id => throw new NotImplementedException(); - public ObservableCollection Functions { get; set; } = new ObservableCollection(); public double MinArg { get; set; } public double MaxArg { get; set; } diff --git a/StructureHelperCommon/Models/Functions/GraphPoint.cs b/StructureHelperCommon/Models/Functions/GraphPoint.cs index 0a09608..35073d0 100644 --- a/StructureHelperCommon/Models/Functions/GraphPoint.cs +++ b/StructureHelperCommon/Models/Functions/GraphPoint.cs @@ -8,6 +8,7 @@ namespace StructureHelperCommon.Models.Functions { public class GraphPoint : ICloneable { + public bool Exclude { get; set; } public double X { get; set; } public double Y { get; set; } @@ -15,6 +16,7 @@ namespace StructureHelperCommon.Models.Functions { X = x; Y = y; + Exclude = false; } public object Clone() { diff --git a/StructureHelperCommon/Models/Functions/TableFunction.cs b/StructureHelperCommon/Models/Functions/TableFunction.cs index d01b10b..87d3269 100644 --- a/StructureHelperCommon/Models/Functions/TableFunction.cs +++ b/StructureHelperCommon/Models/Functions/TableFunction.cs @@ -27,7 +27,6 @@ namespace StructureHelperCommon.Models.Functions public string Name { get; set; } public string Description { get; set; } public List Table { get; set; } - public Guid Id => throw new NotImplementedException(); public ObservableCollection Functions { get; set; } = new ObservableCollection(); public double MinArg { get; set; } @@ -72,21 +71,36 @@ namespace StructureHelperCommon.Models.Functions public double GetByX(double xValue) { - //Реализовать взятие значения из таблицы и интерполяцию по таблице - - - - return 100; + GraphPoint leftBound = null; + GraphPoint rightBound = null; + for (int i = 0; i < Table.Count - 1; i++) + { + leftBound = Table[i]; + rightBound = Table[i + 1]; + if (xValue == leftBound.X) + { + return leftBound.Y; + } + else if (xValue > leftBound.X && xValue < rightBound.X) + { + return MathUtils.Interpolation(xValue, leftBound.X, rightBound.X, leftBound.Y, rightBound.Y); + } + else if (xValue == rightBound.X) + { + return rightBound.Y; + } + } + return 0; + //Можно добавить экстраполяцию } - public GraphSettings GetGraphSettings() { var graphSettings = new GraphSettings(Color, Color); - foreach(GraphPoint point in Table) + foreach (GraphPoint point in Table) { var graphPoint = new GraphPoint(point.X, GetByX(point.X)); graphSettings.GraphPoints.Add(graphPoint); - } + } return graphSettings; } } diff --git a/StructureHelperCommon/Services/MathUtils.cs b/StructureHelperCommon/Services/MathUtils.cs new file mode 100644 index 0000000..6e56a84 --- /dev/null +++ b/StructureHelperCommon/Services/MathUtils.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StructureHelperCommon.Services +{ + public static class MathUtils + { + public static double Interpolation(double x, double x1, double x2, double y1, double y2) + { + if (Math.Abs(x2 - x1) < 10E-3) + { + return (y1 + y2) / 2; + } + var y = (y2 - y1) * (x - x1) / (x2 - x1) + y1; + return y; + } + } +} diff --git a/StructureHelperTests/UnitTests/Interpolation/InterpolationTest.cs b/StructureHelperTests/UnitTests/Interpolation/InterpolationTest.cs new file mode 100644 index 0000000..97311da --- /dev/null +++ b/StructureHelperTests/UnitTests/Interpolation/InterpolationTest.cs @@ -0,0 +1,23 @@ +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StructureHelperTests.UnitTests.Interpolation +{ + public class InterpolationTest + { + [TestCase(1.5, 5.5, 1, 2, 5, 6)] + public void InterpolationTestMethod(double x, double y, double x1, double x2, double y1, double y2) + { + //Arrange + //Act + var result = StructureHelperCommon.Services.MathUtils.Interpolation(x, x1, x2, y1, y2); + //Assert + Assert.AreEqual(result, y); + + } + } +}