I have an arrow drawn between two objects on Winform.
What would be the easiest way to determine if my mouse is currently freezing or approaching this line.
I examined the question of whether a mouse point intersects a square defined and extrapolated by two points, however this would be possible only if the two points had very similar x or y values.
I also think that this problem is probably more in the fields of linear algebra, rather than simple trigonometry, and although I remember simpler aspects of matrices, this problem does not depend on linear algebra.
On the other hand, if the .NET library can handle this function, even better.
EDIT Thanks for the answers, there were a few very good ones that deserve to be marked as an answer.
I chose Coincoin's answer as accepted, because I like that it can be applied to any figure, but ultimately embodied Tim Robinson's decision, since it turned out to be much more effective with a simple equation, and not with the appearance of graphic tracks and pens, since in my case I need to do this onMouseMove for 1 different relationship (obviously, there will be caching and optimization, but the point still remains)
The main problem with this equation was that he considered the line to be infinite, so I also added a border test.
Code (initial cut, Iβm probably a little ahead), for those who are interested, below
if (Math.Sqrt( Math.Pow(_end.X - _start.X, 2) + Math.Pow(_end.Y - _start.Y, 2) ) == 0) { _isHovering = new RectangleF(eX, eY, 1, 1).IntersectsWith(_bounds); } else { float threshold = 10.0f; float distance = (float)Math.Abs( ( ( (_end.X - _start.X) * (_start.Y - eY) ) - ( (_start.X - eX) * (_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(eX, eY, 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));