Denaes Posted January 31, 2007 Posted January 31, 2007 I'm drawing shapes on a picturebox. I want to know when I'm inside of a shape (and also outside of a shape). This is really easy for a rectangle, you just check the X & Y & width & height to see if the mouse is inside. Like say I have a wedge (using a piefill method to draw). The Rect covers the whole circle, but the Wedge is drawn with the Rect, a starting angle and an ending angle. I'm just not sure how to get at those points in the angle... I'm not sure if there is a method for the premade drawing methods that does this or if I have to use the easy methods and then do it all myself to find the points anyways (I guess by figuring out the two lines and the arc) Quote
Nate Bross Posted January 31, 2007 Posted January 31, 2007 I don't know of any built in methods for checking collision detection of non rectangular shapes; however, assuming you have a Right Triangle, and you know the length of the legs (height , and width) you can calculate the hypotenuse using the Pythagorean Theorem, and thus figure out if the mouse position is inside the area of the triangle or not. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
MrPaul Posted February 1, 2007 Posted February 1, 2007 GraphicsPath object If your piefill method can be adapted to create a GraphicsPath object instead of, or in addition to, the rendering, then you can use the IsVisible method to test whether a point (such as the cursor) lies within the segment. A GraphicsPath object can be created by adding the lines and curves that make up the pie segment. This approach will work with any kind of shape which can be represented as a GraphicsPath. Good luck :) Quote Never trouble another for what you can do for yourself.
Denaes Posted February 1, 2007 Author Posted February 1, 2007 Unfortunately not right triangles. Searching around I've seen mention of creating objects for each shape and then looping through the shapes to see if the point is in/out of bounds... that does simplify things a little, but you still have to compare the points to the shape/location of the shape you drew. A few tests rely on making a bitmap of the drawing (I think of the whole drawing screen because I don't think you can make irregular sided bitmaps), but those do a check for the color of the pixel at the point given, which works for color coded shapes. Quote
Denaes Posted February 1, 2007 Author Posted February 1, 2007 (edited) Hmm, the graphicsPath seems like a good place to store the image. Just have to see if there is an easy way to do it (ie, the .draw<shape> methods) or if you have to draw the shape by hand. Edit: GraphicsPath.AddPie works the same as Graphics.DrawPie (or so it seems) so I'll create an object for each of the shapes that will handle their graphicspath and checking mouse and drawing the shape to the screen. I'll post here later with an example if I get it working as I hope for future people with a similar problem Edited February 1, 2007 by Denaes Quote
Denaes Posted February 1, 2007 Author Posted February 1, 2007 Okay, I really went an easy route as I already had much of the pertinent info for shapes stored in objects already and everything had to be recalculated with redraws because I change sizes along with form resizes. So here is how it works: PiePath = New Drawing2D.GraphicsPath PiePath.AddPie(CircleRect, StartAngle, SweepAngle) If PiePath.IsVisible(MouseLoc) Then ' Mouse is over wedge, so highlight the wedge and display it's info g.FillPie(Brushes.Gray, CircleRect, StartAngle, SweepAngle) End If g.DrawPie(PiePen, CircleRect, StartAngle, SweepAngle) This checks each wedge prior to it being drawn and if the mouse is in it's shape, I use the FillPie to give it a color (kinda like highlighting it). This ended up being super easy with the GraphicsPath object, thanks :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.