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 PolygonCentroid
{
    public partial class frm62PolygonCentroid : Form
    {
        public frm62PolygonCentroid()
        {
            InitializeComponent();
        }

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

        // The polygon's centroid.
        private PointF Centroid = new PointF(0, 0);

        // Add a point to the polygon.
        private void frm62PolygonCentroid_MouseClick(object sender, MouseEventArgs e)
        {
            PolygonPoints.Add(e.Location);
            Centroid = FindCentroid(PolygonPoints);
            Refresh();
        }

        // Draw the polygon.
        private void frm62PolygonCentroid_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);

            // Draw the centroid.
            if (PolygonPoints.Count > 2)
                DrawPoint(e.Graphics, Point.Round(Centroid), Brushes.Black, Pens.Black);
        }

        // 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);
        }

        // Find the polygon's centroid.
        private PointF FindCentroid(List<Point> points)
        {
            int numPoints = points.Count;
            if (numPoints < 3) return new PointF(
                float.NegativeInfinity,
                float.NegativeInfinity);

            // Find the centroid.
            float X = 0;
            float Y = 0;
            float secondFactor;
            for (int i = 0; i < numPoints; i++)
            {
                int j = (i + 1) % numPoints;
                secondFactor =
                    points[i].X * points[j].Y -
                    points[j].X * points[i].Y;
                X += (points[i].X + points[j].X) * secondFactor;
                Y += (points[i].Y + points[j].Y) * secondFactor;
            }

            // Divide by -6 times the polygon's area.
            float polygon_area = SignedPolygonArea(points);
            X /= -(6 * polygon_area);
            Y /= -(6 * polygon_area);

            return new PointF(X, Y);
        }

        // 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;
        }


        /// <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.SuspendLayout();
            // 
            // frm62PolygonCentroid
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Name = "frm62PolygonCentroid";
            this.Text = "PolygonCentroid";
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.frm62PolygonCentroid_Paint);
            this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.frm62PolygonCentroid_MouseClick);
            this.ResumeLayout(false);

        }

        #endregion
    }
}