using bresenham algorithm for circles to make it more efficient

This commit is contained in:
Ghoulboy78 committed 2021-02-24 12:06:17 +01:00
1 parent fa75401278
commit 467767a8e7
1 file changed
+127 -24
@@ -1,9 +1,9 @@
//a library app, mostly for the draw command, which allows to import these shape-drawing functions into your own app. //This is a library app, mostly for the draw command, which allows to import these shape-drawing functions into your own app.
//Arguments for each are to be given as a singleton list, for ease of use. This is with the exception of fill_flat, with //Arguments for each are to be given as a singleton list, for ease of use. This is with the exception of the fill_flat functions, with
//the assumption that it is unlikely to be used outside of internal calls, but it is still available to be called if necessary. //the assumption that they're unlikely to be used outside of internal calls, but it is still available to be called if necessary.
//Make sure you read the arguments and input them in the correct order, or it will mess up. //Make sure you read the arguments and input them in the correct order, or it will mess up.
//these functions return a set of all the positions in the shape, given the parameters, without repeating a position. //these functions return a set of all the positions in the shape, given the parameters, without repeating a position.
//This way, you can efficiently iterate over the positions once having generated them. See draw_beta.sc for an example. //This way, you can efficiently iterate over the positions once having generated them. See draw_beta.sc for an example. todo change to draw once renamed
draw_sphere(args)->( draw_sphere(args)->(
@@ -76,34 +76,137 @@ draw_prism(args)->(
return(positions) return(positions)
); );
fill_flat(pos, offset, dr, rectangle, orientation, hollow)->( fill_flat(pos, offset, dr, rectangle, orientation, hollow)->
if(rectangle,
fill_flat_rectangle(pos, offset, dr, orientation, hollow),
fill_flat_circle(pos, offset, dr, orientation, hollow)
);
fill_flat_circle(pos, offset, dr, orientation, hollow) ->( //Bresenham circle algorithm to be super efficient
r = floor(dr); r = floor(dr);
drsq = dr^2; a=0;
b=r;
d=3-(2*r);
positions = {}; positions = {};
if(orientation=='x',
scan(pos,0,-r,-r, positions += fill_flat_circle_points(pos, orientation, offset, hollow, a, b);
if((!hollow && (rectangle || _y^2 + _z^2 <= drsq))||//if not hollow, vry simple
(hollow && ((rectangle && (abs(_y) == r || abs(_z) ==r)) || //If hollow and it's a rectangle while(a<=b, 10^10,
(!rectangle && (_y^2 + _z^2 <= drsq && (abs(_y)+1)^ 2 + (abs(_z)+1)^2 >= drsq)))),//If hollow and not rectangle if(d<=0,
positions += [_x+offset,_y,_z] d += (4*a)+6,
d += (4*a)-(4*b)+10;
b=b-1
);
a+=1;
positions += fill_flat_circle_points(pos, orientation, offset, hollow, a, b)
);
return(positions)
);
fill_flat_circle_points(pos, orientation, offset, hollow, a, b) ->(
[x, y, z] = pos;
positions = {};
if(orientation == 'x',
if(hollow,
positions += [x + offset, a+y, b+z];
positions += [x + offset, a+y,-b+z];
positions += [x + offset,-a+y,-b+z];
positions += [x + offset,-a+y, b+z];
positions += [x + offset, b+y, a+z];
positions += [x + offset, b+y,-a+z];
positions += [x + offset,-b+y,-a+z];
positions += [x + offset,-b+y, a+z],
c_for(c = -a, c<=a, c+=1,
positions += [x + offset,c+y, b + z];
positions += [x + offset,c+y,-b + z]
);
c_for(c = -b, c<=b, c+=1,
positions += [x + offset, a+y,c + z];
positions += [x + offset,-a+y,c + z]
) )
), ),
orientation == 'y', orientation=='y',
scan(pos,-r,0,-r, if(hollow,
if((!hollow && (rectangle || _x^2 + _z^2 <= drsq))||//if not hollow, vry simple positions += [ a+x, y + offset, b+z];
(hollow && ((rectangle && (abs(_x) == r || abs(_z) ==r)) || //If hollow and it's a rectangle positions += [ a+x, y + offset,-b+z];
(!rectangle && (_x^2 + _z^2 <= drsq && (abs(_x)+1)^ 2 + (abs(_z)+1)^2 >= drsq)))),//If hollow and not rectangle positions += [-a+x, y + offset,-b+z];
positions += [_x,_y+offset,_z] positions += [-a+x, y + offset, b+z];
positions += [ b+x, y + offset, a+z];
positions += [ b+x, y + offset,-a+z];
positions += [-b+x, y + offset,-a+z];
positions += [-b+x, y + offset, a+z],
c_for(c = -a, c<=a, c+=1,
positions += [c+x,y + offset, b + z];
positions += [c+x,y + offset,-b + z]
);
c_for(c = -b, c<=b, c+=1,
positions += [ a+x,y + offset,c + z];
positions += [-a+x,y + offset,c + z]
) )
), ),
orientation == 'z', orientation=='z',
scan(pos,-r,-r,0, if(hollow,
if((!hollow && (rectangle || _y^2 + _x^2 <= drsq))||//if not hollow, vry simple positions += [ a+x, b+y, z + offset];
(hollow && ((rectangle && (abs(_y) == r || abs(_x) ==r)) || //If hollow and it's a rectangle positions += [ a+x,-b+y, z + offset];
(!rectangle && (_y^2 + _x^2 <= drsq && (abs(_y)+1)^ 2 + (abs(_x)+1)^2 >= drsq)))),//If hollow and not rectangle positions += [-a+x,-b+y, z + offset];
positions += [_x,_y,_z+offset] positions += [-a+x, b+y, z + offset];
positions += [ b+x, a+y, z + offset];
positions += [ b+x,-a+y, z + offset];
positions += [-b+x,-a+y, z + offset];
positions += [-b+x, a+y, z + offset],
c_for(c = -a, c<=a, c+=1,
positions += [c+x, b + y,z + offset];
positions += [c+x,-b + y,z + offset]
);
c_for(c = -b, c<=b, c+=1,
positions += [ a+x,c + y,z + offset];
positions += [-a+x,c + y,z + offset]
) )
) )
)
);
fill_flat_rectangle(pos, offset, dr, orientation, hollow) -> (
r = floor(dr);
drsq = dr^2;
positions = {};
if(orientation == 'x',
c_for(a = -r, a <=r, a +=1,
if(hollow,
positions += [r + offset, a, r];
positions += [r + offset, a,-r];
positions += [r + offset, r, a];
positions += [r + offset,-r, a],
c_for(b = -r, b <=r, b +=1,
positions += [r + offset, a, b]
)
)
),
orientation == 'y',
c_for(a = -r, a <=r, a +=1,
if(hollow,
positions += [ r, r + offset,a];
positions += [-r, r + offset,a];
positions += [a, r + offset, r];
positions += [a, r + offset,-r],
c_for(b = -r, b <=r, b +=1,
positions += [a, r + offset, b]
)
)
),
orientation == 'z',
c_for(a = -r, a <=r, a +=1,
if(hollow,
positions += [ r, a,r + offset];
positions += [-r, a,r + offset];
positions += [a, r, r + offset];
positions += [a,-r, r + offset],
c_for(b = -r, b <=r, b +=1,
positions += [b, a, r + offset]
)
)
),
); );
return(positions) return(positions)
); );