I'm new in pixilang coding.
I start coding half an hour ago
I like Arduino, Polargraph, Pyrograph and other machines which converts my thoughts into the picture.
Now I found Pixilang for my coding.
Here's great tool for Processing lang which converts image to lines. ( Death to sharpie )
http://www.dullbits.com/drawbot/drawbot
I rewrite this source to python but is really slooow
now i trying to rewrite in Pixilang for speed comparsion but my skills are poor.
find someone who would help me with the program and explained my mistakes in program ?
thanks.
Code: Select all
drawing_scale = 1.0
adjustbrightness = 8.0
half_radius = 3.0
darkest_x = 0
darkest_y = 0
squiggle_count = 0
squiggle_length = 100
xoffset = 0
yoffset = 0
image_width = 800
image_height = 600
fn math_max($min, $max)
{
if($min < $max) {ret($max)}
if($min > $max) {ret($min)}
if($min == $max) {ret($max)}
}
fn math_min($min, $max)
{
if($min < $max) {ret($min)}
if($min > $max) {ret($max)}
if($min == $max) {ret($min)}
}
fn math_sqrt($val)
{
$rsrt = $val
$div = $val
if $val <= 0 {ret}
while(1)
{
$div = ($val / $div + $div) / 2
if($rslt) > $div {$rslt = $div}
else {ret}
}
ret($rslt)
}
fn Constrain($value, $min, $max)
{
ret(math_max(math_min($max, $value), $min))
}
fn FindDarkest()
{
$darkest_value = 256
$x = half_radius
$y = half_radius
while($x < (image_width - half_radius))
{
while($y < (image_height - half_radius))
{
$loc = $x + $y * image_width
$point = image[$loc]
$r = get_red($point)
// printf("Red: %u\n", $r) // WORKING
if($r < $darkest_value)
{
darkest_x = $x
darkest_y = $y
$darkest_value = $r
}
$y + 1
}
$x + 1
}
}
fn FindDarkestNeighbor($start_x, $start_y)
{
$darkest_neighbor = 256
$min_x = Constrain($start_x - half_radius, half_radius, image_width - half_radius)
$min_y = Constrain($start_y - half_radius, half_radius, image_height - half_radius)
$max_x = Constrain($start_x + half_radius, half_radius, image_width - half_radius)
$max_y = Constrain($start_y + half_radius, half_radius, image_height - half_radius)
$x = $min_x
$y = $min_y
while($x <= $max_x)
{
while($y <= $max_y)
{
$loc = $x + $y * image_width
$d = math_sqrt(($x - $start_x)*($x - $start_x) + ($y - $start_y)*($y - $start_y))
if(d <= half_radius)
{
$point = image[$loc]
$r = get_red($point) + rand(0.01)
if($r < $darkest_neighbor)
{
darkest_x = $x
darkest_y = $y
$darkest_neighbor = $r
}
}
$y + 1
}
$x + 1
}
}
fn LightenOnePixel($x, $y)
{
$loc = $y * image_width + x
$point = image[$loc]
$r = get_red($point)
$r = $r + adjustbrightness
$r = Constrain($r, 0, 255)
red[$loc] = get_color($r, 0, 0)
}
fn Lighten($start_x, $start_y)
{
$min_x = Constrain($start_x - half_radius, half_radius, image_width - half_radius)
$min_y = Constrain($start_y - half_radius, half_radius, image_height - half_radius)
$max_x = Constrain($start_x + half_radius, half_radius, image_width - half_radius)
$max_y = Constrain($start_y + half_radius, half_radius, image_height - half_radius)
$x = 0
$y = 0
while($x <= $max_x)
{
while($y <= $max_y)
{
$d = math_sqrt(($x*$x - $start_x*$start_x) + ($y*$y - $start_y*$start_y))
if($d <= half_radius)
{
$loc = $y * image_width + x
$point = image[$loc]
$r = get_red($point)
$r = $r + adjustbrightness / $d
$r = Constrain($r, 0, 255)
red[$loc] = get_color($r, 0, 0)
}
$y + 1
}
$x + 1
}
LightenOnePixel(adjustbrightness * 6, $start_x, $start_y)
LightenOnePixel(adjustbrightness * 2, $start_x + 1, $start_y)
LightenOnePixel(adjustbrightness * 2, $start_x - 1, $start_y)
LightenOnePixel(adjustbrightness * 2, $start_x, $start_y + 1)
LightenOnePixel(adjustbrightness * 2, $start_x, $start_y - 1)
LightenOnePixel(adjustbrightness * 1, $start_x + 1, $start_y + 1)
LightenOnePixel(adjustbrightness * 1, $start_x - 1, $start_y - 1)
LightenOnePixel(adjustbrightness * 1, $start_x - 1, $start_y + 1)
LightenOnePixel(adjustbrightness * 1, $start_x + 1, $start_y - 1)
}
fn RandomDarknessWalk()
{
FindDarkest()
$x = darkest_x
$y = darkest_y
squiggle_count + 1
FindDarkestNeighbor($x, $y)
$newx = darkest_x * drawing_scale + xoffset
$newy = darkest_y * drawing_scale + yoffset
printf("%u: %u\n", darkest_x, darkest_y)
line($oldx, $oldy, $newx, $newy, WHITE)
$s = 0
while($s < squiggle_length)
{
FindDarkestNeighbor($x, $y)
Lighten(darkest_x, darkest_y)
$newx = darkest_x * drawing_scale + xoffset
$newy = darkest_y * drawing_scale + yoffset
line($oldx, $oldy, $newx, $newy, WHITE)
$x = darkest_x
$y = darkest_y
$oldx = $newx
$oldy = $newy
//printf("%u, %u\n", $newx, $newy)
$s + 1
}
}
start:
image = load("./1.jpg")
red = new(get_xsize(image), get_ysize(image), PIXEL)
image_width = get_xsize(image)
image_height = get_ysize(image)
size = image_width * image_height
start_timer(0)
t = get_timer(0) / 1000
offsetx = 0 //image_width / 2
offsety = 0
while 1
{
RandomDarknessWalk()
frame()
//printf("%u\n", squiggle_count)
if(squiggle_count >= squiggle_length)
{
//printf("end in %u min.\n", image_width)
save(red,"./pokus.png",FORMAT_PNG)
//while( get_event() ) { if EVT[ EVT_TYPE ] == EVT_QUIT { halt } }
halt
}
}
go start