Bresenham3D

Source-code only - please, don't post questions here.
BasicCoder2
Posts: 2753
Joined: Jan 01, 2009 7:03

Bresenham3D

Postby BasicCoder2 » Sep 13, 2017 22:34

deleted in favor of frisian's suggestion in the next post.
.
Last edited by BasicCoder2 on Sep 14, 2017 20:52, edited 1 time in total.
frisian
Posts: 231
Joined: Oct 08, 2009 17:25

Re: Bresenham3D

Postby frisian » Sep 14, 2017 20:14

BasicCoder2
Your program contains for i as integer = 0 to l that should be for i = 0 to l. The i in for i as integer = 0 to l is a different one then the one in dim as integer i.

Here is a other Bresenham in 3D that is much simpler. You should have a look there, it's a simple clear page with simple listings in C.

Code: Select all

ScreenRes 500, 500, 32
Color RGB(0, 0, 0), RGB(255, 255, 255) : Cls

Sub SETXYZ(x As Integer, y As Integer, z As Integer, cc As ULong)
    'circle ((x+z\2)+250,(y-z\2)+250),3,cc
    PSet ((x+z\2)+250,(y-z\2)+250),cc
End Sub

Sub Br_line_3d(x0 As Integer, y0 As Integer, z0 As Integer, x1 As Integer, y1 As Integer, z1 As Integer, Col As ULong)

    Dim As Integer dx = Abs(x1 - x0), sx = IIf(x0 < x1, 1, -1)
    Dim As Integer dy = Abs(y1 - y0), sy = IIf(y0 < y1, 1, -1)
    Dim As Integer dz = Abs(z1 - z0), sz = IIf(z0 < z1, 1, -1)

    Dim As Integer dm = IIf(dx > dy, dx, dy)
                   dm = IIf(dz > dm, dz, dm)
    Dim As Integer i = dm ' maximum difference
    x1 = dm \ 2 : y1 = dm \ 2 : z1 = dm \ 2 ' error offset

    Do
        SETXYZ(x0, y0, z0, col)
        If i = 0 Then Exit Do
        i -= 1
        x1 -= dx : If x1 < 0 Then x1 += dm : x0 += sx
        y1 -= dy : If y1 < 0 Then y1 += dm : y0 += sy
        z1 -= dz : If z1 < 0 Then z1 += dm : z0 += sz
    Loop

End Sub

'draw x,y,z axis
Br_line_3d(-250,0,0,250,0,0,RGB(255,0,0))
Br_line_3d(0,-250,0,0,250,0,RGB(0,255,0))
Br_line_3d(0,0,-250,0,0,250,RGB(0,0,255))

'draw triangle
Br_line_3d(  0, -100,    0,   0,    0, -100, RGB(255,0,255))
Br_line_3d(  0,    0, -100, 100,    0,    0, RGB(255,0,255))
Br_line_3d(100,    0,    0,   0, -100,    0, RGB(255,0,255))

Sleep
End

Regards.

[Edit] corrected a typo in the link.
BasicCoder2
Posts: 2753
Joined: Jan 01, 2009 7:03

Re: Bresenham3D

Postby BasicCoder2 » Sep 14, 2017 20:55

Thank you for your input and the link.
I noticed two of the triangle's sides were unconnected dots which was of concern.
.
BasicCoder2
Posts: 2753
Joined: Jan 01, 2009 7:03

Re: Bresenham3D

Postby BasicCoder2 » Sep 16, 2017 23:07

Changed the SETXYZ to display the isometric coordinates that I had been using in some computer game isometric displays.
It also now uses a POINT3D type.
There is still something wrong in the Bresenham3D algorithm in that some lines are not made up of connected pixels.

Code: Select all

'http://members.chello.at/~easyfilter/bresenham.html
ScreenRes 500, 500, 32
Color RGB(0, 0, 0), RGB(255, 255, 255) : Cls

type POINT3D
    as integer x
    as integer y
    as integer z
    as ulong   c   'color of point
end type

sub setXYZ(pt as POINT3D)
    dim as single tempX,tempY,tempZ
    tempX =  pt.x - pt.z
    tempY = (pt.x + pt.z) / 2
    tempZ = pt.y
    pset (tempX+250,(tempZ-tempY)+250),pt.c
    'circle (tempX+250,(tempZ-tempY)+250),1,cc,,,,f
end sub

Sub Br_line_3d(pt0 as POINT3D, pt1 as POINT3D)

    Dim As Integer dx = Abs(pt1.x - pt0.x), sx = IIf(pt0.x < pt1.x, 1, -1)
    Dim As Integer dy = Abs(pt1.y - pt0.y), sy = IIf(pt0.y < pt1.y, 1, -1)
    Dim As Integer dz = Abs(pt1.z - pt0.z), sz = IIf(pt0.z < pt1.z, 1, -1)

    Dim As Integer dm = IIf(dx > dy, dx, dy)
                   dm = IIf(dz > dm, dz, dm)
    Dim As Integer i = dm ' maximum difference
    pt1.x = dm \ 2 : pt1.y = dm \ 2 : pt1.z = dm \ 2 ' error offset

    Do
        SETXYZ(pt0)
        If i = 0 Then Exit Do
        i -= 1
        pt1.x -= dx : If pt1.x < 0 Then pt1.x += dm : pt0.x += sx
        pt1.y -= dy : If pt1.y < 0 Then pt1.y += dm : pt0.y += sy
        pt1.z -= dz : If pt1.z < 0 Then pt1.z += dm : pt0.z += sz
    Loop

End Sub

dim as POINT3D pt0,pt1
'draw x,y,z axis
pt0.c = rgb(255,0,0)
pt0.x = -250
pt0.y = 0
pt0.z = 0
pt1.x = 250
pt1.y = 0
pt1.z = 0
Br_line_3d(pt0,pt1)

pt0.c = rgb(0,255,0)
pt0.x = 0
pt0.y = -250
pt0.z = 0
pt1.x = 0
pt1.y = 250
pt1.z = 0
Br_line_3d(pt0,pt1)

pt0.c = rgb(0,0,255)
pt0.x = 0
pt0.y = 0
pt0.z = -250
pt1.x = 0
pt1.y = 0
pt1.z = 250
Br_line_3d(pt0,pt1)


'draw triangle
pt0.c = rgb(255,0,255)
pt0.x = 0
pt0.y = -100
pt0.z = 0
pt1.x = 0
pt1.y = 0
pt1.z = -100
Br_line_3d(pt0,pt1)

pt0.c = rgb(255,0,255)
pt0.x = 0
pt0.y = 0
pt0.z = -100
pt1.x = 100
pt1.y = 0
pt1.z = 0
Br_line_3d(pt0,pt1)

pt0.c = rgb(255,0,255)
pt0.x = 100
pt0.y = 0
pt0.z = 0
pt1.x = 0
pt1.y = -100
pt1.z = 0
Br_line_3d(pt0,pt1)

Sleep
End

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 2 guests