## Bresenham3D

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

### Bresenham3D

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: 234
Joined: Oct 08, 2009 17:25

### Re: Bresenham3D

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, 32Color RGB(0, 0, 0), RGB(255, 255, 255) : ClsSub 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),ccEnd SubSub 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    LoopEnd Sub'draw x,y,z axisBr_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 triangleBr_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))SleepEnd`

Regards.

 corrected a typo in the link.
BasicCoder2
Posts: 3050
Joined: Jan 01, 2009 7:03

### Re: Bresenham3D

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: 3050
Joined: Jan 01, 2009 7:03

### Re: Bresenham3D

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.htmlScreenRes 500, 500, 32Color RGB(0, 0, 0), RGB(255, 255, 255) : Clstype POINT3D    as integer x    as integer y    as integer z    as ulong   c   'color of pointend typesub 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,,,,fend subSub 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    LoopEnd Subdim as POINT3D pt0,pt1'draw x,y,z axispt0.c = rgb(255,0,0)pt0.x = -250pt0.y = 0pt0.z = 0pt1.x = 250pt1.y = 0pt1.z = 0Br_line_3d(pt0,pt1)pt0.c = rgb(0,255,0)pt0.x = 0pt0.y = -250pt0.z = 0pt1.x = 0pt1.y = 250pt1.z = 0Br_line_3d(pt0,pt1)pt0.c = rgb(0,0,255)pt0.x = 0pt0.y = 0pt0.z = -250pt1.x = 0pt1.y = 0pt1.z = 250Br_line_3d(pt0,pt1)'draw trianglept0.c = rgb(255,0,255)pt0.x = 0pt0.y = -100pt0.z = 0pt1.x = 0pt1.y = 0pt1.z = -100Br_line_3d(pt0,pt1)pt0.c = rgb(255,0,255)pt0.x = 0pt0.y = 0pt0.z = -100pt1.x = 100pt1.y = 0pt1.z = 0Br_line_3d(pt0,pt1) pt0.c = rgb(255,0,255)pt0.x = 100pt0.y = 0pt0.z = 0pt1.x = 0pt1.y = -100pt1.z = 0Br_line_3d(pt0,pt1)SleepEnd`