我正在尝试根据中心点创建时钟指针坐标(我已经有了中心点)。这是我到目前为止的代码:
我的 VB.NET 表格上有什么:
http://i58.tinypic.com/3ehwl.png
Private Sub CreateClockPoints(ByVal centerPt As VISIPoint)
Dim vPtD As New VISIPoint
If rbn1.Checked Then
vPtD.Put(centerPt.X, centerPt.Y + Util.convertVal(1.5, 1, 2), centerPt.Z)
ElseIf rbn2.Checked Then
vPtD.Put(centerPt.X, centerPt.Y, centerPt.Z)
ElseIf rbn3.Checked Then
vPtD.Put(centerPt.X, centerPt.Y, centerPt.Z)
ElseIf rbn4.Checked Then
vPtD.Put(centerPt.X + Util.convertVal(1.5, 1, 2), centerPt.Y, centerPt.Z)
ElseIf rbn5.Checked Then
vPtD.Put(centerPt.X, centerPt.Y, centerPt.Z)
ElseIf rbn6.Checked Then
vPtD.Put(centerPt.X, centerPt.Y, centerPt.Z)
ElseIf rbn7.Checked Then
vPtD.Put(centerPt.X, centerPt.Y - Util.convertVal(1.5, 1, 2), centerPt.Z)
ElseIf rbn8.Checked Then
vPtD.Put(centerPt.X, centerPt.Y, centerPt.Z)
ElseIf rbn9.Checked Then
vPtD.Put(centerPt.X, centerPt.Y, centerPt.Z)
ElseIf rbn10.Checked Then
vPtD.Put(centerPt.X - Util.convertVal(1.5, 1, 2), centerPt.Y, centerPt.Z)
ElseIf rbn11.Checked Then
vPtD.Put(centerPt.X, centerPt.Y, centerPt.Z)
ElseIf rbn12.Checked Then
vPtD.Put(centerPt.X, centerPt.Y, centerPt.Z)
End If
End Sub
从中心到时钟点的距离必须为 1.5。
如您所见,我已经弄清楚了 1、4、7 和 10。我不确定 2、3、5、6、8、9、11 和 12 的公式是什么。如果您需要任何其他信息,请告诉我,谢谢。
这看起来像是家庭作业,所以我会剧透一些答案,我不会给你实际的代码。
假设您已经学习了三角函数并知道单位圆的公式。
您想要的偏移量 x 和 y 的公式可以从时钟指针的角度确定(我们称之为 theta
)。 由于有 12 小时,并且圆圈 360 度,因此theta = hours * 360 / 12
. 然后你可以得到:
x = 1.5 * 罪(θ)
和
<块引用类>y = -1.5 * 余弦(θ)
块引用类> 块引用类>一个普通的时钟应该在顶部有 12,在底部应该有 6
。要获取像素的坐标,您可以使用旋转矩阵将矢量从 0° 旋转到 359°
!这不是真正的 vb.net 代码!
function rotate(ByVal p as Point2D,ByVal angle as Single) as Point2D
Single X = p.X * cos(angle) - p.Y * sin(angle)
Single Y = p.X * sin(angle) + p.Y * cos(angle)
return new Point2D(X,Y)
end function
请注意,旋转矩阵将逆时针旋转,因此您应该否定角度。您应该在度角和您的时间之间创建某种映射
这可以通过将你的时间除以 12 并用 360° 进行多次调整来完成
function getAngleForTime(ByVal time as Integer) as Single
Dim scale = time / 12
return scale * 360
end function
此功能使 12:00 至 360° 和 3:00 至 90°
现在您可以旋转长度为 1.5 的矢量:
Dim time as Integer = 4
Dim angle as Single = getAngleForTime(time)
Dim p as new Point2D(1.5,0)
Dim p_r as Point2D = rotate(p, -angle) 'use negative angle here !
现在你有一个旋转的向量。要获得点的最终坐标,您必须将中心点的坐标添加到矢量:
Dim final_point as Point2D
final_point.X = p_r.X + center_point.X
final_point.Y = p_r.Y + center_point.Y
我希望这对你有所帮助。我没有视觉工作室来测试这段代码。请将此代码改编为您的示例。对不起,我的坏结局。
您需要使用一些基本的三角形来确定 X,Y 坐标。 假设有 12 个时钟点,您可以使用此函数来确定时钟上任何点的 X,Y 坐标。 请注意,这将返回标准笛卡尔平面的 X,Y 坐标。 您必须转换为 .Net 坐标系才能进行绘制。
'Returns the X,Y coordinate of a point on a clock given a center point and radius.
'Clock point 0 = 12:00, 1 = 1:00, etc.
Private Function getClockPoint(center As PointF, radius As Double, clockPoint As Integer) As PointF
Dim angle As Double = clockPoint * (2.0 * Math.PI) / 12.0
Dim x As Single = center.X + CSng(radius * Math.Sin(angle))
Dim y As Single = center.Y + CSng(radius * Math.Cos(angle))
Return New PointF(x, y)
End Function