1 |
#Region "Microsoft.VisualBasic::54fe0fc3918c2d292daa3384d0b3894d, Microsoft.VisualBasic.Core\Extensions\Math\Trigonometric\Trigonometric.vb"
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
#End Region
|
43 |
|
44 |
Imports System.Drawing
|
45 |
Imports System.Runtime.CompilerServices
|
46 |
Imports sys = System.Math
|
47 |
|
48 |
Namespace Math
|
49 |
|
50 |
Public Module Trigonometric
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
<Extension> Public Function ToCartesianPoint(polar As (r#, alpha!), Optional fromDegree As Boolean = True) As PointF
|
59 |
Dim alpha = polar.alpha
|
60 |
|
61 |
If fromDegree Then
|
62 |
alpha = alpha * sys.PI / 180
|
63 |
End If
|
64 |
|
65 |
Dim x = polar.r * sys.Cos(alpha)
|
66 |
Dim y = polar.r * sys.Sin(alpha)
|
67 |
|
68 |
Return New PointF(x, y)
|
69 |
End Function
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
<param name="radian">``0 -> 2*<see cref="Math.PI"/>``</param>
|
75 |
|
76 |
Public Function GetAngleVector(radian As Single, Optional r As Double = 1) As PointF
|
77 |
Dim x = sys.Cos(radian) * r
|
78 |
Dim y = sys.Sin(radian) * r
|
79 |
|
80 |
Return New PointF(x, y)
|
81 |
End Function
|
82 |
|
83 |
|
84 |
计算结果为角度
|
85 |
|
86 |
<param name="p"></param>
|
87 |
|
88 |
|
89 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
90 |
<Extension>
|
91 |
Public Function Angle(p As PointF) As Double
|
92 |
Return sys.Atan2(p.Y, p.X)
|
93 |
End Function
|
94 |
|
95 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
96 |
Public Function Distance(a As Point, b As Point) As Double
|
97 |
Return sys.Sqrt((a.X - b.X) ^ 2 + (a.Y - b.Y) ^ 2)
|
98 |
End Function
|
99 |
|
100 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
101 |
Public Function Distance(a As PointF, b As PointF) As Double
|
102 |
Return sys.Sqrt((a.X - b.X) ^ 2 + (a.Y - b.Y) ^ 2)
|
103 |
End Function
|
104 |
|
105 |
Public Function GetAngle(p1 As Point, p2 As Point) As Double
|
106 |
Dim xDiff As Double = p2.X - p1.X
|
107 |
Dim yDiff As Double = p2.Y - p1.Y
|
108 |
Return 180 - (ToDegrees(sys.Atan2(yDiff, xDiff)) - 90)
|
109 |
End Function
|
110 |
|
111 |
<Extension>
|
112 |
Public Function MovePoint(p As Point, angle As Double, distance As Integer) As Point
|
113 |
p = New Point(p)
|
114 |
p.X += distance * sys.Sin(angle)
|
115 |
p.Y += distance * sys.Cos(angle)
|
116 |
|
117 |
Return p
|
118 |
End Function
|
119 |
|
120 |
|
121 |
Converts an angle measured in degrees to an approximately
|
122 |
equivalent angle measured in radians. The conversion from
|
123 |
degrees to radians is generally inexact.
|
124 |
|
125 |
<param name="angdeg"> an angle, in degrees </param>
|
126 |
<returns> the measurement of the angle {@code angdeg}
|
127 |
in radians.
|
128 |
@since 1.2 </returns>
|
129 |
|
130 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
131 |
<Extension> Public Function ToRadians(angdeg As Double) As Double
|
132 |
Return angdeg / 180.0 * sys.PI
|
133 |
End Function
|
134 |
|
135 |
|
136 |
Converts an angle measured in radians to an approximately
|
137 |
equivalent angle measured in degrees. The conversion from
|
138 |
radians to degrees is generally inexact; users should
|
139 |
<i>not</i> expect {@code cos(toRadians(90.0))} to exactly
|
140 |
equal {@code 0.0}.
|
141 |
|
142 |
<param name="angrad"> an angle, in radians </param>
|
143 |
<returns> the measurement of the angle {@code angrad}
|
144 |
in degrees.
|
145 |
@since 1.2 </returns>
|
146 |
|
147 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
148 |
<Extension> Public Function ToDegrees(angrad As Double) As Double
|
149 |
Return angrad * 180.0 / sys.PI
|
150 |
End Function
|
151 |
|
152 |
<Extension>
|
153 |
Public Function NearestPoint(points As IEnumerable(Of Point), x%, y%, radius#) As Point
|
154 |
For Each pos As Point In points
|
155 |
Dim dist = sys.Sqrt((x - pos.X) ^ 2 + (y - pos.Y) ^ 2)
|
156 |
|
157 |
If dist <= radius Then
|
158 |
Return pos
|
159 |
End If
|
160 |
Next
|
161 |
|
162 |
Return Nothing
|
163 |
End Function
|
164 |
End Module
|
165 |
End Namespace
|