|
-
Mar 8th, 2010, 11:39 AM
#1
Thread Starter
Member
[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!
-
Mar 8th, 2010, 12:07 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 8th, 2010, 12:13 PM
#3
Thread Starter
Member
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! ^^
-
Mar 8th, 2010, 12:20 PM
#4
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
-
Mar 8th, 2010, 12:45 PM
#5
Re: [RESOLVED] Keeping track of ellipses
i thought of a different method, using a hidden picturebox:
vb Code:
Public Class Form1
Dim boundingRectangles() As Rectangle
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
boundingRectangles = New Rectangle() {New Rectangle(20, 20, 100, 100), New Rectangle(50, 50, 200, 200)}
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
For Each r As Rectangle In boundingRectangles
If r.Contains(e.Location) Then
Dim pb As New PictureBox
pb.Bounds = r
Dim r2 As Rectangle = r
r2.Offset(-r2.Left, -r2.Top)
Dim path As New Drawing.Drawing2D.GraphicsPath
path.AddEllipse(r2)
pb.Region = New Region(path)
Dim p As Point = e.Location
p.Offset(-r.Left, -r.Top)
If pb.Region.IsVisible(p) Then
MsgBox("point in ellipse " & Array.IndexOf(boundingRectangles, r))
End If
pb.Dispose()
End If
Next
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
For Each r As Rectangle In boundingRectangles
e.Graphics.DrawEllipse(Pens.Black, r)
Next
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 8th, 2010, 01:01 PM
#6
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|