Problem with detecting points on screen
-
Hi i have a problem that is making me scratch my head until it hurts. Hopefully someone can help. Well in my project I have a graph were I plot information from a database. Anyways, when I draw my data to the screen I add each point to an arraylist. I use an arraylist since the number of points can vary. So I have my points drawn on screen so next is were my problem comes in. What I want to happen is when the mouse pointer goes over any of my points on my graph, then a small box pops up with more information. So in my mousemove event I have code as follows For Each p As Point In myCoordinatesArrayL gPath.AddEllipse(p.X, p.Y, 30, 30) If gPath.IsVisible(mouse.X, mouse.Y) Then 'Then make my small window pop up Else 'Do nothing or close small window if its up End If Next So im taking the points from my arraylist and adding an ellipse to a graphics path. Then I check the graphics path 'IsVisible' function if the mouse's x and y are over it. I have mixed results with this so far. Some strange and unexpected things have happened. I can get the window to pop up but sometimes it doesn't. I have done a bit of error checking were I have placed two labels on my form and I increment a number when ever the mouse is not over the points and then increments the other number when the mouse pointer is over the point. This has unexpected results such as when I have only one point on the screen and i move the mouse over the point then both label increment and when i have more than one point on the screen, its works as expected except for the first point on my graph where is produces the same problem. Hope you can make sense of this. Is there a better way to do what im trying to do. Any suggestions. Thanks for your help
-
Hi i have a problem that is making me scratch my head until it hurts. Hopefully someone can help. Well in my project I have a graph were I plot information from a database. Anyways, when I draw my data to the screen I add each point to an arraylist. I use an arraylist since the number of points can vary. So I have my points drawn on screen so next is were my problem comes in. What I want to happen is when the mouse pointer goes over any of my points on my graph, then a small box pops up with more information. So in my mousemove event I have code as follows For Each p As Point In myCoordinatesArrayL gPath.AddEllipse(p.X, p.Y, 30, 30) If gPath.IsVisible(mouse.X, mouse.Y) Then 'Then make my small window pop up Else 'Do nothing or close small window if its up End If Next So im taking the points from my arraylist and adding an ellipse to a graphics path. Then I check the graphics path 'IsVisible' function if the mouse's x and y are over it. I have mixed results with this so far. Some strange and unexpected things have happened. I can get the window to pop up but sometimes it doesn't. I have done a bit of error checking were I have placed two labels on my form and I increment a number when ever the mouse is not over the points and then increments the other number when the mouse pointer is over the point. This has unexpected results such as when I have only one point on the screen and i move the mouse over the point then both label increment and when i have more than one point on the screen, its works as expected except for the first point on my graph where is produces the same problem. Hope you can make sense of this. Is there a better way to do what im trying to do. Any suggestions. Thanks for your help
You creating an ellipse for every point in the graph every time the mouse moves?? Wouldn't it be easier to just track the mouse movement, mapping it to the closest point in the X axis, then just create the one ellipse to see if the mouse is in it? Also, the code looks incomplete. Are you Disposing the GraphicsPath you created when you're done with it?? Are you creating a new one every time you iterate through this loop?? You don't need to.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
You creating an ellipse for every point in the graph every time the mouse moves?? Wouldn't it be easier to just track the mouse movement, mapping it to the closest point in the X axis, then just create the one ellipse to see if the mouse is in it? Also, the code looks incomplete. Are you Disposing the GraphicsPath you created when you're done with it?? Are you creating a new one every time you iterate through this loop?? You don't need to.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Hi, Not sure I know what you mean. So i think that for every point in my arraylist that I should check its x coordinate against the mouse x coordinate and then if it does , then i should add the ellipse to my graphics path. This is what I've tried but its not really working for me For Each p As Point In myCoordinatesArrayL If p.X = mouse.X Then gPath.AddEllipse(p.X, p.Y, 30, 30) If gPath.IsVisible(mouse.X, mouse.Y) Then 'code for displaying my window End If Else 'do nothing or close my window End If Next I dont have code to dispose the graphicsPath here but in my forms closing method. Is this right Thanks for your time
-
Hi, Not sure I know what you mean. So i think that for every point in my arraylist that I should check its x coordinate against the mouse x coordinate and then if it does , then i should add the ellipse to my graphics path. This is what I've tried but its not really working for me For Each p As Point In myCoordinatesArrayL If p.X = mouse.X Then gPath.AddEllipse(p.X, p.Y, 30, 30) If gPath.IsVisible(mouse.X, mouse.Y) Then 'code for displaying my window End If Else 'do nothing or close my window End If Next I dont have code to dispose the graphicsPath here but in my forms closing method. Is this right Thanks for your time
No, you have to do a little math to figure out which point the X coord is NEAREST to on the graph. In your method, the mouse has to be EXACTLY on the point your looking for, not around it. If you have 3 points on the graph at 50, 75 and 100, you take the mouse X coord, say 80, then check it against the X coord of the points on the graph, plus or minus say 10. Doing the math is FAR faster than creating a graphics path to do the test for you.
For Each p As Point In myCoordinatesArrayL
If p.X - mouse.X <= 1- Then
' You don't even need the GraphicsPath since you just
' found the point you're looking for!
' If you really needed to, you can do the same test for the Y coordinate in here.
End If
NextA guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
No, you have to do a little math to figure out which point the X coord is NEAREST to on the graph. In your method, the mouse has to be EXACTLY on the point your looking for, not around it. If you have 3 points on the graph at 50, 75 and 100, you take the mouse X coord, say 80, then check it against the X coord of the points on the graph, plus or minus say 10. Doing the math is FAR faster than creating a graphics path to do the test for you.
For Each p As Point In myCoordinatesArrayL
If p.X - mouse.X <= 1- Then
' You don't even need the GraphicsPath since you just
' found the point you're looking for!
' If you really needed to, you can do the same test for the Y coordinate in here.
End If
NextA guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008