473,378 Members | 1,680 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,378 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 10368
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.