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

        // The circle's coordinates.
        float Cx, Cy, Radius;

        // The points on the line.
        PointF Point1, Point2;
        bool Drawing = false, HavePoints = false;

        // The points of intersection.
        PointF Intersection1, Intersection2;
        int NumIntersections;

        // Redraw on resize.
        private void frm59LineCircleIntersection_Load(object sender, EventArgs e)
        {
            this.ResizeRedraw = true;

            // Get the circle's initial geometry.
            InitializeCircle();
        }

        // Calculate the circle's position.
        private void frm59LineCircleIntersection_Resize(object sender, EventArgs e)
        {
            NumIntersections = 0;
            HavePoints = false;
            InitializeCircle();
        }
        private void InitializeCircle()
        {
            Cx = this.ClientSize.Width / 2;
            Cy = this.ClientSize.Height / 2;
            Radius = (float)(Math.Min(Cx, Cy) * 0.8);
            HavePoints = false;
            this.Invalidate();
        }

        // Get the points.
        private void frm59LineCircleIntersection_MouseDown(object sender, MouseEventArgs e)
        {
            Drawing = true;
            NumIntersections = 0;
            Point1 = new PointF(e.X, e.Y);
            Point2 = new PointF(e.X, e.Y);
            HavePoints = true;
            this.Invalidate();
        }
        private void frm59LineCircleIntersection_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Drawing) return;
            Point2 = new PointF(e.X, e.Y);
            this.Invalidate();
        }
        private void frm59LineCircleIntersection_MouseUp(object sender, MouseEventArgs e)
        {
            Drawing = false;

            // Find and draw the intersections.
            NumIntersections = FindLineCircleIntersections(
                Cx, Cy, Radius, Point1, Point2,
                out Intersection1, out Intersection2);
            this.Invalidate();
        }

        // Draw the circle and the segment if we have it.
        private void frm59LineCircleIntersection_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            // Draw the circle.
            e.Graphics.DrawEllipse(Pens.Blue,
                Cx - Radius, Cy - Radius, Radius * 2, Radius * 2);

            // Draw the intersections (if we have them).
            switch (NumIntersections)
            {
                case 0:
                    break;
                case 1:
                    DrawPoint(e.Graphics, Intersection1, Brushes.HotPink, Pens.Red);
                    break;
                case 2:
                    DrawPoint(e.Graphics, Intersection1, Brushes.HotPink, Pens.Red);
                    DrawPoint(e.Graphics, Intersection2, Brushes.HotPink, Pens.Red);
                    break;
            }

            // Draw the segment.
            if (HavePoints)
            {
                DrawPoint(e.Graphics, Point1, Brushes.White, Pens.Black);
                DrawPoint(e.Graphics, Point2, Brushes.White, Pens.Black);
                e.Graphics.DrawLine(Pens.BlueViolet, Point1, Point2);
            }
        }

        // Find the points of intersection.
        private int FindLineCircleIntersections(
            float cx, float cy, float radius,
            PointF point1, PointF point2,
            out PointF intersection1, out PointF intersection2)
        {
            float dx = point2.X - point1.X;
            float dy = point2.Y - point1.Y;

            float A = dx * dx + dy * dy;
            float B = 2 * (dx * (point1.X - cx) + dy * (point1.Y - cy));
            float C = (point1.X - cx) * (point1.X - cx) + (point1.Y - cy) * (point1.Y - cy) - radius * radius;

            float det = B * B - 4 * A * C;
            if ((A <= 0.0000001) || (det < 0))
            {
                // No real solutions.
                intersection1 = new PointF(float.NaN, float.NaN);
                intersection2 = new PointF(float.NaN, float.NaN);
                return 0;
            }
            else if (det == 0)
            {
                // One solution.
                float t = -B / (2 * A);
                intersection1 = new PointF(point1.X + t * dx, point1.Y + t * dy);
                intersection2 = new PointF(float.NaN, float.NaN);
                return 1;
            }
            else
            {
                // Two solutions.
                float t1 = (float)((-B + Math.Sqrt(det)) / (2 * A));
                intersection1 = new PointF(point1.X + t1 * dx, point1.Y + t1 * dy);
                float t2 = (float)((-B - Math.Sqrt(det)) / (2 * A));
                intersection2 = new PointF(point1.X + t2 * dx, point1.Y + t2 * dy);
                return 2;
            }
        }

        // Draw a point.
        private void DrawPoint(Graphics gr, PointF pt, Brush brush, Pen pen)
        {
            const int RADIUS = 3;
            gr.FillEllipse(brush,
                pt.X - RADIUS, pt.Y - RADIUS,
                2 * RADIUS, 2 * RADIUS);
            gr.DrawEllipse(pen,
                pt.X - RADIUS, pt.Y - RADIUS,
                2 * RADIUS, 2 * RADIUS);
        }


        /// <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();
            // 
            // frm59LineCircleIntersection
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 161);
            this.Name = "frm59LineCircleIntersection";
            this.Text = "LineCircleIntersection";
            this.Load += new System.EventHandler(this.frm59LineCircleIntersection_Load);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.frm59LineCircleIntersection_Paint);
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.frm59LineCircleIntersection_MouseDown);
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.frm59LineCircleIntersection_MouseMove);
            this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.frm59LineCircleIntersection_MouseUp);
            this.Resize += new System.EventHandler(this.frm59LineCircleIntersection_Resize);
            this.ResumeLayout(false);

        }

        #endregion
    }
}