none
check points from drawline c# RRS feed

  • Question

  • Hi,

    i am drawing some lines and arcs. But now i would like to check if i clicked on the line or arc.
    for example

    for(int i=1;i<10;i++)
    {
    g.drawline(pen,x(i-1),y(i-1)x(i),y(i);
    }
    
    i would like to check if my e.X and e.Y are on this line.
    
    i know that i could use this formula:
    
    y = a * x + b
    
    but i dont know how to calculate it and then make if statements if the y is the correct value.
    Thanks
    Always looking on the bright side of the life!
    Saturday, July 4, 2009 1:42 PM

Answers

  • There is more to it, you would also have to check if the location is beyond to start or end of the line.  You can avoid having to do the math with the GraphicPath class.  Here's a sample program that uses it, it changes the cursor and the line color when the mouse hovers over a line:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Drawing.Drawing2D;

    namespace WindowsFormsApplication1 {
      public partial class Form1 : Form {
        public Form1() {
          this.DoubleBuffered = true;
          InitializeComponent();
        }

        class Entity {
          public Entity(Point lineStart, Point lineEnd) {
            Path = new GraphicsPath();
            Path.AddLine(lineStart, lineEnd);
          }
          public GraphicsPath Path { get; set; }
          public bool Selected { get; set; }
        }

        // Some sample lines
        List<Entity> mEntities = new List<Entity>() {
          new Entity(new Point(0, 0), new Point(100, 100)),
          new Entity(new Point(10, 20), new Point(80, 90)),
          new Entity(new Point(30, 60), new Point(90, 70))
        };

        protected override void OnPaint(PaintEventArgs e) {
          // Draw the lines
          foreach (Entity entity in mEntities)
            e.Graphics.DrawPath(entity.Selected ? Pens.Red : Pens.Black, entity.Path);
          base.OnPaint(e);
        }

        protected override void OnMouseMove(MouseEventArgs e) {
          // Select line if mouse hovers over it
          bool any = false;
          bool redraw = false;
          using (Pen pen = new Pen(Brushes.Black, 3)) {
            foreach (Entity entity in mEntities) {
              bool hit = entity.Path.IsOutlineVisible(e.Location, pen);
              hit = hit && !any;   // Optional: only select first hit
              if (entity.Selected != hit) redraw = true;
              entity.Selected = hit;
              if (hit) any = true;
            }
          }
          if (redraw) this.Invalidate();
          this.Cursor = any ? Cursors.Cross : Cursors.Default;
        }
      }
    }


    Hans Passant.
    • Marked as answer by Jabcek Saturday, July 4, 2009 6:15 PM
    Saturday, July 4, 2009 3:21 PM
    Moderator

All replies

  • There is more to it, you would also have to check if the location is beyond to start or end of the line.  You can avoid having to do the math with the GraphicPath class.  Here's a sample program that uses it, it changes the cursor and the line color when the mouse hovers over a line:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Drawing.Drawing2D;

    namespace WindowsFormsApplication1 {
      public partial class Form1 : Form {
        public Form1() {
          this.DoubleBuffered = true;
          InitializeComponent();
        }

        class Entity {
          public Entity(Point lineStart, Point lineEnd) {
            Path = new GraphicsPath();
            Path.AddLine(lineStart, lineEnd);
          }
          public GraphicsPath Path { get; set; }
          public bool Selected { get; set; }
        }

        // Some sample lines
        List<Entity> mEntities = new List<Entity>() {
          new Entity(new Point(0, 0), new Point(100, 100)),
          new Entity(new Point(10, 20), new Point(80, 90)),
          new Entity(new Point(30, 60), new Point(90, 70))
        };

        protected override void OnPaint(PaintEventArgs e) {
          // Draw the lines
          foreach (Entity entity in mEntities)
            e.Graphics.DrawPath(entity.Selected ? Pens.Red : Pens.Black, entity.Path);
          base.OnPaint(e);
        }

        protected override void OnMouseMove(MouseEventArgs e) {
          // Select line if mouse hovers over it
          bool any = false;
          bool redraw = false;
          using (Pen pen = new Pen(Brushes.Black, 3)) {
            foreach (Entity entity in mEntities) {
              bool hit = entity.Path.IsOutlineVisible(e.Location, pen);
              hit = hit && !any;   // Optional: only select first hit
              if (entity.Selected != hit) redraw = true;
              entity.Selected = hit;
              if (hit) any = true;
            }
          }
          if (redraw) this.Invalidate();
          this.Cursor = any ? Cursors.Cross : Cursors.Default;
        }
      }
    }


    Hans Passant.
    • Marked as answer by Jabcek Saturday, July 4, 2009 6:15 PM
    Saturday, July 4, 2009 3:21 PM
    Moderator
  • Thanks for answering, but can you send me the app to poteko.jure@gmail.com. Because i get some compiling errors that i do not know how to fix.


    Always looking on the bright side of the life!
    Saturday, July 4, 2009 4:45 PM
  • I don't give out my email address.  What are the errors?

    Hans Passant.
    Saturday, July 4, 2009 5:26 PM
    Moderator
  • Ok,

    first there was an error here

    // Some sample lines
        List<Entity> mEntities = new List<Entity>() {
          new Entity(new Point(0, 0), new Point(100, 100)),
          new Entity(new Point(10, 20), new Point(80, 90)),
          new Entity(new Point(30, 60), new Point(90, 70))
        };

    i changed {} to (), and this error was gone.

    after that i get this 5 errors:
    Error    1    'WindowsApplication1.Form1.Dispose(bool)': no suitable method found to override
    Error    2    'WindowsFormsApplication1.Form1.Entity.Path.get' must declare a body because it is not marked abstract or extern   
    Error    3    'WindowsFormsApplication1.Form1.Entity.Path.set' must declare a body because it is not marked abstract or extern   
    Error    4    'WindowsFormsApplication1.Form1.Entity.Selected.get' must declare a body because it is not marked abstract or extern   
    Error    5    'WindowsFormsApplication1.Form1.Entity.Selected.set' must declare a body because it is not marked abstract or extern   

    errors 2-5 i can fix if i use (extern in front)
          public extern GraphicsPath Path { get; set; }
          public extern bool Selected { get; set; }

    but Error no. 1 i do not know how to fix



    Always looking on the bright side of the life!
    Saturday, July 4, 2009 5:41 PM
  • You are using an old version of Visual Studio.  Fix:

       class Entity {
          public Entity(Point lineStart, Point lineEnd) {
            Path = new GraphicsPath();
            Path.AddLine(lineStart, lineEnd);
          }
          public GraphicsPath Path;
          public bool Selected;
      }
    Hans Passant.
    Saturday, July 4, 2009 5:45 PM
    Moderator
  • Thanks, now it works
    Saturday, July 4, 2009 6:09 PM