Keep a point inside a region?

  • Thread starter Thread starter Brian Basquille
  • Start date Start date
B

Brian Basquille

Hello all,

Quick question!

How would i keep a point inside a region? For example, the mouse pointer.
You should be able to move the mouse around in this region but not anywhere
outside it.

How would i go about doing this?

Thanks in advance,

Brian
 
Do a test move that creates a point from the current point and the new
delta. If its inside the region, you check this ising Region.IsVisible, make
the move permanent. If not, discard it.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Cheers Bob.. but that was probably a bad example as i've implemented it
using the mouse position myself but it's not really ideal as what i want to
do is ensure the position of a paddle is inside of a player's region.

Since the mouse controls the paddle.. i assumed simply using the mouse
positions with Region.IsVisible would work. But if you move the mouse
quickly outside of the region, it won't redraw it where it should (at the
edge of the region).. it'll simply freeze at the last place it was drawn.

I should really be using the X and Y position of the paddle but that does
not seem to work.. as the paddle will stick as soon as it goes outside of
the region, with no way to move it back inside the region (as it's already
outside, you're disregarding any movement it's attempting).

Quite difficult to word there.. and probably more difficult to understand.

The code i was using for the mouse position was:

Point paddle1Position = new Point(e.X, e.Y);
bool isVisibleInP1Reg = player1Rgn.IsVisible(paddle1Position);

if(isVisibleInP1Reg == true)
{
paddle1_x = e.X;
paddle1_y = e.Y;
}
else
{
//do nothing
}
 
The game needs to capture the mouse and also probably get the current mouse
position and extrapolate a line from the last position to the current one to
see if it exits the region. Bresenhams line algorithm will give you all the
points between two coordinates.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Bob,

Cheers for the info.

Read up on that Bresenham's Line Algorithm.. seems a bit too complicated for
me, especially at this time of night.

Will come back to that problem in a couple of days.

Thanks again!
 
Back
Top