Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Structure CircleF
- Private _Diameter As Single
- Private _Radius As Single
- Private _Offset As PointF
- Private _CenterOffset As PointF
- Private _Size As SizeF
- 'Takes 3 singles and creates a new circle
- Public Sub New(ByVal X As Single,
- ByVal Y As Single,
- ByVal Radius As Single)
- _Diameter = Radius * 2
- _Radius = Radius
- _Offset = New PointF(X, Y)
- _CenterOffset = New PointF(X + Radius, Y + Radius)
- _Size = New SizeF(_Diameter, _Diameter)
- End Sub
- 'Returns the height of the circle
- Public ReadOnly Property Height() As Single
- Get
- Return _Diameter
- End Get
- End Property
- 'Returns the top left corner of a rectangle that fits the circle
- Public ReadOnly Property Offset() As PointF
- Get
- Return _Offset
- End Get
- End Property
- 'Returns the Size of the circle
- Public ReadOnly Property Size() As SizeF
- Get
- Return _Size
- End Get
- End Property
- 'Returns the width of the circle
- Public ReadOnly Property Width() As Single
- Get
- Return _Diameter
- End Get
- End Property
- 'Returns the radius of the circle
- Public ReadOnly Property Radius() As Single
- Get
- Return _Radius
- End Get
- End Property
- 'Returns the Diameter length
- Public ReadOnly Property Diameter() As Single
- Get
- Return _Diameter
- End Get
- End Property
- 'Returns the center coordinates of the circle
- Public ReadOnly Property CenterOffset() As PointF
- Get
- Return _CenterOffset
- End Get
- End Property
- 'Returns whether this circle intersects with another circle
- Public Function Intersects(ByVal Circle2 As CircleF) As Boolean
- Return CircleIntersectsCircle(Me, Circle2)
- End Function
- 'Returns whether this circle intersects with a specified rectangle
- Public Function Intersects(ByVal Rectangle As RectangleF) As Boolean
- Return CircleIntersectsRectangle(Me, Rectangle)
- End Function
- 'Returns whether this circle contains a given point
- Public Function Contains(ByVal Point As PointF) As Boolean
- Return CircleContainsPoint(Me, Point)
- End Function
- 'Returns whether a given point is inside a circle
- Public Shared Function CircleContainsPoint(ByVal Circle As CircleF, ByVal Point As PointF) As Boolean
- If ((Circle.CenterOffset.X - Point.X) ^ 2) + ((Circle.CenterOffset.Y - Point.Y) ^ 2) <= Circle.Radius ^ 2 Then Return True
- Return False
- End Function
- 'Returns whether 2 given circles intersect
- Public Shared Function CircleIntersectsCircle(ByVal C1 As CircleF, ByVal C2 As CircleF) As Boolean
- If (C1.Radius + C2.Radius) ^ 2 >= ((C1.CenterOffset.X - C2.CenterOffset.X) ^ 2) + ((C1.CenterOffset.Y - C2.CenterOffset.Y) ^ 2) Then Return True
- Return False
- End Function
- 'Returns whether a circle intersects with a rectangle
- Public Shared Function CircleIntersectsRectangle(ByVal Circle As CircleF, ByVal Rectangle As RectangleF) As Boolean
- Dim X_Width As Single = Rectangle.X + Rectangle.Width
- Dim Y_Height As Single = Rectangle.Y + Rectangle.Height
- If Circle.CenterOffset.X >= Rectangle.X - Circle.Radius _
- AndAlso Circle.CenterOffset.X <= X_Width + Circle.Radius _
- AndAlso Circle.CenterOffset.Y >= Rectangle.Y - Circle.Radius _
- AndAlso Circle.CenterOffset.Y <= Y_Height + Circle.Radius Then
- If Circle.CenterOffset.X >= Rectangle.X AndAlso Circle.CenterOffset.X <= X_Width Then Return True
- If Circle.CenterOffset.Y >= Rectangle.Y AndAlso Circle.CenterOffset.Y <= Y_Height Then Return True
- If Circle.Contains(New PointF(Rectangle.X, Rectangle.Y)) Then Return True
- If Circle.Contains(New PointF(Rectangle.X, Y_Height)) Then Return True
- If Circle.Contains(New PointF(X_Width, Rectangle.Y)) Then Return True
- If Circle.Contains(New PointF(X_Width, Y_Height)) Then Return True
- End If
- Return False
- End Function
- End Structure
Advertisement
Add Comment
Please, Sign In to add comment