Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 670509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:16:19+00:00 2026-05-14T00:16:19+00:00

I have an arrow drawn between two objects on a Winform. What would be

  • 0

I have an arrow drawn between two objects on a Winform.

What would be the simplest way to determine that my mouse is currently hovering over, or near, this line.

I have considered testing whether the mouse point intersects a square defined and extrapolated by the two points, however this would only be feasible if the two points had very similar x or y values.

I am thinking, also, this problem is probably more in the realms of linear algebra rather than simple trigonometry, and whilst I do remember the simpler aspects of matrices, this problem is beyond my knowledge of linear algebra.

On the other hand, if a .NET library can cope with the function, even better.

EDIT
Thanks for the answers, there were a few very good ones all deserving being tagged as answered.

I chose Coincoin’s answer as accepted, as I like that it could be applied to any shape drawn, however ended up implementing Tim Robinson’s equation, as it appeared much more efficient to with a simple equation rather than newing up graphics paths and pens, as in my case I need to do it onMouseMove for 1-n different relationships (obviously there would be some caching and optimisations, but the point still remains)

The main issue with the equation was that it appeared to treat the line as infinite, so I added a bounds test as well.

The code (initial cut, I’ll probably neaten it a bit), for those interested, is below

    if (Math.Sqrt( Math.Pow(_end.X - _start.X, 2) + 
           Math.Pow(_end.Y - _start.Y, 2) ) == 0)
    {
        _isHovering =
            new RectangleF(e.X, e.Y, 1, 1).IntersectsWith(_bounds);
    }
    else
    {
        float threshold = 10.0f;

        float distance = (float)Math.Abs( 
            ( ( (_end.X - _start.X) * (_start.Y - e.Y) ) -
            ( (_start.X - e.X) * (_end.Y - _start.Y) ) ) /
            Math.Sqrt( Math.Pow(_end.X - _start.X, 2) + 
            Math.Pow(_end.Y - _start.Y, 2) ));

        _isHovering = (
            distance <= threshold &&
                new RectangleF(e.X, e.Y, 1, 1).IntersectsWith(_bounds)
            );
    }

and _bounds is defined as:

    _bounds = new Rectangle(
    Math.Min(_start.X, _end.X),
    Math.Min(_start.Y, _end.Y),
    Math.Abs(_start.X - _end.X), Math.Abs(_start.Y - _end.Y));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:16:20+00:00Added an answer on May 14, 2026 at 12:16 am

    If you want to easly make hit tests on arbitrary drawn shapes, you can create a path containing your drawing, then widden the path and make a visibility test using only framework functions.

    For instance, here we create a path with a line:

    GraphicsPath path = new GraphicsPath();
    
    path.AddLine(x1, y1, x2, y2);
    path.CloseFigure();
    

    Then, widen the path and create a region for the hit test:

    path.Widen(new Pen(Color.Black, 3));
    region = new Region(path);
    

    Finally, the hit test:

    region.IsVisible(point);
    

    The advantage of that method is it can easily extend to splines, arrows, arc, pies or pretty much anything drawable with GDI+. The same path can be used in both the HitTest and Draw logic by extracting it.

    Here is the code combining it all:

    public GraphicsPath Path
    {
        get { 
            GraphicsPath path = new GraphicsPath();
            path.AddLine(x1, y1, x2, y2);
            path.CloseFigure();
    
            return path;
        }
    }
    
    bool HitTest(Point point)
    {
        using(Pen new pen = Pen(Color.Black, 3))
        using(GraphicsPaht path = Path)
        {
            path.Widen(pen);
    
            using(Region region = new Region(path))
                return region.IsVisible(point);
        }
    }
    
    
    void Draw(Graphics graphics)
    {
        using(Pen pen = new Pen(Color.Blue, 0))
        using(GraphicsPaht path = Path)
            graphics.DrawPath(pen, path);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 372k
  • Answers 372k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer On the sites I develop, I wrap everything in try… May 14, 2026 at 7:15 pm
  • Editorial Team
    Editorial Team added an answer The following will do what you need: NSTimer *timer =… May 14, 2026 at 7:15 pm
  • Editorial Team
    Editorial Team added an answer The offical java api: http://java.sun.com/javase/6/docs/api/index.html?overview-summary.html And any good Java book… May 14, 2026 at 7:15 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.