1 #Region "Microsoft.VisualBasic::70e707135b08f5cea088c478eeaea13c, Microsoft.VisualBasic.Core\Extensions\Image\Math\Models\Polygon.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Structure Polygon
35     
36     '         Properties: Length
37     
38     '         Constructor: (+3 OverloadsSub New
39     '         FunctionGetEnumerator, IEnumerable_GetEnumerator
40     
41     
42     ' /********************************************************************************/
43
44 #End Region
45
46 Imports System.Drawing
47 Imports System.Runtime.CompilerServices
48
49 Namespace Imaging.Math2D
50
51     Public Structure Polygon : Implements IEnumerable(Of PointF)
52
53         Dim Points As PointF()
54
55         Public ReadOnly Property Length() As Integer
56             Get
57                 Return Points.Length
58             End Get
59         End Property
60
61         Default Public Property Item(index As IntegerAs PointF
62             Get
63                 Return Points(index)
64             End Get
65             Set
66                 Points(index) = Value
67             End Set
68         End Property
69
70         Public Sub New(points As PointF())
71             Me.Points = points
72         End Sub
73
74         <MethodImpl(MethodImplOptions.AggressiveInlining)>
75         Sub New(rect As Rectangle)
76             Call Me.New(New RectangleF(rect.Left, rect.Top, rect.Width, rect.Height))
77         End Sub
78
79         ''' <summary>
80         ''' 
81         ''' </summary>
82         ''' <param name="rect">
83         ''' 四个顶点是具有前后顺序的,按照顺序构建出一个四方形
84         ''' </param>
85         Sub New(rect As RectangleF)
86             Call Me.New({
87                 New PointF(rect.Left, rect.Top),
88                 New PointF(rect.Right, rect.Top),
89                 New PointF(rect.Right, rect.Bottom),
90                 New PointF(rect.Left, rect.Bottom)
91             })
92         End Sub
93
94         Public Shared Widening Operator CType(polygon As Polygon) As PointF()
95             Return polygon.Points
96         End Operator
97
98         Public Shared Widening Operator CType(points As PointF()) As Polygon
99             Return New Polygon(points)
100         End Operator
101
102         Private Function IEnumerable_GetEnumerator() As IEnumerator(Of PointF) Implements IEnumerable(Of PointF).GetEnumerator
103             Return DirectCast(Points.GetEnumerator(), IEnumerator(Of PointF))
104         End Function
105
106         Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
107             Return Points.GetEnumerator()
108         End Function
109     End Structure
110 End Namespace