VB.NET Liang-Barskyの実装の問題
-
29-09-2019 - |
質問
いくつかの調査の後、私はそれを使用することにしました Liang-Barsky 2Dゲームのラインクリッピングアルゴリズム。 Googleは、このアルゴリズムのVB.NET実装を提供しませんでしたが、多くのC/++の実装を提供しました。したがって、私はC ++の知識を持っているので、 スカイトピアで見つかったポート1 vb.netまで。残念ながら、それは動作しません:
Public Class PhysicsObject
Public Function CollideRay(ByVal p0 As Point, ByVal p1 As Point, ByRef clip0 As Point, ByRef clip1 As Point) As Boolean
Dim t0 As Double = 0.0
Dim t1 As Double = 1.0
Dim xdelta As Double = p1.X - p0.X
Dim ydelta As Double = p1.Y - p0.Y
Dim p, q, r As Double
For edge = 0 To 3
' Traverse through left, right, bottom, top edges
If (edge = 0) Then
p = -xdelta
q = -(AABB.Left - p0.X)
ElseIf (edge = 1) Then
p = xdelta
q = (AABB.Right - p0.X)
ElseIf (edge = 2) Then
p = -ydelta
q = -(AABB.Bottom - p0.Y)
ElseIf (edge = 3) Then
p = ydelta
q = (AABB.Top - p0.Y)
End If
r = q / p
If p = 0 And q < 0 Then Return False ' Don't draw line at all. (parallel line outside)
If p < 0 Then
If r > t1 Then
Return False ' Don't draw line at all.
ElseIf r > t0 Then
t0 = r ' Line is clipped!
End If
ElseIf p > 0 Then
If r < t0 Then
Return False ' Don't draw line at all.
ElseIf r < t1 Then
t1 = r ' Line is clipped!
End If
End If
Next
clip0.X = p0.X + t0 * xdelta
clip0.Y = p0.Y + t0 * ydelta
clip1.X = p0.X + t1 * xdelta
clip1.Y = p0.Y + t1 * ydelta
Return True ' (clipped) line is drawn
End Function
Public AABB As Rectangle
End Class
次のようなクラス/メソッドを使用しています。
Dim testPhysics As PhysicsObject = New PhysicsObject
testPhysics.AABB = New Rectangle(30, 30, 20, 20)
Dim p0, p1 As Point
p0 = New Point(0, 0)
p1 = New Point(120, 120)
Dim clip0, clip1 As Point
clip0 = New Point(-1, -1)
clip1 = New Point(-1, -1)
GlobalRenderer.Graphics.DrawLine(Pens.LimeGreen, p0, p1)
If testPhysics.CollideRay(p0, p1, clip0, clip1) Then
GlobalRenderer.Graphics.DrawLine(Pens.Magenta, clip0, clip1)
End If
しかし Collideray メソッドは、3番目のエッジイテレーション(Edge = 3)、R <T0で失敗します。したがって、関数はfalseを返します。
誰かが私の問題を見つけることができるかどうか疑問に思っています Collideray 私は元気で本当に困惑しているので、この動作をもたらす機能です。
前もって感謝します。
解決
コードは別の座標系を想定しています、TopEdgeは 大きい リンクされたWebページのBottomEdgeよりも。テストは、下部が上部よりも大きい通常のグラフィック調整で動作します。下の引数と一番の引数を交換する必要があります。
所属していません StackOverflow