473,563 Members | 2,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hit Detection on a Line

I want to know if the mouse is over (hitting) a line. Therefore I created a
Region object that holds the line and use the IsVisible() method to test if
the mouse hits the line. It doesn't work! In the little program below I
create two Regions: one is square, the other is a line. Hit testing works on
the square-Region, not on the line-Region.

How can I do a hit-test over a line?

//namespace HitTest
namespace HitTest
{
//class Form
class Form:System.Win dows.Forms.Form
{

//two points that make up a line
System.Drawing. Point point1=new System.Drawing. Point(0,0);
System.Drawing. Point point2=new System.Drawing. Point(50,50);


//a rectangle
System.Drawing. Rectangle rectangle=new
System.Drawing. Rectangle(50,50 ,100,100);


//a line region
System.Drawing. Region line;


//a square region
System.Drawing. Region square;


//a brush
System.Drawing. Brush brush=System.Dr awing.Brushes.W hite;


//constructor
Form()
{
//set style for double buffered graphics
SetStyle
(
System.Windows. Forms.ControlSt yles.AllPaintin gInWmPaint|
System.Windows. Forms.ControlSt yles.DoubleBuff er|
System.Windows. Forms.ControlSt yles.ResizeRedr aw|
System.Windows. Forms.ControlSt yles.UserPaint,
true
);
//get a GraphicsPath object
System.Drawing. Drawing2D.Graph icsPath graphicspath=ne w
System.Drawing. Drawing2D.Graph icsPath();
//add a line to the graphicspath
graphicspath.Ad dLine(point1,po int2);
//use the graphicspath to define the line-region
line=new System.Drawing. Region(graphics path);
//use the rectangle to define the square-region
square=new System.Drawing. Region(rectangl e);
//prepare for mouse-input
MouseMove+=new System.Windows. Forms.MouseEven tHandler(OnMous eMove);
//prepare for paint
Paint+=new System.Windows. Forms.PaintEven tHandler(OnPain t);

}


//OnMouseMove
void OnMouseMove(obj ect a,System.Window s.Forms.MouseEv entArgs b)
{

//use White brush
brush=System.Dr awing.Brushes.W hite;

if(square.IsVis ible(b.X,b.Y))
{

//use Red brush if mouse is over square
brush=System.Dr awing.Brushes.R ed;
}

else if(line.IsVisib le(b.X,b.Y))
{

//use Blue brush if mouse is over line
brush=System.Dr awing.Brushes.B lue;
}

//force redraw
Invalidate();
}


//OnPaint
void OnPaint(object a,System.Window s.Forms.PaintEv entArgs b)
{
b.Graphics.Draw Line(System.Dra wing.Pens.Black ,point1,point2) ;
b.Graphics.Draw Rectangle(Syste m.Drawing.Pens. Black,rectangle );
b.Graphics.Fill Region(brush,sq uare);
}


//Main
public static void Main()
{
System.Windows. Forms.Applicati on.Run(new Form());
}

}
}


Apr 16 '06 #1
2 10398
Martijn,

This could be because the region is actually empty it has no area. I'd
suggest using GrpahicsPaths. They support IsVisible and IsOutlineVisibl e
methods. The latter is what you need to use for hittesting lines.
--
HTH
Stoitcho Goutsev (100)

"Martijn Mulder" <i@m> wrote in message
news:44******** *************** @news.wanadoo.n l...
I want to know if the mouse is over (hitting) a line. Therefore I created
a Region object that holds the line and use the IsVisible() method to test
if the mouse hits the line. It doesn't work! In the little program below I
create two Regions: one is square, the other is a line. Hit testing works
on the square-Region, not on the line-Region.

How can I do a hit-test over a line?

//namespace HitTest
namespace HitTest
{
//class Form
class Form:System.Win dows.Forms.Form
{

//two points that make up a line
System.Drawing. Point point1=new System.Drawing. Point(0,0);
System.Drawing. Point point2=new System.Drawing. Point(50,50);


//a rectangle
System.Drawing. Rectangle rectangle=new
System.Drawing. Rectangle(50,50 ,100,100);


//a line region
System.Drawing. Region line;


//a square region
System.Drawing. Region square;


//a brush
System.Drawing. Brush brush=System.Dr awing.Brushes.W hite;


//constructor
Form()
{
//set style for double buffered graphics
SetStyle
(
System.Windows. Forms.ControlSt yles.AllPaintin gInWmPaint|
System.Windows. Forms.ControlSt yles.DoubleBuff er|
System.Windows. Forms.ControlSt yles.ResizeRedr aw|
System.Windows. Forms.ControlSt yles.UserPaint,
true
);
//get a GraphicsPath object
System.Drawing. Drawing2D.Graph icsPath graphicspath=ne w
System.Drawing. Drawing2D.Graph icsPath();
//add a line to the graphicspath
graphicspath.Ad dLine(point1,po int2);
//use the graphicspath to define the line-region
line=new System.Drawing. Region(graphics path);
//use the rectangle to define the square-region
square=new System.Drawing. Region(rectangl e);
//prepare for mouse-input
MouseMove+=new System.Windows. Forms.MouseEven tHandler(OnMous eMove);
//prepare for paint
Paint+=new System.Windows. Forms.PaintEven tHandler(OnPain t);

}


//OnMouseMove
void OnMouseMove(obj ect a,System.Window s.Forms.MouseEv entArgs b)
{

//use White brush
brush=System.Dr awing.Brushes.W hite;

if(square.IsVis ible(b.X,b.Y))
{

//use Red brush if mouse is over square
brush=System.Dr awing.Brushes.R ed;
}

else if(line.IsVisib le(b.X,b.Y))
{

//use Blue brush if mouse is over line
brush=System.Dr awing.Brushes.B lue;
}

//force redraw
Invalidate();
}


//OnPaint
void OnPaint(object a,System.Window s.Forms.PaintEv entArgs b)
{
b.Graphics.Draw Line(System.Dra wing.Pens.Black ,point1,point2) ;
b.Graphics.Draw Rectangle(Syste m.Drawing.Pens. Black,rectangle );
b.Graphics.Fill Region(brush,sq uare);
}


//Main
public static void Main()
{
System.Windows. Forms.Applicati on.Run(new Form());
}

}
}

Apr 17 '06 #2
This could be because the region is actually empty it has no area. I'd
suggest using GrpahicsPaths. They support IsVisible and IsOutlineVisibl e
methods. The latter is what you need to use for hittesting lines.

Thank you so much Stoitcho,
Here is the improved program that does hit testing on a line :-)


//namespace HitTest
namespace HitTest
{
//class Form
class Form:System.Win dows.Forms.Form
{

//two points that make up a line
System.Drawing. Point point1=new System.Drawing. Point(0,0);
System.Drawing. Point point2=new System.Drawing. Point(50,50);


//a rectangle
System.Drawing. Rectangle rectangle=new
System.Drawing. Rectangle(50,50 ,100,100);


//a square region
System.Drawing. Region square;

//a graphicspath
System.Drawing. Drawing2D.Graph icsPath graphicspath=ne w
System.Drawing. Drawing2D.Graph icsPath();


//a brush
System.Drawing. Brush brush=System.Dr awing.Brushes.W hite;


//constructor
Form()
{
//set style for double buffered graphics
SetStyle
(
System.Windows. Forms.ControlSt yles.AllPaintin gInWmPaint|
System.Windows. Forms.ControlSt yles.DoubleBuff er|
System.Windows. Forms.ControlSt yles.ResizeRedr aw|
System.Windows. Forms.ControlSt yles.UserPaint,
true
);
//add a line to the graphicspath
graphicspath.Ad dLine(point1,po int2);

//use the rectangle to define the square-region
square=new System.Drawing. Region(rectangl e);
//prepare for mouse-input
MouseMove+=new System.Windows. Forms.MouseEven tHandler(OnMous eMove);
//prepare for paint
Paint+=new System.Windows. Forms.PaintEven tHandler(OnPain t);

}


//OnMouseMove
void OnMouseMove(obj ect a,System.Window s.Forms.MouseEv entArgs b)
{

//use White brush
brush=System.Dr awing.Brushes.W hite;

if(square.IsVis ible(b.X,b.Y))
{

//use Red brush if mouse is over square
brush=System.Dr awing.Brushes.R ed;
}

else if(graphicspath .IsOutlineVisib le(b.X,b.Y,Syst em.Drawing.Pens .Black))
{

//use Blue brush if mouse is over line
brush=System.Dr awing.Brushes.B lue;
}

//force redraw
Invalidate();
}

//OnPaint
void OnPaint(object a,System.Window s.Forms.PaintEv entArgs b)
{
b.Graphics.Draw Line(System.Dra wing.Pens.Black ,point1,point2) ;
b.Graphics.Draw Rectangle(Syste m.Drawing.Pens. Black,rectangle );
b.Graphics.Fill Rectangle(brush ,rectangle);
}

//Main
public static void Main()
{
System.Windows. Forms.Applicati on.Run(new Form());
}
}
}


Apr 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
4873
by: yawnmoth | last post by:
say i have a for loop that would iterate through every character and put a space between every 80th one, in effect forcing word wrap to occur. this can be implemented easily using a regular expression. if i wanted to improve on this, and make it so stuff in url's didn't count towards that 80 character limit, a regular expression would not...
3
3165
by: Thorsten Reichelt | last post by:
Hi, I'm involved in a research project on spatial prepositions. In that project we use very simple, static 3D maps that are represented in a tiny subset of x3d enriched with some few linguistic information. Until now, we home-brewed all 3D-related calculations for two reasons: First, we thought we'd get away with pretty basic and simple...
60
7222
by: Fotios | last post by:
Hi guys, I have put together a flexible client-side user agent detector (written in js). I thought that some of you may find it useful. Code is here: http://fotios.cc/software/ua_detect.htm The detector requires javascript 1.0 to work. This translates to netscape 2.0 and IE 3.0 (although maybe IE 2.0 also works with it)
6
4750
by: Gustav Medler | last post by:
Hello, there is a known problem with Opera and the execution of content shown in <NOSCRIPT> tag. Everythings works fine, if there is only one simple script like: http://www.dr-wald.de/test/Opera-NOSCRIPT.html Switching off Javascript will show the alternative content. Javascript enabled will only show the JS-content, _not_ the <NOSCRIPT>...
8
4538
by: R. Smits | last post by:
I've have got this script, the only thing I want to be changed is the first part. It has to detect IE version 6 instead of just "Microsoft Internet Explorer". Can somebody help me out? I tried "Microsoft Internet Explorer 6" but that doesn't work. <SCRIPT LANGUAGE="Javascript"> <!-- bName = navigator.appName; if (bName =="Microsoft...
3
4336
by: PyPK | last post by:
Does anyone know of a simple implementation of a straight line detection algorithm something like hough or anything simpler.So something like if we have a 2D arary of pixel elements representing a particular Image. How can we identify lines in this Image. for example: ary = , , ,
7
2647
by: mosaic | last post by:
Hi, all I really interested in how to check the memory leak of a program. Your smart guys, do you have excellent ideas that could share with me? Thank you. The following is my idea: In C programming language, there's a "malloc", there must a "free", my solution of the detection of leak is, find the corresponding "free" of "malloc". This...
0
1362
by: darrenhello | last post by:
hi there, I am doing my last year's project and I have a 'little' problem. I have to do an edge detection filter. for now, some normal edge detection filters that I used worked fine but there a problem. I need an edge detection filter in which I can offset the edge according to a variable that the user gives. The problem is that I got no idea...
0
1904
by: origami.takarana | last post by:
Intrusion Detection Strategies ----------------------------------- Until now, we’ve primarily discussed monitoring in how it relates to intrusion detection, but there’s more to an overall intrusion detection installation than monitoring alone. Monitoring can help you spot problems in your network, as well as identify performance problems,...
10
3246
by: Conrad Lender | last post by:
In a recent thread in this group, I said that in some cases object detection and feature tests weren't sufficient in the development of cross-browser applications, and that there were situations where you could improve the application by detecting the browser vendor/version. Some of the posters here disagreed. Since then, I've had to deal with...
0
7885
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7948
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.