2016-04-26 32 views
1

ユーザの画面サイズ(この例では2000px幅、1000pxの高さ)の「四角形」の内側に、ダイナミックな六角形(この例では28)を配置する必要があります。他のすべての列を相殺したい。パーセンテージのマージンがあります。矩形内の矩形のサイズを計算する

私は、カスタムパネルを作成しようとしました、私のコードは以下の通りです:

Protected Overrides Function MeasureOverride(ByVal availableSize As Size) As Size 
    On Error Resume Next 

    Dim side As Double = ((((Me.ActualHeight * 2) + (Me.ActualWidth * 2))/6) - Me.InternalChildren.Count)/6 
    Dim w As Double = 2 * side 
    Dim h As Double = side * Math.Sqrt(3.0) 

    For Each child As FrameworkElement In InternalChildren 
     Dim childMaxSize = New Size(w, h) 
     child.Measure(childMaxSize) 
    Next 

    Return availableSize 
End Function 

Protected Overrides Function ArrangeOverride(ByVal finalSize As Size) As Size 
    On Error Resume Next 

    Dim side As Double = ((((Me.ActualHeight * 2) + (Me.ActualWidth * 2))/6) - Me.InternalChildren.Count)/6 
    Dim w As Double = 2 * side 
    Dim h As Double = side * Math.Sqrt(3.0) 

    Dim x As Double = 0 
    Dim y As Double = 0 
    Dim shift As Boolean = True 
    Dim shiftOffset As Double 

    Dim offsetChild As FrameworkElement = TryCast(InternalChildren(0), FrameworkElement) 
    shiftOffset = h/2 

    For i As Integer = 0 To InternalChildren.Count - 1 'For Each child As FrameworkElement In Me.InternalChildren 
     Dim child As FrameworkElement = TryCast(InternalChildren(i), FrameworkElement) 
     child.Margin = New Thickness(side * 0.02) 

     If child IsNot Nothing Then 
      Dim finalY As Double = y 

      If shift Then finalY += shiftOffset 

      shift = Not shift 
      child.Arrange(New Rect(New Point(x, finalY), New Size(w, h))) 

      x += w * 0.75 
      Dim nextWidth As Double = 0 

      If i + 1 < InternalChildren.Count Then nextWidth = w 

      If x + nextWidth > Me.ActualWidth Then 
       shift = True 
       x = 0 
       y += h 
      End If 
     End If 
    Next 

    Return finalSize 
End Function 
End Class 

私の問題は、サイドを計算しようとしています。私は近づいていますが、「四角形」にはまったく適合していないように見えますが、それ以外のものがあるかもしれません。

ご協力いただきありがとうございます。

EDIT: 少し違って説明します - 他のすべての六角形の列(テスト済み)をオフセットしないと、私の式が機能します。私の根本的な問題は、コードを配置する前に、指定された四角形に収まるように各六角形のサイズを測定しているということです。しかし、それらが配置され、オフセットされた後、私は四角形の外に出現する六角形を探しています。ああ、私の六角形もフラットトップです。ありがとう! 写真は私が必要とするものです。下は私が得ているものです。

want have

EDIT 2:: - オフセット(私のオフセット高さ×0.5) 私が側=側のMBOの提案を試みた 赤い六角形の中に収まるように必要な領域です。あまりにも小さな六角形を作るために表示されます

Protected Overrides Function MeasureOverride(ByVal availableSize As Size) As Size 
    On Error Resume Next 

    Dim side As Double = ((((Me.ActualHeight * 2) + (Me.ActualWidth * 2))/6) - Me.InternalChildren.Count)/6 
    Dim h As Double = side * Math.Sqrt(3.0) 

    'new 
    side = side - (h * 0.5) 
    h = side * Math.Sqrt(3.0) 

    Dim w As Double = 2 * side 

    For Each child As FrameworkElement In InternalChildren 
     Dim childMaxSize = New Size(w, h) 
     child.Measure(childMaxSize) 
    Next 

    Return availableSize 
End Function 

Protected Overrides Function ArrangeOverride(ByVal finalSize As Size) As Size 
    On Error Resume Next 

    Dim side As Double = ((((Me.ActualHeight * 2) + (Me.ActualWidth * 2))/6) - Me.InternalChildren.Count)/6 
    Dim h As Double = side * Math.Sqrt(3.0) 

    'new 
    side = side - (h * 0.5) 
    h = side * Math.Sqrt(3.0) 

    Dim w As Double = 2 * side 
+1

あなたは間違っている何が起こるかだ示す写真を添付し​​てもらえますか? – MBo

+0

何がありますか?目標パラメータとは何ですか?エッジサイズを求め、数式がオフセットなしで機能する場合は、オフセットを無視してサイズを見つけ、オフセットを減算して最終エッジサイズを取得する必要があります。 – MBo

+0

私はこれを考えましたが、オフセットから何を差し引くのですか?個々の身長、体重、またはその両方?目標の境界線が何を意味するのか分かりません - 長方形の意味ですか?この場合、2倍(1000 + 2000)になります。これは、 'side' doubleを計算する最初の部分で行っていることです。 – Turkwise

答えて

1

R列、C列、垂直六角形方位(先のとがったトッピング、二つの垂直エッジ)、エッジサイズAとの密な六角形グリッドのボックスのサイズを境界検討: enter image description here

Height = A/2 * (3 * R + 1) 

幅は、設定に依存:

同じ六角形の数C(N = R *のC)と奇数と偶数行:

Width = A * Sqrt(3)/2 * (2 * C + 1) 
E E E E 
O O O O 
E E E E 
O O O O 

奇数行は、C-1六角形(また、R = 1の場合)(N〜R *(C-1/2))と短いです:

Width = A * Sqrt(3) * C 
E E E E 
O O O 
E E E E 

あなたは必要な数に合わせてこれらの式を適用することができN個のヘクスを所定のボックスWxHに入れる?あなたはおそらく、(例えば、38=2x19はほぼ正方形領域には適していませんが、40=5x8または42=6x7が良いかもしれ空きスペースを最小限にするためにR、Cの要因をいくつかNのfactorisations(またはわずかに大きい数)を考慮することも必要

あなたの設定の場合)

編集
- フラット突破、最初の列は、第二のより低い:

BoundWidth = Side/2 * (3 * C + 1) 
BoundHeight = Side * Sqrt(3)/2 * (2 + R) = Height * (R/2 + 1) 
+0

ありがとうございました!私は下の方で質問を編集して、より明確に言葉を言いました。私はあなたのアドバイスを試みるつもりですが、私はすべての六角形を同じサイズにしたいと思います。私は本当に奇妙な空白を気にしない、私は彼らができる最高にフィットする必要があります。再度、感謝します! – Turkwise