Skip to content

Commit

Permalink
Merge pull request #11 from prekel/rootsdots
Browse files Browse the repository at this point in the history
Rootsdots
  • Loading branch information
prekel authored Feb 22, 2018
2 parents 697dd86 + a25ff36 commit 0559e14
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
60 changes: 48 additions & 12 deletions MyExpression.Wpf/FunctionGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,30 @@ public class FunctionGraph : Canvas, IEnumerable<FunctionGraph.DrawableFunction>

public Point Scale { get; set; }

private IList<DrawableFunction> Functions { get; set; } = new List<DrawableFunction>(1);
public IList<DrawableFunction> Functions { get; set; } = new List<DrawableFunction>(1);

public class DrawableFunction
{
public Func<double, double> Function { get; set; }
public SolidColorBrush Brush { get; set; } = Brushes.DarkMagenta;
public Interval DefinitionArea { get; set; }
public Func<double, double> Function { get; private set; }

public SolidColorBrush LineBrush { get; private set; } = Brushes.DarkMagenta;

public Interval DefinitionArea { get; private set; }

// TODO: разобраться с сеттером
public bool IsDrawed { get; set; }
public DrawableFunction(Func<double, double> f, Interval defarea, SolidColorBrush brush = null)

public SolidColorBrush RootsBrush { get; private set; } = Brushes.Indigo;

public IList<double> Roots { get; set; }

public DrawableFunction(Func<double, double> f, Interval defarea, SolidColorBrush brush = null, SolidColorBrush rootbrush = null, IList<double> roots = null)
{
Function = f;
DefinitionArea = new Interval(defarea.Left, defarea.Right);
if (brush != null) Brush = brush;
if (brush != null) LineBrush = brush;
if (rootbrush != null) RootsBrush = rootbrush;
if (roots != null) Roots = new List<double>(roots);
}
}

Expand Down Expand Up @@ -96,7 +107,7 @@ public void DrawFunction()
f.IsDrawed = true;
var l = new Polyline
{
Stroke = f.Brush,
Stroke = f.LineBrush,
StrokeThickness = 1
};
for (var i = f.DefinitionArea.Left; i <= f.DefinitionArea.Right; i += Step)
Expand All @@ -113,7 +124,7 @@ public void DrawFunction()
Children.Add(l);
l = new Polyline
{
Stroke = f.Brush,
Stroke = f.LineBrush,
StrokeThickness = 1
};
}
Expand All @@ -127,6 +138,29 @@ public void DrawFunction()
}
}

public void DrawRoots()
{
foreach (var f in Functions)
{
if (f.Roots is null) continue;
foreach (var i in f.Roots)
{
var wh = 5;
var p = new Ellipse
{
Width = wh,
Height = wh,
Fill = f.RootsBrush,
Stroke = Brushes.IndianRed,
StrokeThickness = 0.5
};
SetLeft(p, i * Scale.X - wh / 2.0);
SetTop(p, f.Function(i) * Scale.Y - wh / 2.0);
Children.Add(p);
}
}
}

public Point CellsStep { get; set; }

public Interval CellsIntervalX { get; set; }
Expand Down Expand Up @@ -197,10 +231,12 @@ public void ClearAll()
public void Clear()
{
Functions.Clear();
for (var i = 0; i < Children.Count; i++)
{
if (Children[i] is Polyline) Children.RemoveAt(i);
}
Children.Clear();
DrawAxis();
//for (var i = 0; i < Children.Count; i++)
//{
// if (Children[i] is Polyline) Children.RemoveAt(i);
//}
}

public IEnumerator<DrawableFunction> GetEnumerator() => Functions.GetEnumerator();
Expand Down
3 changes: 2 additions & 1 deletion MyExpression.Wpf/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<Button x:Name="ClearButton" Content="Clear" HorizontalAlignment="Left" Margin="180,232,0,0" VerticalAlignment="Top" Width="60" Click="ClearButton_Click"/>
<Label Content="Count" HorizontalAlignment="Left" Margin="66,33,0,0" VerticalAlignment="Top" Height="28" Width="49"/>
<Label x:Name="CountLabel" Content="0" HorizontalAlignment="Left" Margin="115,33,0,0" VerticalAlignment="Top" Width="45"/>
<local:BrushesList x:Name="GraphBrushComboBox" Margin="115,335,0,0" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left" Width="125"/>
<local:BrushesList x:Name="GraphBrushComboBox" Margin="10,335,0,0" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left" Width="150"/>
<Button x:Name="SolveButton" Content="Solve" HorizontalAlignment="Left" Margin="165,335,0,0" VerticalAlignment="Top" Width="75" Click="SolveButton_Click"/>
</Grid>
</Window>
16 changes: 16 additions & 0 deletions MyExpression.Wpf/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,21 @@ private void ClearButton_Click(object sender, RoutedEventArgs e)
MessageBox.Show(ex.StackTrace, ex.Message);
}
}

private void SolveButton_Click(object sender, RoutedEventArgs e)
{
try
{
var p = Core.Polynomial.Parse(Polynomial.Text);
var pe = new PolynomialEquation(p);
pe.Solve();
Graph.Functions.Last().Roots = new List<double>(pe.Roots);
Graph.DrawRoots();
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace, ex.Message);
}
}
}
}

0 comments on commit 0559e14

Please sign in to comment.