Welcome, Guest. Please login or register.
November 23, 2014, 05:31:20 am

Login with username, password and session length
Members
Total Members: 488
Latest: Wimmie
Stats
Total Posts: 7318
Total Topics: 1178
Online Today: 7
Online Ever: 229
(April 05, 2011, 09:10:50 am)
Users Online
Users: 0
Guests: 1
Total: 1
Permissions

A Squeakyduck Design product
Pages: [1]
Print
Author Topic: Three Dimensional Line Algorithm  (Read 13726 times)
Flying Cucco
Cobra Owner
Jr. Member
*
Posts: 52



« on: May 31, 2009, 10:28:12 pm »

Prints (to debug console) a list of all the points/pixels a line passes through from (x0,y0,z0) to (x1,y1,z1), i.e. Bresenham's in 3D.

Code:
program

uses pure2d, keyset

procedure Swap(byref a :integer, byref b :integer)
var
    c   :integer
begin
    c := a
    a := b
    b := c
end

var
    x, x0, x1, delta_x, step_x  :integer
    y, y0, y1, delta_y, step_y  :integer
    z, z0, z1, delta_z, step_z  :integer
    swap_xy, swap_xz            :boolean
    drift_xy, drift_xz          :integer
    cx, cy, cz                  :integer

begin

    //start and end points (change these values)
    x0 := 0     x1 := -2
    y0 := 0     y1 := 5
    z0 := 0     z1 := -10
    
    //'steep' xy Line, make longest delta x plane 
    swap_xy := Abs(y1 - y0) > Abs(x1 - x0)
    if swap_xy
        Swap(x0, y0)
        Swap(x1, y1)
    endif
               
    //do same for xz
    swap_xz := Abs(z1 - z0) > Abs(x1 - x0) 
    if swap_xz
        Swap(x0, z0)
        Swap(x1, z1)
    endif
    
    //delta is Length in each plane
    delta_x := Abs(x1 - x0)
    delta_y := Abs(y1 - y0)
    delta_z := Abs(z1 - z0)
    
    //drift controls when to step in 'shallow' planes
    //starting value keeps Line centred
    drift_xy  := (delta_x / 2)
    drift_xz  := (delta_x / 2)
    
    //direction of line
    step_x := 1;  if (x0 > x1) then  step_x := -1
    step_y := 1;  if (y0 > y1) then  step_y := -1
    step_z := 1;  if (z0 > z1) then  step_z := -1
    
    //starting point
    y := y0
    z := z0
    
    //step through longest delta (which we have swapped to x)
    for x = x0 to x1 step step_x
        
        //copy position
        cx := x;    cy := y;    cz := z

        //unswap (in reverse)
        if swap_xz then Swap(cx, cz)
        if swap_xy then Swap(cx, cy)
        
        //passes through this point
        debugmsg(":" + cx + ", " + cy + ", " + cz)
        
        //update progress in other planes
        drift_xy = drift_xy - delta_y
        drift_xz = drift_xz - delta_z

        //step in y plane
        if drift_xy < 0
            y = y + step_y
            drift_xy = drift_xy + delta_x
        endif  
        
        //same in z
        if drift_xz < 0
            z = z + step_z
            drift_xz = drift_xz + delta_x
        endif
    next

end
« Last Edit: June 06, 2009, 10:44:37 am by Flying Cucco » Logged
Jason W.
Cobra Owner
Jr. Member
*
Posts: 186



« Reply #1 on: June 05, 2009, 05:59:19 pm »

I need to test this out  Grin ...thanks Flying Cucco! 

Jason
Logged
Pages: [1]
Print
Jump to: