fguihen Posted March 22, 2005 Posted March 22, 2005 i have to draw 10 circles on a form, all with the same diameter. they cannot overlap. the formula to see if they overlap is: SquareRoot( Squared(A.x -B.x) + Squared(A.y - B.y)) what i cant figure out is a system that will allow me to check each new circle against the previous ones, to see if they overlap, and if they do , to generate new random co-ordinates and re check. any ideas anyone? Quote
Leaders snarfblam Posted March 23, 2005 Leaders Posted March 23, 2005 Assuming that you are storing the existing's centers and radii in an array, Why can't you just loop through the existing circles and see if they overlap. I was bored so I made this: Structure Circle Dim Radius As Single Dim Location As PointF Shared Randomness As Random ' Shared Sub New() Randomness = New Random End Sub ' Public Sub New(ByVal MinRad As Single, ByVal MaxRad As Single, ByVal Region As RectangleF) Radius = CSng(Randomness.NextDouble() * (MaxRad - MinRad) + MinRad) Location.X = CSng(Randomness.NextDouble() * (Region.Width) + Region.Left) Location.Y = CSng(Randomness.NextDouble() * (Region.Height) + Region.Y) End Sub ' Public Function Overlaps(ByVal WithWhat As Circle) As Boolean 'If the distance of the centers is less than the sum of the radii then they overlap If ((WithWhat.Location.X - Me.Location.X) ^ 2 + _ (WithWhat.Location.Y - Me.Location.Y) ^ 2) ^ 0.5 _ < Me.Radius + WithWhat.Radius Then Return True Else Return False End If End Function ' Public ReadOnly Property ContainingRect() As RectangleF Get Return New RectangleF(Location.X - Radius, Location.Y - Radius, Radius * 2, Radius * 2) End Get End Property End Structure ' Dim Circles(9) As Circle ' Private Sub MakeSomeCirclesForMeBaby() Dim Overlap As Boolean = False For i As Integer = 0 To Circles.Length - 1 Do 'We will create new random circles using the constructor and loop until 'it does not overlap with any previous (j to 0) circles Circles(i) = New Circle(0, 10, New RectangleF(0, 0, 100, 100)) Overlap = False For j As Integer = i - 1 To 0 Step -1 If Circles(j).Overlaps(Circles(i)) Then Overlap = True End If Next Loop While Overlap Next End Sub Quote [sIGPIC]e[/sIGPIC]
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.