Need help with code.

Pixilang programming language
Post Reply
martblek
Posts: 6
Joined: Fri Dec 09, 2016 9:42 pm

Need help with code.

Post by martblek » Fri Dec 09, 2016 10:05 pm

Hi all,
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


martblek
Posts: 6
Joined: Fri Dec 09, 2016 9:42 pm

Re: Need help with code.

Post by martblek » Sun Dec 11, 2016 4:03 pm

Some progress here.
I need learn how to work with colors and alpha channel.
But some results is here:

Code: Select all

fn rand_range($a, $b)
{
	//print("randrange")
	ret($a + rand() % ($b - $a))
}

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 Constrain($value, $min, $max)
{
	//print("constrain")
	ret(math_max(math_min($max, $value), $min))
}

fn FindDarkest()
{
	//printf("%s\n", "findDarkest")
	$darkest_value = 256
	$x = half_radius
	while($x < (image_width - half_radius))
	{
		$y = half_radius
		while($y < (image_height - half_radius))
		{
			$loc = $x + $y * image_width
			$point = image[$loc]
			$r = get_red($point)
			if($r < $darkest_value)
			{
				darkest_x = $x
				darkest_y = $y
				$darkest_value = $r
			}
			$y + 1
		}
		$x + 1
	}
}


fn FindDarkestNeighbor($start_x, $start_y)
{
	//printf("%s\n", "DarkestNeighbor")
	$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
	while($x <= $max_x)
	{
		$y = $min_y
		while($y <= $max_y)
		{
			$loc = $x + $y * image_width
			//printf("X: %d, Y: %d, NewX: %d, NewY: %d\n", $x, $y, $start_x, $start_y)
			$d = sqrt(($start_x - $x)*($start_x - $x) + ($start_y - $y) * ($start_y - $y))
			if($d <= half_radius)
			{
				$point = image[$loc]
				$random = rand_range(1, 10) / 100
				$r = get_red($point) + $random
				if($r < $darkest_neighbor)
				{
					darkest_x = $x
					darkest_y = $y
					$darkest_neighbor = $r
				} 
			}
		$y + 1
		}
	$x + 1
	}
}

fn LightenOnePixel($x, $y)
{
	//printf("%s\n", "Lighten One Pixel")
	$loc = $y * image_width + $x
	$point = image[$loc]
	$r = get_red($point)
	$r = $r + adjustbrightness
	$r = Constrain($r, 0, 255)
	image[$loc] = get_color($r, 0, 0)
}


fn Lighten($start_x, $start_y)
{
	//printf("%s\n", "Lighten")
	$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
	while($x <= $max_x)
	{
		$y = $min_y
		while($y <= $max_y)
		{
			$d = sqrt(($start_x - $x) * ($start_x - $x) + ($start_y - $y) * ($start_y - $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)
				image[$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()
{
	printf("Darkest: %d\n", squiggle_count)
	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
	
	move_abs($newx, $newy)
	is_pen_down = 1

	$s = 0
	while($s < squiggle_length)
	{
		//printf("Squiggle Length: %d\n", $s)
		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, BLACK)
		move_abs($newx, $newy)
		$x = darkest_x
		$y = darkest_y
		$oldx = $newx
		$oldy = $newy

		$s + 1
	}
	is_pen_down = 0
}

fn move_abs($x, $y)
{
	coordx = $x
	coordy = $y

	if(is_pen_down == 1)
	{
		$color = get_color(0, 0, 0, 100-(squiggle_count * dry_out))
		line(oldx + centerx, oldy + centery, coordx + centerx, coordy + centery, $color)
	}
	oldx = $x
	oldy = $y
}

start:

drawing_scale = 1.0
adjustbrightness = 8.0
half_radius = 3
darkest_x = 0
darkest_y = 0
squiggle_count = 0
squiggle_length = 600
squiggle_total = 250
is_pen_down = 0
dry_out = 0.25
oldx = 0
oldy = 0

image = load("./m1.jpg")

image_width = get_xsize(image)
image_height = get_ysize(image)

draw = new(image_height, image_height, PIXEL)

xoffset = -(image_width / 2)
yoffset = -(image_height / 2)
centerx = 0
centery = 0

set_screen(draw)
clear(WHITE)
pixi(draw)

start_timer(0)

while 1
{
	RandomDarknessWalk()
	frame()
	if(squiggle_count >= squiggle_total)
	{
		save(draw,"./pokus2.png",FORMAT_PNG)
		//while( get_event() ) { if EVT[ EVT_TYPE ] == EVT_QUIT { halt } }
		printf("Time: %f sec.\n", get_timer(0) / 1000)
		halt
	}
}

go start
I need find way to work better with red channel in Lighten functions.

BTW:
How i define if value is Float or Int ?
No type define ?
or val = new(1, 1, FLOAT) ?
thanks

martblek
Posts: 6
Joined: Fri Dec 09, 2016 9:42 pm

Re: Need help with code.

Post by martblek » Mon Dec 12, 2016 1:08 am

i think i'm done :)
too much work not yet finished but here is some result.
http://martblek.tumblr.com/

User avatar
NightRadio
Site Admin
Posts: 2837
Joined: Fri Jan 23, 2004 12:28 am
Location: Ekaterinburg. Russia
Contact:

Re: Need help with code.

Post by NightRadio » Mon Dec 12, 2016 10:21 am

Hello! :)
How i define if value is Float or Int ?
No type define ?
or val = new(1, 1, FLOAT) ?
Variables have a dynamic type in Pixilang: INT32 or FLOAT32.
Examples:

Code: Select all

a = 32 //INT
a = 32.4 //FLOAT
a = 2 //INT
b = a * 2.2 //FLOAT
b = b div 1 //convert from FLOAT to INT

Post Reply