I'm creating a program in C# that do not allow intersecting lines. How will I modify my code? Thank you.
I've already attached the whole source code of my program for you to check the other class I've included.But here's the code for the drawing process:

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

namespace WindowsFormsApplication2
{
    public partial class Form3 : Form
    {
        private Boolean _calculateDouglasPeuckerReduction = false;
        private List<WindowsFormsApplication2.PointEnhanced> _amerenPoints = new List<WindowsFormsApplication2.PointEnhanced>();


        public Form3()
        {
            InitializeComponent();
        }


        protected override void OnPaint(PaintEventArgs e)
        {

            List<System.Drawing.Point> drawingPoints = new List<System.Drawing.Point>();
            foreach (WindowsFormsApplication2.PointEnhanced point in _amerenPoints)
            {
                drawingPoints.Add(new System.Drawing.Point(Convert.ToInt32(point.X), Convert.ToInt32(point.Y)));

            }

            if (drawingPoints.Count > 2)
            {
                e.Graphics.DrawLines(new Pen(Brushes.Black, 2), drawingPoints.ToArray());
                lblOriginal2.Text = drawingPoints.Count.ToString();

                if (_calculateDouglasPeuckerReduction)
                {
                    List<WindowsFormsApplication2.PointEnhanced> points = Utility2.DouglasPeuckerReduction(_amerenPoints, Convert.ToDouble(nudTolerance2.Value));
                    drawingPoints = new List<System.Drawing.Point>();
                    foreach (WindowsFormsApplication2.PointEnhanced point in points)
                    {
                        drawingPoints.Add(new System.Drawing.Point(Convert.ToInt32(point.X), Convert.ToInt32(point.Y)));
                    }

                    e.Graphics.DrawLines(new Pen(Brushes.Red, 2), drawingPoints.ToArray());
                    lblSimplified2.Text = drawingPoints.Count.ToString();
                }
            }


            base.OnPaint(e);
        }
        private void Form3_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _amerenPoints.Add(new WindowsFormsApplication2.PointEnhanced(e.X, e.Y));
                this.Invalidate();
            }
        }

        private void Form3_MouseUp(object sender, MouseEventArgs e)
        {
            //DO the calculation
            _calculateDouglasPeuckerReduction = true;
            this.Invalidate();
        }

        private void Form3_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _amerenPoints.Clear();
                _calculateDouglasPeuckerReduction = false;
            }
        }

        private void nudTolerance2_ValueChanged(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        private void Form3_Load(object sender, EventArgs e)
        {

        }

    }
}

You can always check the current position of the lines, and do the math of the lengths to come up with an algorithm. However if it's just one line drawing towards another and you don't want them to intersect then I'd try using a loop and a multi-dimension array that stores the occupied points at which the lines are present.

int[,] X_Y = new int[9999, 9999];

for (int i = 0; i < 9999; i++)
{
// store the points as it draws them.
X_Y[pointX, pointY] = // currently present point in the loop.
}

Something of that nature, I don't really deal too much with painting or graphics but I'd use that with XNA Framework on XBOX 360 so it should work. It's a ghetto way of doing it because it's a big code. I remember mine was like 32 lines long in the loop. But it can be done in less. I don't have my source code for that anymore or I'd share it. Sorry that I can't be of more assistance.
}

Edited 5 Years Ago by lxXTaCoXxl: n/a

This article has been dead for over six months. Start a new discussion instead.