Private Type PointApi x As Long y As Long
End Type
Dim oXY As PointApi, rndXY As PointApi'定义原点坐标和随机点坐标形成原始直线
Private Function getNewZb(jiao As Long, oldZb As PointApi, xyo As PointApi) As PointApi '根据圆点坐标、圆上一点坐标、圆半径求转过一个角度jiao后的坐标 Dim x As Single, y As Single, h As Single h = jiao * 3.1415926 / 180 '把角度换算为弧度 x = (oldZb.x - xyo.x) * Cos(h) + (oldZb.y - xyo.y) * Sin(h) + xyo.x y = (oldZb.y - xyo.y) * Cos(h) + xyo.y - (oldZb.x - xyo.x) * Sin(h) '记住上述两个算法,复习复习下三角函数就知道为什么这样算了。 getNewZb.x = x getNewZb.y = y
End Function
Private Sub Command1_Click() Dim nXY As PointApi Me.Cls Me.Line (oXY.x, oXY.y)-(rndXY.x, rndXY.y), vbBlue'用蓝色画参考直线 nXY = getNewZb(Val(Text1), rndXY, oXY) Me.Line (oXY.x, oXY.y)-(nXY.x, nXY.y), vbRed '用红色画指定角度直线
End Sub
Private Sub Form_Load() Dim l As Long oXY.x = Me.Width * 0.5 oXY.y = Me.Height * 0.5 '坐标原点位于窗体中间 Randomize l = Me.Height * 0.25 If Me.Width < Me.Height Then l = Me.Width * 0.3 l = l + Rnd * l '产生一个随机数作为另一点的x坐标 rndXY.x = l + oXY.x rndXY.y = oXY.y'首先虚拟一个直线,该直线是平行于x轴的 Randomize l = 360 * Rnd '产生一个随机角度 rndXY = getNewZb(l, rndXY, oXY)'根据虚拟直线和随机角度获取实际的随机参考直线坐标 Me.Line (oXY.x, oXY.y)-(rndXY.x, rndXY.y), vbBlue'用蓝色画参考直线
End Sub