void AddPointNormalized(MathHelpers.Point p) { int m = (int)mode; CurvePoint cp = new CurvePoint(this); cp.HorizontalAlignment = HorizontalAlignment.Left; cp.VerticalAlignment = VerticalAlignment.Top; cp.Width = 8; cp.Height = 8; cp.MouseDown += Point_MouseDown; cp.MouseUp += Point_MouseUp; cp.MouseMove += Point_MouseMove; cp.Normalized = p; Points[m].Add(cp); CurveView.Children.Add(cp); }
private void Point_MouseMove(object sender, MouseEventArgs e) { if (target == sender) { e.Handled = true; Point ep = e.GetPosition(CurveView); double dx = ep.X - mouseStart.X; double dy = ep.Y - mouseStart.Y; CurvePoint cp = target as CurvePoint; MathHelpers.Point p = cp.Position; p.X += dx; p.Y += dy; cp.Position = p; mouseStart = ep; //update curve UpdatePath(); UpdateProperty(); } }
/// <summary> /// Renderes the path to the buffer /// and returns the normalized curve data /// the path should be a normalized set of points from 0 - 1 /// </summary> /// <param name="path"></param> /// <param name="buffer"></param> /// <returns></returns> protected List <MathHelpers.Point> RenderPath(List <CurvePoint> path, RawBitmap buffer, byte r = 175, byte g = 175, byte b = 175) { List <MathHelpers.Point> points = new List <MathHelpers.Point>(); List <MathHelpers.Point> curve = new List <MathHelpers.Point>(); List <MathHelpers.Point> normalized = new List <MathHelpers.Point>(); double width = CurveView.ActualWidth; double height = CurveView.ActualHeight - 1; foreach (CurvePoint p in path) { MathHelpers.Point n = p.Normalized; points.Add(new MathHelpers.Point(n.X * width, n.Y * height)); } Sort(points); //make sure we have x points on edges if (points.Count >= 2) { MathHelpers.Point f = points[0]; if (f.X > 0) { points.Insert(0, new MathHelpers.Point(0, f.Y)); } MathHelpers.Point l = points[points.Count - 1]; if (l.X < width) { points.Add(new MathHelpers.Point(width, l.Y)); } } double[] sd = Curves.SecondDerivative(points.ToArray()); for (int i = 0; i < points.Count - 1; ++i) { MathHelpers.Point cur = points[i]; MathHelpers.Point next = points[i + 1]; for (double x = cur.X; x < next.X; ++x) { double t = (double)(x - cur.X) / (next.X - cur.X); double a = 1 - t; double bt = t; double h = next.X - cur.X; double y = a * cur.Y + bt * next.Y + (h * h / 6) * ((a * a * a - a) * sd[i] + (bt * bt * bt - bt) * sd[i + 1]); if (y < 0) { y = 0; } if (y > height - 1) { y = height - 1; } curve.Add(new MathHelpers.Point(x, y)); normalized.Add(new MathHelpers.Point(x / width, y / height)); } } MathHelpers.Point lp = points[points.Count - 1]; curve.Add(lp); normalized.Add(new MathHelpers.Point(lp.X / width, lp.Y / height)); Parallel.For(0, curve.Count - 1, i => { MathHelpers.Point p1 = curve[i]; MathHelpers.Point p2 = curve[i + 1]; buffer.DrawLine((int)p1.X, (int)p1.Y, (int)p2.X, (int)p2.Y, r, g, b, 255); }); return(normalized); }