TizzyT

Circle Structure -TizzyT

Jul 13th, 2015
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.58 KB | None | 0 0
  1.     Public Structure CircleF
  2.         Private _Diameter As Single
  3.         Private _Radius As Single
  4.         Private _Offset As PointF
  5.         Private _CenterOffset As PointF
  6.         Private _Size As SizeF
  7.  
  8.         'Takes 3 singles and creates a new circle
  9.         Public Sub New(ByVal X As Single,
  10.                        ByVal Y As Single,
  11.                        ByVal Radius As Single)
  12.             _Diameter = Radius * 2
  13.             _Radius = Radius
  14.             _Offset = New PointF(X, Y)
  15.             _CenterOffset = New PointF(X + Radius, Y + Radius)
  16.             _Size = New SizeF(_Diameter, _Diameter)
  17.         End Sub
  18.  
  19.         'Returns the height of the circle
  20.         Public ReadOnly Property Height() As Single
  21.             Get
  22.                 Return _Diameter
  23.             End Get
  24.         End Property
  25.  
  26.         'Returns the top left corner of a rectangle that fits the circle
  27.         Public ReadOnly Property Offset() As PointF
  28.             Get
  29.                 Return _Offset
  30.             End Get
  31.         End Property
  32.  
  33.         'Returns the Size of the circle
  34.         Public ReadOnly Property Size() As SizeF
  35.             Get
  36.                 Return _Size
  37.             End Get
  38.         End Property
  39.  
  40.         'Returns the width of the circle
  41.         Public ReadOnly Property Width() As Single
  42.             Get
  43.                 Return _Diameter
  44.             End Get
  45.         End Property
  46.  
  47.         'Returns the radius of the circle
  48.         Public ReadOnly Property Radius() As Single
  49.             Get
  50.                 Return _Radius
  51.             End Get
  52.         End Property
  53.  
  54.         'Returns the Diameter length
  55.         Public ReadOnly Property Diameter() As Single
  56.             Get
  57.                 Return _Diameter
  58.             End Get
  59.         End Property
  60.  
  61.         'Returns the center coordinates of the circle
  62.         Public ReadOnly Property CenterOffset() As PointF
  63.             Get
  64.                 Return _CenterOffset
  65.             End Get
  66.         End Property
  67.  
  68.         'Returns whether this circle intersects with another circle
  69.         Public Function Intersects(ByVal Circle2 As CircleF) As Boolean
  70.             Return CircleIntersectsCircle(Me, Circle2)
  71.         End Function
  72.  
  73.         'Returns whether this circle intersects with a specified rectangle
  74.         Public Function Intersects(ByVal Rectangle As RectangleF) As Boolean
  75.             Return CircleIntersectsRectangle(Me, Rectangle)
  76.         End Function
  77.  
  78.         'Returns whether this circle contains a given point
  79.         Public Function Contains(ByVal Point As PointF) As Boolean
  80.             Return CircleContainsPoint(Me, Point)
  81.         End Function
  82.  
  83.         'Returns whether a given point is inside a circle
  84.         Public Shared Function CircleContainsPoint(ByVal Circle As CircleF, ByVal Point As PointF) As Boolean
  85.             If ((Circle.CenterOffset.X - Point.X) ^ 2) + ((Circle.CenterOffset.Y - Point.Y) ^ 2) <= Circle.Radius ^ 2 Then Return True
  86.             Return False
  87.         End Function
  88.  
  89.         'Returns whether 2 given circles intersect
  90.         Public Shared Function CircleIntersectsCircle(ByVal C1 As CircleF, ByVal C2 As CircleF) As Boolean
  91.             If (C1.Radius + C2.Radius) ^ 2 >= ((C1.CenterOffset.X - C2.CenterOffset.X) ^ 2) + ((C1.CenterOffset.Y - C2.CenterOffset.Y) ^ 2) Then Return True
  92.             Return False
  93.         End Function
  94.  
  95.         'Returns whether a circle intersects with a rectangle
  96.         Public Shared Function CircleIntersectsRectangle(ByVal Circle As CircleF, ByVal Rectangle As RectangleF) As Boolean
  97.             Dim X_Width As Single = Rectangle.X + Rectangle.Width
  98.             Dim Y_Height As Single = Rectangle.Y + Rectangle.Height
  99.             If Circle.CenterOffset.X >= Rectangle.X - Circle.Radius _
  100.             AndAlso Circle.CenterOffset.X <= X_Width + Circle.Radius _
  101.             AndAlso Circle.CenterOffset.Y >= Rectangle.Y - Circle.Radius _
  102.             AndAlso Circle.CenterOffset.Y <= Y_Height + Circle.Radius Then
  103.                 If Circle.CenterOffset.X >= Rectangle.X AndAlso Circle.CenterOffset.X <= X_Width Then Return True
  104.                 If Circle.CenterOffset.Y >= Rectangle.Y AndAlso Circle.CenterOffset.Y <= Y_Height Then Return True
  105.                 If Circle.Contains(New PointF(Rectangle.X, Rectangle.Y)) Then Return True
  106.                 If Circle.Contains(New PointF(Rectangle.X, Y_Height)) Then Return True
  107.                 If Circle.Contains(New PointF(X_Width, Rectangle.Y)) Then Return True
  108.                 If Circle.Contains(New PointF(X_Width, Y_Height)) Then Return True
  109.             End If
  110.             Return False
  111.         End Function
  112.     End Structure
Advertisement
Add Comment
Please, Sign In to add comment