// Clover - Draw An AntiAliased Clover

using System;
using System.Windows.Forms;

using IPlusPlus;
using IPlusPlus.Drawing;

enum Segments { Picture };

class CloverProgram : Form
{
    R2 r2;

    [STAThread]
    public static void Main()
    {
        application.EnableVisualStyles();
        application.Run(new CloverProgram());
    }

    CloverProgram()
    {
        r2 = new R2();
        OnResize(this, null);
        Text = "Draw A Clover";
        Paint += OnPaint;
        Resize += OnResize;
    }

    R2 RetainTheClover(int Width, int Height)
    {
        R2 r2 = new R2();

        Path PathA = new Path();
        Path PathB = new Path();
        Path PathC = new Path();
        Path PathD = new Path();

        PathA.AddEllipse(new IPlusPlus.Drawing.Rectangle(0, Height / 3, Width / 2, Height / 3));
        PathB.AddEllipse(new IPlusPlus.Drawing.Rectangle(Width / 2, Height / 3, Width / 2, Height / 3));
        PathC.AddEllipse(new IPlusPlus.Drawing.Rectangle(Width / 3, 0, Width / 3, Height / 2));
        PathD.AddEllipse(new IPlusPlus.Drawing.Rectangle(Width / 3, Height / 2, Width / 3, Height / 2));

        Region A = new Region(PathA);
        Region B = new Region(PathB);
        Region C = new Region(PathC);
        Region D = new Region(PathD);

        r2.OpenSegment((int)Segments.Picture);

        r2.Clear(Color.LightGray);

        r2.Clip = (A | B) ^ (C ^ D);

        r2.SmoothingMode = SmoothingMode.AntiAlias;

        r2.Transform = new Transform(1, 0, 0, 1, Width / 2, Height / 2);

        double Radius = Math.Sqrt(Width * Width / 2.0 + Height * Height / 2.0);

        for (double Angle = 0; Angle < 2 * Maths.Pi; Angle += Maths.Pi / 180)
            r2.DrawLine(new Point(0, 0),
                        new Point((int)(Radius * Math.Cos(Angle) + 0.5),
                                  (int)(-Radius * Math.Sin(Angle) + 0.5)));

        r2.CloseSegment();

        return r2;
    }


    void OnResize(object source, EventArgs args)
    {
        r2 = RetainTheClover(ClientRectangle.Width, ClientRectangle.Height);
        Invalidate();
    }

    void OnPaint(object source, PaintEventArgs args)
    {
        Graphics graphics = new Graphics(this);
        r2.Draw(graphics, true);
    }

    protected override void OnPaintBackground(PaintEventArgs e) { }
}