473,425 Members | 1,557 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

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.Windows.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.Drawing.Brushes.White;


//constructor
Form()
{
//set style for double buffered graphics
SetStyle
(
System.Windows.Forms.ControlStyles.AllPaintingInWm Paint|
System.Windows.Forms.ControlStyles.DoubleBuffer|
System.Windows.Forms.ControlStyles.ResizeRedraw|
System.Windows.Forms.ControlStyles.UserPaint,
true
);
//get a GraphicsPath object
System.Drawing.Drawing2D.GraphicsPath graphicspath=new
System.Drawing.Drawing2D.GraphicsPath();
//add a line to the graphicspath
graphicspath.AddLine(point1,point2);
//use the graphicspath to define the line-region
line=new System.Drawing.Region(graphicspath);
//use the rectangle to define the square-region
square=new System.Drawing.Region(rectangle);
//prepare for mouse-input
MouseMove+=new System.Windows.Forms.MouseEventHandler(OnMouseMove );
//prepare for paint
Paint+=new System.Windows.Forms.PaintEventHandler(OnPaint);

}


//OnMouseMove
void OnMouseMove(object a,System.Windows.Forms.MouseEventArgs b)
{

//use White brush
brush=System.Drawing.Brushes.White;

if(square.IsVisible(b.X,b.Y))
{

//use Red brush if mouse is over square
brush=System.Drawing.Brushes.Red;
}

else if(line.IsVisible(b.X,b.Y))
{

//use Blue brush if mouse is over line
brush=System.Drawing.Brushes.Blue;
}

//force redraw
Invalidate();
}


//OnPaint
void OnPaint(object a,System.Windows.Forms.PaintEventArgs b)
{
b.Graphics.DrawLine(System.Drawing.Pens.Black,poin t1,point2);
b.Graphics.DrawRectangle(System.Drawing.Pens.Black ,rectangle);
b.Graphics.FillRegion(brush,square);
}


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

}
}


Apr 16 '06 #1
2 10375
Martijn,

This could be because the region is actually empty it has no area. I'd
suggest using GrpahicsPaths. They support IsVisible and IsOutlineVisible
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.nl...
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.Windows.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.Drawing.Brushes.White;


//constructor
Form()
{
//set style for double buffered graphics
SetStyle
(
System.Windows.Forms.ControlStyles.AllPaintingInWm Paint|
System.Windows.Forms.ControlStyles.DoubleBuffer|
System.Windows.Forms.ControlStyles.ResizeRedraw|
System.Windows.Forms.ControlStyles.UserPaint,
true
);
//get a GraphicsPath object
System.Drawing.Drawing2D.GraphicsPath graphicspath=new
System.Drawing.Drawing2D.GraphicsPath();
//add a line to the graphicspath
graphicspath.AddLine(point1,point2);
//use the graphicspath to define the line-region
line=new System.Drawing.Region(graphicspath);
//use the rectangle to define the square-region
square=new System.Drawing.Region(rectangle);
//prepare for mouse-input
MouseMove+=new System.Windows.Forms.MouseEventHandler(OnMouseMove );
//prepare for paint
Paint+=new System.Windows.Forms.PaintEventHandler(OnPaint);

}


//OnMouseMove
void OnMouseMove(object a,System.Windows.Forms.MouseEventArgs b)
{

//use White brush
brush=System.Drawing.Brushes.White;

if(square.IsVisible(b.X,b.Y))
{

//use Red brush if mouse is over square
brush=System.Drawing.Brushes.Red;
}

else if(line.IsVisible(b.X,b.Y))
{

//use Blue brush if mouse is over line
brush=System.Drawing.Brushes.Blue;
}

//force redraw
Invalidate();
}


//OnPaint
void OnPaint(object a,System.Windows.Forms.PaintEventArgs b)
{
b.Graphics.DrawLine(System.Drawing.Pens.Black,poin t1,point2);
b.Graphics.DrawRectangle(System.Drawing.Pens.Black ,rectangle);
b.Graphics.FillRegion(brush,square);
}


//Main
public static void Main()
{
System.Windows.Forms.Application.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 IsOutlineVisible
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.Windows.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.GraphicsPath graphicspath=new
System.Drawing.Drawing2D.GraphicsPath();


//a brush
System.Drawing.Brush brush=System.Drawing.Brushes.White;


//constructor
Form()
{
//set style for double buffered graphics
SetStyle
(
System.Windows.Forms.ControlStyles.AllPaintingInWm Paint|
System.Windows.Forms.ControlStyles.DoubleBuffer|
System.Windows.Forms.ControlStyles.ResizeRedraw|
System.Windows.Forms.ControlStyles.UserPaint,
true
);
//add a line to the graphicspath
graphicspath.AddLine(point1,point2);

//use the rectangle to define the square-region
square=new System.Drawing.Region(rectangle);
//prepare for mouse-input
MouseMove+=new System.Windows.Forms.MouseEventHandler(OnMouseMove );
//prepare for paint
Paint+=new System.Windows.Forms.PaintEventHandler(OnPaint);

}


//OnMouseMove
void OnMouseMove(object a,System.Windows.Forms.MouseEventArgs b)
{

//use White brush
brush=System.Drawing.Brushes.White;

if(square.IsVisible(b.X,b.Y))
{

//use Red brush if mouse is over square
brush=System.Drawing.Brushes.Red;
}

else if(graphicspath.IsOutlineVisible(b.X,b.Y,System.Dr awing.Pens.Black))
{

//use Blue brush if mouse is over line
brush=System.Drawing.Brushes.Blue;
}

//force redraw
Invalidate();
}

//OnPaint
void OnPaint(object a,System.Windows.Forms.PaintEventArgs b)
{
b.Graphics.DrawLine(System.Drawing.Pens.Black,poin t1,point2);
b.Graphics.DrawRectangle(System.Drawing.Pens.Black ,rectangle);
b.Graphics.FillRectangle(brush,rectangle);
}

//Main
public static void Main()
{
System.Windows.Forms.Application.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
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...
3
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...
60
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 ...
6
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:...
8
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...
3
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...
7
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...
0
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...
0
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...
10
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.