2015-04-20 26 views
0

我想創建基於中心點(我已經有中心點)的時鐘手座標。這裏是我到目前爲止的代碼:創建基於偏離中心點的時鐘手座標

我有我的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的配方是什麼。讓我知道你是否需要任何額外的信息,謝謝。

+4

你對三角函數有什麼瞭解?如果你有角度,那麼計算'X = length * sin(angle)+ centerPt.X; Y =長度* cos(角度)+ centerPt.Y;'在3D中明顯更復雜一點(但想法相同):[維基救援](https://en.wikipedia.org/wiki/) Rotation_(數學)#Three_dimensions) – Carsten

+0

@CarstenKönig非常感謝,這就是我一直在尋找的東西。不熟悉數學知道這從我的頭頂,但正確的方向我都準備好了。再次感謝你,你應該讓這個答案,所以你得到信用。 – Joe

+0

沒問題 - 其他答案是一樣的 - 我得到了我需要的所有代表(沒有更多的里程碑獲得,而且我也永遠不會成爲J. Skeet) - 只需選擇最好的答案(接受)並接受它即可 – Carsten

回答

1

這看起來像一個家庭作業,所以我會搞砸一些答案,我不會給你實際的代碼。

讓我們假設你已經採取了三角法並且知道unit circle的公式。

偏移x和y想要的公式可以根據時鐘的角度來確定(我們將其稱爲theta)。由於有12個小時,並且360度爲一圈,所以theta = hours * 360/12。然後就可以得到:

X = 1.5 * SIN(THETA)

Y = -1.5 * cos(theta)

+1

Be當然,使用.Net Trig函數將角度轉換爲弧度。 –

+0

@ChrisDunaway,我故意指定我沒有給出真正的代碼,但是,這是.NET實現的一個重要注意事項。 –

+0

喲埃裏克我試圖upvote這個,但它說我沒有足夠的聲譽。非常感謝,這和以前的評論是我正在尋找! – Joe

0

一個正常的時鐘應該有12個頂部和6個底部。

得到您的像素座標,你可以使用一個旋轉matrix從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 div的你的時間來完成,並與360°

function getAngleForTime(ByVal time as Integer) as Single 
    Dim scale = time/12 
    return scale * 360 
end function 

MULT它這個功能使時間爲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 

我希望這對你有幫助。我沒有視覺工作室來測試這個代碼。請將此代碼適用於您的示例。對不起,我的壞結局。

0

您將需要使用一些基本的Trig來確定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