Skip to content

Instantly share code, notes, and snippets.

@lobingera
Created June 21, 2015 15:53
Show Gist options
  • Save lobingera/4fd1532b4eca3158fd9e to your computer and use it in GitHub Desktop.
Save lobingera/4fd1532b4eca3158fd9e to your computer and use it in GitHub Desktop.
really basic painting into an array (as bitmap)
# from
# http://stackoverflow.com/questions/1201200/fast-algorithm-for-drawing-filled-circles#1201304
#
# void DrawFilledCircle(int x0, int y0, int radius)
# {
# int x = radius;
# int y = 0;
# int xChange = 1 - (radius << 1);
# int yChange = 0;
# int radiusError = 0;
# while (x >= y)
# {
# for (int i = x0 - x; i <= x0 + x; i++)
# {
# SetPixel(i, y0 + y);
# SetPixel(i, y0 - y);
# }
# for (int i = x0 - y; i <= x0 + y; i++)
# {
# SetPixel(i, y0 + x);
# SetPixel(i, y0 - x);
# }
# y++;
# radiusError += yChange;
# yChange += 2;
# if (((radiusError << 1) + xChange) > 0)
# {
# x--;
# radiusError += xChange;
# xChange += 2;
# }
# }
# }
function drawFilledCircle!(m::Array{Uint32,2},x0::Int,y0::Int,radius::Int,c::Int)
x::Int = radius
y::Int = 0
xChange::Int = 1 - (radius << 1)
yChange::Int = 0;
radiusError::Int = 0
while x >= y
for i = (x0-x):(x0+x)
m[y0+y,i] = c
m[y0-y,i] = c
end
for i = (x0-y):(x0+y)
m[y0+x,i] = c
m[y0-x,i] = c
end
y +=1
radiusError += yChange;
yChange += 2
if ((radiusError << 1) + xChange) > 0
x -= 1
radiusError += xChange
xChange += 2
end
end
end
function random_dots(n::Int,yrange::Int,xrange::Int,radius::Int)
x = int(rand(n)*xrange)
y = int(rand(n)*yrange)
# clamping the coordinates, so no boundary processing in painting
for i = 1:length(x)
x[i] = clamp(x[i],radius+2,xrange-radius-2);
y[i] = clamp(y[i],radius+2,yrange-radius-2);
end
c = int(rand(length(x))*100) # random color
x,y,c
end
function Lpaint(x::Array{Int,1},y::Array{Int,1},c::Array{Int,1},m::Array{Uint32,2},radius::Int)
for i = 1:length(x)
drawFilledCircle!(m,x[i],y[i],radius,c[i])
end
end
m = zeros(Uint32,800,1000); # out bitmap, y,x
n_dots = [30,100,300,1000,3000,10000,30000,100000,300000,1000000,3000000,10000000]
x,y,c = random_dots(maximum(n_dots),800,1000,5)
for n in n_dots
display(n)
@time Lpaint(x[1:n],y[1:n],c[1:n],m,5)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment