Results 1 to 6 of 6

Thread: [RESOLVED] Keeping track of ellipses

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    46

    Resolved [RESOLVED] Keeping track of ellipses

    Suppose i want to draw semi-random ellipses on my form.

    Is there any way i can check if they get clicked at a later stage?

    Is there any way of storing the location of individual ellipses and then comparing the mouse-click to my list of locations to see if one was clicked?

    I can imagine how it could be done with squares or rectangles, but i dont know how to approach it with ellipses.

    Im sure this has been asked before, but i've spent a good while searching around for something i can use and came up emptyhanded.

    Thanks a bunch!

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: Keeping track of ellipses

    you'd store the rectangle bounds of the ellipse, in a list(of rectangle). then you'd use a mathematical formula to check if the mouseclick was within the ellipse, within it's bounding rectangle.

    i don't know the formula you'd use though. try asking in the math forum

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    46

    Re: Keeping track of ellipses

    Yes, i figured it was something like that. I guess its the easiest way ^^
    We'll see if its beyond my abilities or not. Gonna go hunt for that formula. Thanks alot! ^^

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [RESOLVED] Keeping track of ellipses

    It's actually pretty easy to figure out if the point is contained in rectangle. Something like this:

    Code:
        Dim allEllipses As List(Of Rectangle)
        Dim rand As New Random()
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            allEllipses = New List(Of Rectangle)
            Using g As Graphics = Graphics.FromHwnd(Me.Handle)
    
                For i = 1 To 5
    
                    Dim s As SizeF = g.MeasureString("...", Me.Font)
                    Dim p As Point = New Point(rand.Next(0, Me.Width), rand.Next(0, Me.Height))
                    g.DrawString("...", Me.Font, Brushes.Red, p)
                    Dim r As New Rectangle(p, s.ToSize())
                    allEllipses.Add(r)
                Next
            End Using
    
        End Sub
    
        Private Sub Form1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseClick
            For Each r As Rectangle In allEllipses
                If r.Contains(e.Location) Then
                    MessageBox.Show("Clicked")
                End If
            Next
        End Sub

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: [RESOLVED] Keeping track of ellipses

    i thought of a different method, using a hidden picturebox:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim boundingRectangles() As Rectangle
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         boundingRectangles = New Rectangle() {New Rectangle(20, 20, 100, 100), New Rectangle(50, 50, 200, 200)}
    7.     End Sub
    8.  
    9.     Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    10.         For Each r As Rectangle In boundingRectangles
    11.             If r.Contains(e.Location) Then
    12.                 Dim pb As New PictureBox
    13.                 pb.Bounds = r
    14.                 Dim r2 As Rectangle = r
    15.                 r2.Offset(-r2.Left, -r2.Top)
    16.                 Dim path As New Drawing.Drawing2D.GraphicsPath
    17.                 path.AddEllipse(r2)
    18.                 pb.Region = New Region(path)
    19.                 Dim p As Point = e.Location
    20.                 p.Offset(-r.Left, -r.Top)
    21.                 If pb.Region.IsVisible(p) Then
    22.                     MsgBox("point in ellipse " & Array.IndexOf(boundingRectangles, r))
    23.                 End If
    24.                 pb.Dispose()
    25.             End If
    26.         Next
    27.     End Sub
    28.  
    29.     Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    30.         For Each r As Rectangle In boundingRectangles
    31.             e.Graphics.DrawEllipse(Pens.Black, r)
    32.         Next
    33.     End Sub
    34.    
    35. End Class

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    46

    Re: [RESOLVED] Keeping track of ellipses

    Let me just say that i am impressed with what you guys can do with a little time on their hands (and i mean little), hehe.

    I deeply appreciate it! I will study it and see if i can figure it out ^^

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width