using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Drawing.Drawing2D;

namespace PolygonArea
{
    public partial class frm61PolygonArea : Form
    {
        public frm61PolygonArea()
        {
            InitializeComponent();
        }

        // The polygon's points.
        private List<Point> PolygonPoints = new List<Point>();

        // Add a point to the polygon.
        private void frm61PolygonArea_MouseClick(object sender, MouseEventArgs e)
        {
            PolygonPoints.Add(e.Location);
            ShowArea();
            Refresh();
        }

        // Draw the polygon.
        private void frm61PolygonArea_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            // Draw the polygon.
            if (PolygonPoints.Count > 2)
                e.Graphics.DrawPolygon(Pens.Blue, PolygonPoints.ToArray());
            foreach (Point point in PolygonPoints)
                DrawPoint(e.Graphics, point, Brushes.White, Pens.Black);
        }

        // Calculate and display the area.
        private void ShowArea()
        {
            float area = PolygonArea(PolygonPoints);
            areaLabel.Text = area.ToString() + " square units";
        }

        // Return the polygon's area in "square units."
        public float PolygonArea(List<Point> points)
        {
            // Return the absolute value of the signed area.
            return Math.Abs(SignedPolygonArea(points));
        }

        // Return the polygon's area in "square units."
        // Negative = clockwise, positive = counter clockwise.
        private float SignedPolygonArea(List<Point> points)
        {
            // Add the first point to the end.
            int numPoints = points.Count;

            // Get the areas.
            float area = 0;
            for (int i = 0; i < numPoints; i++)
            {
                int j = (i + 1) % numPoints;
                area +=
                    (points[j].X - points[i].X) *
                    (points[j].Y + points[i].Y) / 2;
            }

            // Return the result.
            return area;
        }

        // Draw a filled point.
        private void DrawPoint(Graphics gr, Point point, Brush brush, Pen pen)
        {
            const int radius = 4;

            Rectangle rect = new Rectangle(
                point.X - radius, point.Y - radius,
                2 * radius, 2 * radius);
            gr.FillEllipse(brush, rect);
            gr.DrawEllipse(pen, rect);
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.areaLabel = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // areaLabel
            // 
            this.areaLabel.AutoSize = true;
            this.areaLabel.Location = new System.Drawing.Point(12, 9);
            this.areaLabel.Name = "areaLabel";
            this.areaLabel.Size = new System.Drawing.Size(73, 13);
            this.areaLabel.TabIndex = 0;
            this.areaLabel.Text = "0 square units";
            // 
            // frm61PolygonArea
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Controls.Add(this.areaLabel);
            this.Name = "frm61PolygonArea";
            this.Text = "PolygonArea";
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.frm61PolygonArea_Paint);
            this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.frm61PolygonArea_MouseClick);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label areaLabel;
    }
}