Konstantin's BLog
Polygon Intersection Algorithms
home top contents previous up next

´╗┐using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using PG;

namespace PG {
    public partial class Form1 : Form {

        public Form1() {
            InitializeComponent();
            //Works: color[2] = 0xFF | 0x99 | 0x99;
            pgn.Add(Poly.Poly1); col[0] = Color.FromArgb(0xFF, 0xFF, 0x99, 0x99);
            pgn.Add(Poly.Poly2); col[1] = Color.FromArgb(0xFF, 0x99, 0xFF, 0x99);
            pgn.Add(Poly.Poly3); col[2] = Color.FromArgb(0xFF, 0x99, 0x99, 0xFF);
            pgn.Add(Poly.Poly4); col[3] = Color.FromArgb(0xFF, 0x00, 0xFF, 0xFF);
            pgn.Add(Poly.Poly5); col[4] = Color.FromArgb(0xFF, 0x99, 0xFF, 0x00);

            bool b;
            bool b2;
            bool b3;

            b = Poly.TwoConvexPolygonsDoIntersectI(Poly.Poly1, Poly.Poly2);
            b2 = Poly.TwoConvexPolygonsDoIntersectII(Poly.Poly1, Poly.Poly2);
            b3 = Poly.TwoConvexPolygonsDoIntersectIII(Poly.Poly1, Poly.Poly2);
            con("(Yes.) Do Polygons 1,2 intersect? " + b + "," + b2 + "," + b3);
            
            b = PG.Poly.TwoConvexPolygonsDoIntersectI(Poly.Poly3, Poly.Poly2);
            b2 = PG.Poly.TwoConvexPolygonsDoIntersectII(Poly.Poly3, Poly.Poly2);
            b3 = PG.Poly.TwoConvexPolygonsDoIntersectIII(Poly.Poly3, Poly.Poly2);
            con("(No.) Do Polygons 2,3 intersect? " + b + "," + b2 + "," + b3);
            
            
            b = PG.Poly.TwoConvexPolygonsDoIntersectI(Poly.Poly1, Poly.Poly3);
            b2 = PG.Poly.TwoConvexPolygonsDoIntersectII(Poly.Poly1, Poly.Poly3);
            b3 = PG.Poly.TwoConvexPolygonsDoIntersectIII(Poly.Poly1, Poly.Poly3);
            con("(Yes.) Do Polygons 1,3 intersect? " + b + "," + b2 + "," + b3);

            b = PG.Poly.TwoConvexPolygonsDoIntersectI(Poly.Poly1, Poly.Poly4);
            b2 = PG.Poly.TwoConvexPolygonsDoIntersectII(Poly.Poly1, Poly.Poly4);
            b3 = PG.Poly.TwoConvexPolygonsDoIntersectIII(Poly.Poly1, Poly.Poly4);
            con("(No.) Do Polygons 1,4 intersect? " + b + "," + b2 + "," + b3);


            b = PG.Poly.TwoConvexPolygonsDoIntersectI(Poly.Poly1, Poly.Poly5);
            b2 = PG.Poly.TwoConvexPolygonsDoIntersectII(Poly.Poly1, Poly.Poly5);
            b3 = PG.Poly.TwoConvexPolygonsDoIntersectIII(Poly.Poly1, Poly.Poly5);
            con("(Yes. Interwoven.). Do Polygons 1,5 intersect? " + b + "," + b2 + "," + b3);
            

        }

        public void con(string s) {
            textBox1.Text = textBox1.Text + s + "\r\n";
        }

        private void Form1_Load(object sender, EventArgs e) {
            //=========================================================================
            //To draw right on form:
            this.BackColor = Color.Black;
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
            //=========================================================================
        }



        #region drawpictures

        /// <summary>
        /// Auxilairy array to continuously draw pictures on form.
        /// Stores polygons to be tested.
        /// </summary>
        static List<int[,]> pgn = new List<int[,]>();

        //static List<int[]> color = new List<int[]>(6);
        Color[] col = new Color[5];
        static int shiftX = 100;
        static int shiftY = 60;
        static int scale = 10;


        /// <summary>
        /// Continuously draws polygons when paint event happens.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {

            //Draw coordinate axes:
            Pen pn = new Pen(Color.White, 1);
            e.Graphics.DrawLine(pn, new Point(0, shiftY), new Point(shiftX * 3, shiftY));
            e.Graphics.DrawLine(pn, new Point(shiftX, shiftY * 2), new Point(shiftX, 0));

            //Draw polygons with their own color:
            //Each time po
            for ( int i = 0; i < pgn.Count; i++) {
                pn = new Pen(col[i], 2);
                e.Graphics.DrawPath(pn, convertPolygonArrayToGraphicsPath(pgn[i]));
                Font ft = new Font("Courier New", 11, FontStyle.Bold);
                //Brush br = new SolidBrush(col[i]);
                SolidBrush br = new SolidBrush(col[i]);
                int wi = i % 3;
                Point point = new Point(pgn[i][wi, 0] * scale + shiftX, -pgn[i][wi, 1] * scale + shiftY);
                e.Graphics.DrawString(" " + (i+1), ft, br, point);
            }

            pn.Dispose();

		}

        /// <summary>
        /// Converts polygons from integer array form to GraphicsPath form.
        /// </summary>
        /// <param name="p">Polygons as they defined in this package.</param>
        /// <returns>Polygon converted to GraphicsPath which can be drawn on form.</returns>
        private static GraphicsPath convertPolygonArrayToGraphicsPath(int[,] p) {
            int nP = p.GetUpperBound(0) + 1;
            Point[] ar = new Point[nP];
            for (int i = 0; i < nP; i++) {
                ar[i] = new Point(p[i, 0] * scale + shiftX, -p[i, 1] * scale + shiftY);
            }
            GraphicsPath gp = new GraphicsPath();
            gp.AddPolygon(ar);
            return gp;
        }

        #endregion  //drawpictures

    }
}


Copyright (C) 2008 Landkey Computers