-- lua script for drawing Radial spokes
-- some basic constants
local w_mid = width / 2
local h_mid = height / 2
local radius = math.sqrt((w_mid * w_mid) + (h_mid * h_mid)) * 2
local r2d =  180 / math.pi
local r, g, b

-- allow the user to specify the number of radial spokes and location (note range is 0 .. 1.0) 
--@TBCONFIG SLIDER: X Axis, 50
--@TBCONFIG SLIDER: Y Axis, 50
--@TBCONFIG SLIDER: Spokes, 17
--@TBCONFIG SLIDER: Rotation, 0
--@TBCONFIG SLIDER: Thickness, 1
--@TBCONFIG FLAG: Color1 otherwise rainbow, true

local ucenterX = math.floor(sliderVal1 * width)
local ucenterY = math.floor(sliderVal2 * height)
local uspokes =  math.floor((sliderVal3 * 100))
local uangle =  (sliderVal4  * math.pi * 2 / uspokes )
local thickness = math.floor((sliderVal5 * 20))
if thickness == 0 then thickness = 1 end
local halfthickness = math.floor(thickness / 2 )
-- ------------------------------
-- note: functions must be defined BEFORE the calling logic calls them
-- ------------------------------

function plot(pointx,pointy, col)
     if (pointx > 0) and (pointy > 0) and (pointx < width) and (pointy < height) then  
     local	xx = pointx - thickness - 1 
     local	yy = pointy - thickness - 1	
     local x
     local y
    	for xi = 1, thickness do 
    	     x = xx + xi
    	     for yi = 1, thickness do 
    		y = yy + yi
    		if flagVal1 then 
    			set_rgba(x, y, r, g, b, 1)
    		else
    			set_alpha(x, y, 1)
    			set_hsl(x,y,col/spokes*360 ,0.5,0.5)
    		end
    	     end
    	end
    end 
end

function round(x)
    local xf = math.floor(x)
    if (x - xf) <= 0.5 then 
        return xf
    else
        return math.ceil(x)
    end 
end

-- Line Clipping Algorithm -- Liang-Barsky
function Liang_Barsky_clipTest(p, q, u1, u2)
local r
   if (p < 0) then 
        r = q / p
        if (r > u2) then 
            return 0, u1, u2
        elseif (r > u1) then 
            return 1, r, u2
        end
   elseif (p > 0) then 
      r = q / p
      if (r < u1) then 
         return 0, u1, u2
      elseif (r < u2) then 
            return 1, u1, r
      end
   elseif (q < 0) then 
      return 0, u1, u2
   end
   return 1, u1, u2
end

function Liang_Barsky_clipping(x0, y0, x1, y1)
local XS_MIN = 0
local XS_MAX = width
local YS_MIN = 0
local YS_MAX = height
local dx = x1 - x0
local dy = y1 - y0
local u1 = 0
local u2 = 1
local t1, t2, t3, t4 

t1, u1, u2 = Liang_Barsky_clipTest(-dx, x0-XS_MIN, u1, u2)
t2, u1, u2 = Liang_Barsky_clipTest( dx, XS_MAX-x0, u1, u2)
t3, u1, u2 = Liang_Barsky_clipTest(-dy, y0-YS_MIN, u1, u2)
t4, u1, u2 = Liang_Barsky_clipTest( dy, YS_MAX-y0, u1, u2)
    
   if (t1 == 1) and (t2 == 1) and (t3 == 1) and (t4 == 1)  then 
        if (u2 < 1) then 
            x1 = x0 + round(u2 * dx)
            y1 = y0 + round(u2 * dy)
        end
        if (u1 > 0) then 
            x0 = x0 + round(u1 * dx)
            y0 = y0 + round(u1 * dy)
        end
    end
    return x0, y0, x1, y1
end

-- Line Gen using a choice of two algorithms. 
-- Line Generating Algorithm using Digital Differential Analyzer 
function line_dda( x0, y0, x1, y1)  
    local x, y
    local delta
    x0, y0, x1, y1 = Liang_Barsky_clipping(x0, y0, x1, y1)
    if math.abs(x0-x1) > math.abs(y0-y1) then -- draw using X axis for greatest resolution
        delta =  (y1 - y0) /  (x1 - x0)
        y = y0
        if x0 > x1 then 
            t = x0; x0 = x1; x1 = t
            -- delta = -delta
            y = y1
        end
        for x = x0, x1 do
            plot(x, y)
            y = y + delta
        end
    else        -- draw using Y axis for greatest resolution
        delta =  (x1 - x0) /  (y1 - y0)
        x = x0
        if y0 > y1 then 
            t = y0; y0 = y1; y1 = t
            x = x1
        end
        for y = y0, y1 do
            plot(x, y)
            x = x + delta
        end
    end
end

-- Line Generating Algorithm using Bresenham's technique
function line_bres(x0, y0, x1, y1)
   x0, y0, x1, y1 = Liang_Barsky_clipping(x0, y0, x1, y1)
    local steep, t
    if (math.abs(y1 - y0) > math.abs(x1 - x0)) then 
        steep = 1 
    else 
        steep = 0 
    end 

    if steep == 1 then
        t = x0; x0 = y0; y0 = t
        t = x1; x1 = y1; y1 = t
    end
    if x0 > x1 then
        t = x0; x0 = x1; x1 = t
        t = y0; y0 = y1; y1 = t
    end 

    local deltax = x1 - x0
    local deltay = math.abs(y1 - y0)
    local error = round(deltax / 2)
    local ystep
    local y = y0
    if y0 < y1 then ystep = 1 else ystep = -1 end
    
    for x = x0, x1 do
         if (steep == 1) then plot(y,x) else plot(x,y) end 
         error = error - deltay
         if error < 0 then
             y = y + ystep
             error = error + deltax
         end
    end
end

r, g, b = get_color1()

for y = 0, uspokes - 1 do
    local w1, h1
    local angle = ((360 / uspokes) * y)/ r2d + uangle
    w1 = round(radius * math.cos(angle)  + ucenterX)
    h1 = round(radius * math.sin(angle)  + ucenterY)
    line_bres(ucenterX , ucenterY, w1, h1, y) 
end


