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

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

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

            if (PolygonPoints.Count > 2)
            {
                if (IsConvex(PolygonPoints))
                    statusLabel.Text = "Convex";
                else
                    statusLabel.Text = "Not convex";
            }

            Refresh();
        }

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

        // Return true if the polygon is convex.
        private bool IsConvex(List<Point> points)
        {
            bool gotNegative = false;
            bool gotPositive = false;
            int numPoints = points.Count;
            for (int A = 0; A < numPoints; A++)
            {
                int B = (A + 1) % numPoints;
                int C = (B + 1) % numPoints;

                float crossProduct =
                    CrossProductLength(
                        points[A].X, points[A].Y,
                        points[B].X, points[B].Y,
                        points[C].X, points[C].Y);
                if (crossProduct < 0) gotNegative = true;
                else if (crossProduct > 0) gotPositive = true;

                if (gotNegative && gotPositive) return false;
            }

            // If we got this far, the polygon is convex.
            return true;
        }

        // Return the cross product AB x BC.
        public static float CrossProductLength(float Ax, float Ay,
            float Bx, float By, float Cx, float Cy)
        {
            // Get the vectors' coordinates.
            float BAx = Ax - Bx;
            float BAy = Ay - By;
            float BCx = Cx - Bx;
            float BCy = Cy - By;

            // Calculate the Z coordinate of the cross product.
            return (BAx * BCy - BAy * BCx);
        }


        /// <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.statusLabel = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // statusLabel
            // 
            this.statusLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.statusLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.statusLabel.Location = new System.Drawing.Point(12, 9);
            this.statusLabel.Name = "statusLabel";
            this.statusLabel.Size = new System.Drawing.Size(260, 21);
            this.statusLabel.TabIndex = 0;
            this.statusLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // frm63IsPolygonConvex
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 161);
            this.Controls.Add(this.statusLabel);
            this.Name = "frm63IsPolygonConvex";
            this.Text = "IsPolygonConvex";
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.frm63IsPolygonConvex_Paint);
            this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.frm63IsPolygonConvex_MouseClick);
            this.ResumeLayout(false);

        }

        #endregion

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