C#, WPF
Rough approximation with a hand-crafted Bézier curve. To make it more interesting: No use of Line
, Path
or similar, only Ellipse
.

public MainWindow()
{
InitializeComponent();
// Requires a Canvas named Canvas in XAML file
DrawBellCurve(Canvas, 300, 300);
}
public static void DrawBellCurve(Canvas canvas, int width, int height)
{
Point p1 = new Point(0, height);
Point p2 = new Point(0.3 * width, 1 * height);
Point p3 = new Point(0.4 * width, 0.1 * height);
Point peak = new Point(width / 2, 0);
Point p3r = new Point(width - p3.X, p3.Y);
Point p2r = new Point(width - p2.X, p2.Y);
Point p1r = new Point(width, height);
List<Point> controlPoints = new List<Point> { p1, p2, p3, peak, p3r, p2r, p1r };
DrawBezierCurve(canvas, controlPoints, Color.FromRgb(50, 200, 25));
DrawPoints(canvas, controlPoints, 5, Color.FromRgb(80, 80, 220));
}
public static void DrawPoints(Canvas canvas, List<Point> points, double radius, Color color)
{
foreach (var p in points)
{
Ellipse ellipse = new Ellipse();
ellipse.Width = radius;
ellipse.Height = radius;
ellipse.SetValue(Canvas.LeftProperty, p.X - radius / 2);
ellipse.SetValue(Canvas.TopProperty, p.Y - radius / 2);
ellipse.Fill = new SolidColorBrush(color);
canvas.Children.Add(ellipse);
}
}
public static void DrawBezierCurve(Canvas canvas, List<Point> controlPoints, Color color)
{
var curvePoints = new List<Point>();
double tStep = 0.01;
for (double t = 0; t <= 1; t += tStep)
{
curvePoints.Add(GetBezierPoint(controlPoints, t));
}
DrawPoints(canvas, curvePoints, 3, color);
}
public static Point GetBezierPoint(List<Point> controlPoints, double t)
{
// De Casteljau's algorithm
if (controlPoints.Count == 1)
{
return controlPoints[0];
}
var newControlPoints = new List<Point>();
for (int i = 0; i < controlPoints.Count - 1; i++)
{
Point p1 = controlPoints[i];
Point p2 = controlPoints[i + 1];
Point p = new Point(
t * p1.X + (1 - t) * p2.X,
t * p1.Y + (1 - t) * p2.Y);
newControlPoints.Add(p);
}
return GetBezierPoint(newControlPoints, t);
}
getInternetBellCurve+0
? What does "hard-coding" imply? This rule is currently highly opinion based. \$\endgroup\$