By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,570 Members | 1,234 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,570 IT Pros & Developers. It's quick & easy.

Hit Detection on a Line

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.