9 Replies - 10187 Views - Last Post: 01 January 2012 - 11:27 AM Rate Topic: -----

#1 slowly_but_surely   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-December 11

How to detect intersecting lines in C#?

Posted 01 January 2012 - 08:48 AM

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)
        {

        }

    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: How to detect intersecting lines in C#?

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How to detect intersecting lines in C#?

Posted 01 January 2012 - 08:56 AM

So you're really asking "Figure out my logic and architect my software for me" as well as "review all my other classes and fix them for me" - Does that about sum up the post?

Tell ya what... I'll walk through this with you and help you figure out for yourself, what it is you need to do, rather than just give you code. We don't just give people code and solutions. We try to help them learn.


Let's talk about your program a bit. You only told us it was not supposed to allow intersecting lines. So where do the lines come from? I see some mouse code. Is the user drawing one line by selecting point a then selecting point b? Or are they dragging out a line?

Each time you create a line: Where are you storing that within your application?

This post has been edited by tlhIn`toq: 01 January 2012 - 08:56 AM

Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How to detect intersecting lines in C#?

Posted 01 January 2012 - 09:02 AM

Update:

After reading your code I do see a collection of something you are calling a .PointEnhanced. I'm not sure what that is supposed to be. A point really only needs an X and Y to be a point. That's worked for mathematicians for centuries. So what is this PointEnhanced class?

I also see you have a collection for them: _amerenPoints
Which you then convert to a collection of standard points.

But your biggest problem is that you are relying completely on a .NET function to do all your work for you:
36 e.Graphics.DrawLines(new Pen(Brushes.Black, 2), drawingPoints.ToArray());

One line... One call to draw your entire collection of lines.

Obviously that isn't going to work. You're going to have to loop through your collection programmatically and check if the new line you are about to draw intersects with any other line you have already drawn. Take a look at some geometry textbooks or websites for that.

Suggestion:
Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

// Try #1 - May 1, 0900hrs
//    code
//    code  
//    code
// Try #2 - May 2, 1700hrs   Okay, plan B.  What if I do it *this* way
//    code
//    code  
//    code
// Try #14 - May 3, 0500hrs after 5 cans of RedBull.  Maybe I should get some sleep. I can't think of anything else but this last idea
    code
    code  
    code


If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

What does this error mean?

I don't want you to write my code, just give me ideas on how to solve my problem.

This post has been edited by tlhIn`toq: 01 January 2012 - 09:12 AM

Was This Post Helpful? 0
  • +
  • -

#4 slowly_but_surely   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-December 11

Re: How to detect intersecting lines in C#?

Posted 01 January 2012 - 09:15 AM

Thank you for replying to my post.

First, the PointEnhanced is another class, I've attached the whole project so you can check it. The computations is included at the Utility2 class.

I haven't tried doing the calculations for the intersecting lines because I don't know how to start based from the code I've finished. That is why I've posted a question about it and I've shown my code is because I don't know and I have no idea how to do it. Anyways, My code is working, I can actually draw lines but but I want to do now is to enhanced it by DETECTING INTERSECTING LINES and disregard them.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How to detect intersecting lines in C#?

Posted 01 January 2012 - 09:21 AM

Please take a try at coding a solution now that you have been given a direction to research. Once you have updated your code if you are still having issues please post in this thread rather than starting a new one.

A word to our newer members that may be anxious to help:
Please help this person by NOT giving them the code to do it. So much more is learned by trial and error than by someone just handing you the answer. The OP needs to put in the effort and do some trial and error just like you did in the begging if they are to grow as a developer.

View Postslowly_but_surely, on 01 January 2012 - 10:15 AM, said:

Thank you for replying to my post.

First, the PointEnhanced is another class, I've attached the whole project so you can check it. The computations is included at the Utility2 class.


There is no attachment of the entire project. If there were, most people won't download and install an entire project from an unknown source. There really is no reason for it in 99% of the questions. We really only need the relevant code relating to the problem pasted into the post just has you have done here.


View Postslowly_but_surely, on 01 January 2012 - 10:15 AM, said:

I haven't tried doing the calculations for the intersecting lines because I don't know how to start based from the code I've finished. That is why I've posted a question about it and I've shown my code is because I don't know and I have no idea how to do it. Anyways, My code is working, I can actually draw lines but but I want to do now is to enhanced it by DETECTING INTERSECTING LINES and disregard them.


I have given you a direction to start and pointed out the line of code that is the basis of what you need to replace. As well as given you a link to a mathematics site so you can see how to determine if lines intersect. Its up to you now to take that information and make a good faith effort to actually code a solution.

This post has been edited by tlhIn`toq: 01 January 2012 - 09:22 AM

Was This Post Helpful? 0
  • +
  • -

#6 RexGrammer   User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

Re: How to detect intersecting lines in C#?

Posted 01 January 2012 - 09:36 AM

View PosttlhIn`toq, on 01 January 2012 - 04:02 PM, said:

But your biggest problem is that you are relying completely on a .NET function to do all your work for you:
36 e.Graphics.DrawLines(new Pen(Brushes.Black, 2), drawingPoints.ToArray());

One line... One call to draw your entire collection of lines.

Obviously that isn't going to work. You're going to have to loop through your collection programmatically and check if the new line you are about to draw intersects with any other line you have already drawn. Take a look at some geometry textbooks or websites for that.


I'm sorry for intruding, but may I ask: why Isn't this going to work? Since it is a method that accepts a pen and a array of points, so from what I guess the method iterates through all of the points and draws the lines, so I don't get it why isn't this going to work? (Maybe I'm just plain stupid... :P) :D

Graphics.DrawLines Method (Pen, Point()) (MSDN Article)

This post has been edited by RexGrammer: 01 January 2012 - 09:38 AM

Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How to detect intersecting lines in C#?

Posted 01 January 2012 - 09:49 AM

It isn't going to work for what you want to do.

This line simply plots all the lines that you have given it points for. There is no way to tell this one line to not plot intersecting lines.

At the very least you are going to have to go through your entire collection of points, calculate the lines, determine if they intersect, if they do then you need to decide which one to NOT draw. Then whatever lines remain can be fed to this code. In other words you need to filter all of your points data before giving it over to this code.
Was This Post Helpful? 1
  • +
  • -

#8 RexGrammer   User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

Re: How to detect intersecting lines in C#?

Posted 01 January 2012 - 10:01 AM

Oh, my bad...

I get it: It's OK, but not in this particular case (where you have to check if the lines intersect and determine to draw them or not)

Thanks! :D
Was This Post Helpful? 0
  • +
  • -

#9 slowly_but_surely   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-December 11

Re: How to detect intersecting lines in C#?

Posted 01 January 2012 - 11:05 AM

Im so sorry for having an unclear question. Anyways, i have here the sample computation for Perpendicular Distance i used in this program:

public static Double PerpendicularDistance(Point Point1, Point Point2, Point Point)
    {
        Double area = Math.Abs(.5 * (Point1.X * Point2.Y + Point2.X * Point.Y + Point.X * Point1.Y - Point2.X * Point1.Y - Point.X * Point2.Y - Point1.X * Point.Y));
        Double bottom = Math.Sqrt(Math.Pow(Point1.X - Point2.X, 2) + Math.Pow(Point1.Y - Point2.Y, 2));
        Double height = area / bottom * 2;

        return height;
    }



this already works, but my problem is how will I wan to get the intersection of a ONE Line drawn.. :(
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How to detect intersecting lines in C#?

Posted 01 January 2012 - 11:27 AM

To be honest I don't see how your method relates to whether or not two lines intersect.
Your function sure doesn't like like an attempt to code the geometry that I linked to you.
http://zonalandeduca...fTwoLines1.html

Perhaps this will help put you on the right track.
http://stackoverflow...ments-intersect
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1