using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Linq.Expressions;
using ExpressionModifier;
namespace SimplePlots
{
public partial class MainPage : UserControl
{
public const double AXIS_LENGTH = 500d;
public const double AXIS_UNIT = 25d;
public MainPage()
{
InitializeComponent();
YAxis.Y2 = AXIS_LENGTH;
XAxis.X2 = AXIS_LENGTH;
XMarks.Width = AXIS_LENGTH;
YMarks.Height = AXIS_LENGTH;
SetAxes();
// uncomment the two lines below for a sinusoidal example
PlotMainAndModified(x => Math.Sin(-x), x => x * 2 + 3, 0.01, 10d);
AddPlot(Colors.Yellow, 0.01, x =>Math.Sin(-(x * 2 + 3)), 3d);
/*
// uncomment the two lines below for a parabola example
PlotMainAndModified(x => x * x, x => x * x - 2, 0.01, 10d);
AddPlot(Colors.Yellow, 0.01, x => (x * x - 2) * (x * x - 2), 3d);
*/
}
public void PlotMainAndModified(Expression<Func<double, double>> mainExpression, Expression<Func<double, double>> substExpression, double delta = 0.1, double pointSize = POINT_SIZE)
{
AddPlot(Colors.Red, delta, mainExpression.Compile(), pointSize);
LambdaExpression modifiedExpression =
mainExpression.Substitute("x", substExpression);
AddPlot(Colors.Blue, 0.01, modifiedExpression.Compile() as Func<double, double>, pointSize);
}
void SetAxes()
{
XMarks.Children.Clear();
YMarks.Children.Clear();
int currentNumber = (int) -(AXIS_LENGTH / AXIS_UNIT) / 2;
for (double x = 0; x <= AXIS_LENGTH; x += AXIS_UNIT)
{
Line xMark =
new Line
{
Stroke = new SolidColorBrush(Colors.Black),
StrokeThickness = 1,
X1 = x,
Y1 = -5,
X2 = x,
Y2 = 0
};
TextBlock xTextBlock = new TextBlock { Text = "" + currentNumber };
Canvas.SetLeft(xTextBlock, x - 5);
XMarks.Children.Add(xMark);
XMarks.Children.Add(xTextBlock);
double y = AXIS_LENGTH - x;
Line yMark =
new Line
{
Stroke = new SolidColorBrush(Colors.Black),
StrokeThickness = 1,
X1 = 5,
Y1 = y,
X2 = 0,
Y2 = y
};
TextBlock yTextBlock = new TextBlock { Text = "" + currentNumber };
Canvas.SetTop(yTextBlock, y - 10);
Canvas.SetLeft(yTextBlock, -17);
YMarks.Children.Add(yMark);
YMarks.Children.Add(yTextBlock);
currentNumber++;
}
}
const double POINT_SIZE = 5;
void AddPoint(double x, double y, Color color, double pointSize = POINT_SIZE)
{
Ellipse point =
new Ellipse
{
Fill = new SolidColorBrush(color),
Width = pointSize,
Height = pointSize
};
Canvas.SetLeft(point, x * 25 - pointSize / 2d);
Canvas.SetTop(point, -y * 25 - pointSize / 2d);
Plots.Children.Add(point);
}
public void AddPlot(Color color, double delta, Func<double, double> function, double pointSize)
{
double minX = -(AXIS_LENGTH / AXIS_UNIT) / 2;
double maxX = -minX;
for (double currentX = minX; currentX <= maxX; currentX += delta)
{
try
{
double y = function(currentX);
AddPoint(currentX, y, color, pointSize);
}
catch
{
}
}
}
}
}