using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
internal static class Program
{
[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new HeartForm());
}
}
internal sealed class HeartForm : Form
{
private readonly List<PointF> points = new();
private double x;
private double y;
private double heading;
public HeartForm()
{
Text = "Heart";
ClientSize = new Size(600, 600);
BackColor = Color.White;
heading = 0;
TurnLeft(140);
Forward(113);
DrawCurve();
TurnLeft(120);
DrawCurve();
Forward(112);
}
private void TurnLeft(double degrees)
{
heading += degrees;
}
private void TurnRight(double degrees)
{
heading -= degrees;
}
private void Forward(double distance)
{
x += Math.Cos(heading * Math.PI / 180) * distance;
y += Math.Sin(heading * Math.PI / 180) * distance;
points.Add(new PointF((float)x, (float)y));
}
private void DrawCurve()
{
for (int i = 0; i < 200; i++)
{
TurnRight(1);
Forward(1);
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (points.Count < 3)
return;
float minX = float.MaxValue;
float maxX = float.MinValue;
float minY = float.MaxValue;
float maxY = float.MinValue;
foreach (PointF point in points)
{
minX = Math.Min(minX, point.X);
maxX = Math.Max(maxX, point.X);
minY = Math.Min(minY, point.Y);
maxY = Math.Max(maxY, point.Y);
}
float width = Math.Max(maxX - minX, 1);
float height = Math.Max(maxY - minY, 1);
float scale = Math.Min((ClientSize.Width - 80f) / width, (ClientSize.Height - 80f) / height);
PointF[] screenPoints = new PointF[points.Count];
for (int i = 0; i < points.Count; i++)
{
screenPoints[i] = new PointF(
(ClientSize.Width - width * scale) / 2f + (points[i].X - minX) * scale,
(ClientSize.Height - height * scale) / 2f + (maxY - points[i].Y) * scale
);
}
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
using GraphicsPath path = new();
path.AddPolygon(screenPoints);
using SolidBrush brush = new(Color.Red);
e.Graphics.FillPath(brush, path);
}
}