-1

I'm currently programming a Windows Form Application using Visual Studio 2015. The program "spawns" 24 circles at the start which will then disappear, one by one, as the user clicks the different circles. The problem is that I don't know how to check if the cursor is over an ellipse when the user clicks.

Thanks in advance!

Jim
  • 2,974
  • 2
  • 19
  • 29
VÄLFÄRD
  • 1
  • 2
  • 1
    You need to store your ellipse coordinates for example in a `List` or a `List` and use `GraphicsPath.IsVisible` method to perform hit test . Also you can create an `Ellipse` class and encapsulate drawing and hit testing in that class. Take a look at this example: [How can I treat the circle as a control after drawing it?](http://stackoverflow.com/questions/38345828/how-can-i-treat-the-circle-as-a-control-after-drawing-it) – Reza Aghaei Nov 09 '16 at 19:19
  • While a circle is an ellipse, an ellipse is not necessarily a circle. So are you creating circles or ellipses? – Sam Axe Nov 09 '16 at 19:24
  • @SamAxe I'm creating circles with the "FillEllipse" command, giving them the same value in height and width. – VÄLFÄRD Nov 09 '16 at 19:28
  • @RezaAghaei According to Visual Studio, the `GraphicsPath` type doesn't exist. :( – VÄLFÄRD Nov 09 '16 at 19:30
  • @VÄLFÄRD, according to Microsoft's [MSDN documentation](https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.graphicspath(v=vs.110).aspx), it does. – adv12 Nov 09 '16 at 19:31
  • It's [`System.Drawing.Drawing2D.GraphicsPath`](https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.graphicspath(v=vs.110).aspx). You can use its full name or add `using System.Drawing.Drawing2D;` – Reza Aghaei Nov 09 '16 at 19:32
  • @RezaAghaei Ah, thanks! – VÄLFÄRD Nov 09 '16 at 19:33
  • `GraphicsPath`, in my experience, is incredibly slow. – Sam Axe Nov 09 '16 at 19:38
  • 2
    _GraphicsPath, in my experience, is incredibly slow_ Nonsense. – TaW Nov 09 '16 at 20:01

1 Answers1

1

If you store the center points and the radius1 then you can check the distance between the clicked point and all the centers. If the distance is less than the radius then you have a click within the circle bounds.

If you need to keep track of overlapping circles and only remove a the one on "top", then you need to store a z component with your point and radius data as well.

public class Circle {
    public int X {get; set;}  
    public int Y {get; set;}
    public int Z {get; set;}

    public int Radius {get; set;}
}

Store your data in a List<Circle>. Then you can easily extend the Circle class with methods like public bool Contains(int x, int y) { ... }, which makes writing the above algorithm as a LINQ query very easy.

Sam Axe
  • 33,313
  • 9
  • 55
  • 89