![]() |
Tipp 0306
|
Bresenham Linien-Algorithmus
|
 |
|
Autor/Einsender: Datum: |
|
Sebastian Bauersfeld 24.01.2003 |
|
Entwicklungsumgebung:
DirectX-Version: |
|
VB 6
DirectX 7 |
|
|
Das Beispiel soll zeigen, wie man eine Linie mit Hilfe des Bresenham-Algorithmus darstellt. Für grafische Anwendungen, die
DirectX verwenden, ist diese Methode besser geeignet, weil sie schneller als ihr GDI-Pendant
DrawLine ist. Der Grund dafür liegt darin, dass DrawLine
nur auf Software-Algorithmen basiert.
|
Mit SetLockedPixel hat dieses Beispiel jedoch Hardwareunterstützung und damit einen Geschwindigkeitsvorteil. Das Beispiel ist noch lange nicht voll optimiert. Ein möglicher Denkansatz wäre,
eine Linie aus sich wiederholenden Mustern.
|
Alle DirectX-Methoden befinden sich im Modul
DX_STUFF, weil diese nicht von Bedeutung für das Verständnis sind.
|
|
Code im Codebereich des Moduls
BRESENHAM_LINE |
|
|
Option Explicit
Public Sub BresLine(ByVal x1 As Long, ByVal y1 As Long, ByVal x2 _
As Long, ByVal y2 As Long, ByVal Color As Long)
Dim LWidth As Long
Dim LHeight As Long
Dim d As Long
Dim ix As Long
Dim iy As Long
Dim dd As Integer
Dim id As Integer
Dim EmptyRect As RECT
Dim SurfDesc As DDSURFACEDESC2
BackBuffer.Lock EmptyRect, SurfDesc, DDLOCK_WRITEONLY, 0
LWidth = x2 - x1
LHeight = y2 - y1
d = 0
If LWidth < 0 Then
LWidth = -LWidth
ix = -1
Else
ix = 1
End If
If LHeight < 0 Then
LHeight = -LHeight
iy = -1
Else
iy = 1
End If
If LWidth > LHeight Then
dd = LWidth + LWidth
id = LHeight + LHeight
Do
BackBuffer.SetLockedPixel x1, y1, Color
If x1 = x2 Then Exit Do
x1 = x1 + ix
d = d + id
If d > LWidth Then
y1 = y1 + iy
d = d - dd
End If
Loop
Else
dd = LHeight + LHeight
id = LWidth + LWidth
Do
BackBuffer.SetLockedPixel x1, y1, Color
If y1 = y2 Then Exit Do
y1 = y1 + iy
d = d + id
If d > LHeight Then
x1 = x1 + ix
d = d - dd
End If
Loop
End If
BackBuffer.Unlock EmptyRect
End Sub
|
|
|
|
|
|
Dieser Algorithmus ist nur schneller, wenn er in eine EXE-Datei kompiliert wurde! In der Entwicklungsumgebung ist er genauso schnell oder
langsam wie DrawLine !
|
Um dieses Beispiel ausführen zu können, wird die DirectX 7
for Visual Basic Type Library
benötigt (siehe dazu die Erläuterungen in der DirectX-Rubrik).
|
|
Windows-Version |
95 |
 |
|
98/SE |
 |
|
ME |
 |
|
NT |
 |
|
2000 |
 |
|
XP |
 |
|
Vista |
 |
|
Win
7 |
 |
|
|
VB-Version |
VBA 5 |
 |
|
VBA 6 |
 |
|
VB 4/16 |
 |
|
VB 4/32 |
 |
|
VB 5 |
 |
|
VB 6 |
 |
|
|
|
Download (6
kB)
|
Downloads bisher: [ 838 ]
|
|
|