Skip to content

Instantly share code, notes, and snippets.

@oshea00
Created September 28, 2015 05:25
Show Gist options
  • Save oshea00/152d558b6e4012b6c3c4 to your computer and use it in GitHub Desktop.
Save oshea00/152d558b6e4012b6c3c4 to your computer and use it in GitHub Desktop.
void Main()
{
var g = new Grid();
var w = new Window { Width = 420, Height = 440, Title = "Graph", Content = g};
var c = new Canvas { Background = Brushes.BlanchedAlmond };
DrawLine(200,10,200,390,c);
DrawLine(10,200,390,200,c);
double x1 = 150;
double y1 = 100;
double x2 = 300;
double y2 = 220;
DrawText(10,10,c,12,string.Format("p0 = ({0},{1})",x1,y1));
DrawText(10,22,c,12,string.Format( "pend = ({0},{1})",x2,y2));
// Parametric
// p(t) = p0 + td
DrawText(10,34,c,12,"Parametric: p(t)=p0+td");
var dx = x2-x1;
var dy = y2-y1;
var dnorm = Math.Sqrt(dx*dx+dy*dy);
var nx = dx / dnorm;
var ny = dy / dnorm;
// Parametric
DrawText(10,46,c,12,string.Format( "p(t) = ({0},{1}) + t({2},{3})",x1,y1,dx,dy));
// Implicit
DrawText(10,58,c,12,"Implicit: ax+by=d");
DrawText(10,70,c,12,string.Format("{0}x+{1}y={2}",Math.Round(ny,2),Math.Round(-nx,2),Math.Round(x1*ny-y1*nx,2)));
// y=mx+b form
var d = Math.Round(x1*ny-y1*nx,2);
var m = -dy/-dx;
var y0 = d/-dx;
DrawText(10,82,c,12,"Implicit: y=mx+p0");
DrawText(10,94,c,12,string.Format("y={0}x+{1}",Math.Round(m,2),Math.Round(y0,2)));
DrawLine((int)x1,(int)y1,(int)x2,(int)y2,c,Brushes.Red);
g.Children.Add(c);
w.ShowDialog();
}
public void DrawText(int x, int y, Canvas c, double size, string text)
{
var t = new TextBlock { FontFamily = new FontFamily("Courier New"), FontSize = size, Text = text };
Canvas.SetLeft(t,x);
Canvas.SetTop(t,y);
c.Children.Add(t);
}
public void DrawPoint(int x, int y, Canvas c)
{
var e = new Ellipse { Height = 6, Width = 6, Fill = Brushes.Blue, Stroke = Brushes.Black };
Canvas.SetLeft(e,x);
Canvas.SetTop(e,y);
c.Children.Add(e);
}
public void DrawLine(int x1, int y1, int x2, int y2, Canvas c)
{
var l = new Line{ X1 = x1, Y1 = y1, X2 = x2, Y2 = y2, Stroke = Brushes.Black, StrokeThickness = 1 };
c.Children.Add(l);
}
public void DrawLine(int x1, int y1, int x2, int y2, Canvas c, Brush color)
{
var l = new Line{ X1 = x1, Y1 = y1, X2 = x2, Y2 = y2, Stroke = color, StrokeThickness = 1 };
c.Children.Add(l);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment