private Point Bezier3(Point p0, Point p1, Point p2, Point p3, double t, double z) { List <Point> mt = new List <Point> { p0, p1, p2, p3 }; List <Point> res = new List <Point>(); Point r = new Point(); for (int i = 0; i < matrix.Count; i++) { double x = 0; double y = 0; for (int j = 0; j < matrix[i].Count; j++) { x += mt[j].X * matrix[j][i]; y += mt[j].Y * matrix[j][i]; } res.Add(new Point { X = x, Y = y, Z = z }); } for (int i = 0; i < res.Count; i++) { r.X += res[i].X * Math.Pow(t, i); r.Y += res[i].Y * Math.Pow(t, i); } return(r); }
public SunControl(Control parent) { sunXY = new PictureBox(); var sunZ = new TrackBar { Orientation = Orientation.Vertical, Minimum = -100, Maximum = 100, Value = 0, }; parent.Controls.Add(sunXY); parent.Controls.Add(sunZ); parent.Resize += (sender, args) => { var width = Math.Min(parent.ClientSize.Width, parent.ClientSize.Height) - parent.Margin.Vertical - sunZ.Width - 8; if (parent.Width <= 0) { return; } sunXY.Size = new Size(width, width); sunZ.Height = width; Redraw(); }; sunXY.MouseMove += (sender, args) => { if ((args.Button & MouseButtons.Left) == 0) { return; } var delta = new Point { X = args.X - sunXY.Width / 2, Y = args.Y - sunXY.Height / 2 }; var r = (float)delta.Length(); Y = -(float)delta.X / r; X = -(float)delta.Y / r; Redraw(); NewPosition?.Invoke(new Point { X = X * xy, Y = Y * xy, Z = Z }); }; sunZ.ValueChanged += (sender, args) => { Z = sunZ.Value / 100F; Redraw(); NewPosition?.Invoke(new Point { X = X * xy, Y = Y * xy, Z = Z }); }; }
void GenerateBezier() { List <Spline> spline = new List <Spline>(); foreach (var select in context.world.selected) { Spline s = select as Spline; if (s != null && s.points.Count > 3) { var copypoints = new LinkedList <Point>(); int count = (s.points.Count + 1) / 2 - 1; bool flag = s.points.Count % 2 != 0; foreach (var point in s.points) { copypoints.AddLast(point); } LinkedListNode <Point> p0 = copypoints.First; spline.Add(new Spline()); while (count != 0) { LinkedListNode <Point> p1, p2, p3; p1 = p0.Next; p2 = p1.Next; if (flag && count == 1) { p3 = p2; } else if (!flag && count == 1) { p2 = p2.Next; p3 = p2; } else if (!flag && count == 1) { p2 = p1; p3 = p2.Next; count++; flag = true; } else { p3 = p2.Next; } var p = new Point { X = (p2.Value.X + p3.Value.X) / 2, Y = (p2.Value.Y + p3.Value.Y) / 2, Z = (p2.Value.Z + p3.Value.Z) / 2 }; //p' Point q0 = p0.Value; for (float t = (float)0.12; t <= 1.0; t += (float)0.02) { var q1 = Bezier3(p0.Value, p1.Value, p2.Value, p, t, p.Z); spline.Last().Add(q0); q0 = q1; } spline.Last().Add(q0); p0 = copypoints.AddAfter(p2, p); //p' count--; } } } context.world.selected.Clear(); foreach (var s in spline) { context.world.entities.Add(s); context.world.selected.Add(s); } context.Redraw(); }