-- Testing meta-circles FieldMap={} -- Clear FieldMap function ClearFieldMap(w,h) FieldMap["width"]=w FieldMap["height"]=h local i, j for i=1,w,1 do FieldMap[i]={} for j=1, h, 1 do FieldMap[i][j]=0 end end end -- Add Metacircle function MetaCircle(cx, cy, radius) local left, right, top, bottom left = cx-radius right = cx+radius top = cy-radius bottom = cy+radius print("doing: "..cx.." "..cy.." "..radius) local i,j for i=left, right, 1 do for j=top, bottom, 1 do local distance_squared = (cx-i)*(cx-i)+(cy-j)*(cy-j) local distance = sqrt(distance_squared) local inten = (radius - distance) / radius if inten<0 then inten=0 end if inten>1 then inten=1 end if i>=1 and i<=FieldMap["width"] and j>=1 and j<=FieldMap["height"] then --print(inten) FieldMap[i][j] = FieldMap[i][j] + inten end end end end -- Create a list to hold circles that are generated CircleList = List:new() ExtentRadius = 0 -- Circle encompassing entire fractal MinRadius = 3 MaxRadius = 7 -- Randomly choose a circle from the list function SelectCircle() local num = CircleList:len() local x,y, radius local which = RandRange(1, num) local circle = CircleList[which] return circle end -- RandomVector()-- Generate a unit-length vector pointing in a random direction function RandomVector() local x,y x = Rand01() y = Rand01() x = -1 + (x*2) y = -1 + (y*2) local len = sqrt((x*x)+(y*y)) x = x/len y = y/len return x,y end function CirclesCollide(circle1, circle2) local dist = (circle1.x-circle2.x)*(circle1.x-circle2.x) + (circle1.y-circle2.y)*(circle1.y-circle2.y) local max = ((circle1.radius-1)+(circle2.radius-1))*((circle1.radius-1)+(circle2.radius-2)) if dist ExtentRadius then ExtentRadius = dist end end end end -- SeedFractal() -- Seed the fractal with an initial circle (at 0,0) and calculate ExtentRadius function SeedFractal() local radius = RandRange(MinRadius, MaxRadius) local c = {} c.x = 0 c.y = 0 c.radius = radius ExtentRadius = radius CircleList:append(c) end -- AccreteCircle() -- Spawn a random circle, pick a random circle already in the list, and propagate the circle in 1-unit increments until -- it hits the fractal function AccreteCircle() local c1 = SpawnRandomCircle() local c2 = SelectCircle() local vx, vy = (c2.x-c1.x), (c2.y-c1.y) local len = sqrt( (vx*vx)+(vy*vy) ) vx = vx/len vy = vy/len local collide = CircleCollidesWithList(c1) while collide==nil do c1.x = c1.x + vx c1.y = c1.y + vy collide = CircleCollidesWithList(c1) end c1.x = c1.x + vx c1.y = c1.y + vy InsertCircle(c1) end -- Build field -- Iterate through the list of circles and build the field function BuildField() local num = CircleList:len() print("Number of circles: "..num) ClearFieldMap(MW, MH) for i=1,num,1 do local c = CircleList[i] --print("Check") MetaCircle(Int(c.x+(MW/2)), Int(c.y+(MH/2)), c.radius) -- Add 1/2 map dimensions to shift fractal to center of map end end