PicoTurtle Java program to draw a groovy tunnel

I’ve also added Java support to picoturtle. Here’s a program to draw a rather groovy tunnel. Java code follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import in.abhishekmishra.picoturtle.Turtle;
import in.abhishekmishra.picoturtle.TurtleState;

public class Tunnel
{
public static void cuboid(Turtle t, double b, double w, double h) {
t.forward(b);

t.right(-45);
t.forward(h);
t.back(h);
t.left(-45);

t.left(90);
t.forward(w);

t.left(-45);
t.forward(h);
t.back(h);
t.right(-45);

t.left(90);
t.forward(b);

t.left(-135);
t.forward(h);
t.back(h);
t.right(-135);

t.left(90);
t.forward(w);

t.right(-135);
t.forward(h);
t.left(-135);

t.left(90);
t.forward(b);
t.left(90);
t.forward(w);
t.left(90);
t.forward(b);
t.left(90);
t.forward(w);
t.pendown();
}

public static void main(String[] args)
{
// Create the turtle before using
Turtle t = Turtle.CreateTurtle(args);

if (t != null) {
// Your code goes here

t.penup();
t.setx(300);
t.sety(200);
t.heading(100);
TurtleState ts = t.state();

for (double i = 1; i < 50; i+=1.5) {
double b = i*8, w = i*8, h = i/2;
t.right(90);
t.back(b/2);

t.left(90);
t.back(w/2);

t.heading(i*0.7);
t.pendown();
t.penwidth(1);
t.pencolour((int)(4*i), (int)(255 - 3*i), (int)(5*i));
cuboid(t, b, w, h);

t.penup();
t.heading(ts.angle);
t.setx(ts.location.x);
t.sety(ts.location.y);
}

// Your code ends here

// Always stop the turtle
t.stop();
}
else {
System.out.println("Error: Unable to create a turtle.");
System.exit(-1);
}
}
}

PicoTurtle C# program to draw a wave

Recently I added C# .NET support to picoturtle. I was playing around generating waves… here’s an example based on the sine function. And the C# Turtle code follows below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using picoturtle;

namespace cspico
{
class MainClass
{
public static void Main(string[] args)
{
// Create the turtle before using
Turtle t = picoturtle.Turtle.CreateTurtle(args);

if (t != null) {
// Your code goes here

t.penup();
t.setx(0);
t.sety(250);

for(double i = 0; i < 500; i+=0.25) {
t.setx(i);
double sin = Math.Sin(i/15);
double y = sin * 100;
t.forward(y);
t.pendown();
int col = Math.Abs((int)(sin * 255));
t.pencolour(
128 + col/3,
col,
255 - col/2
);
double pw = 40 * sin;
if (pw > 0) {
pw += 5;
} else {
pw -= 5;
}
t.forward(pw);
t.penup();
t.back(y + pw);
}

t.penup();
t.setx(0);
t.sety(250);
for(double i = 0; i < 500; i+=0.25) {
t.setx(i);
double sin = -Math.Sin(i/15);
double y = sin * 100;
t.forward(y);
t.pendown();
int col = Math.Abs((int)(sin * 255));
t.pencolour(
128 + col/3,
col,
255 - col/2
);
double pw = 40 * sin;
if (pw > 0) {
pw += 5;
} else {
pw -= 5;
}
t.forward(pw);
t.penup();
t.back(y + pw);
}

// Your code ends here

// Always stop the turtle
t.stop();
}
else {
Console.Error.WriteLine("Error: Unable to create a turtle.");
Environment.Exit(-1);
}
}
}
}

PicoTurtle released

This week I built the first release of PicoTurtle - a turtle program built on REST/HTTP APIs, allowing programs written in potentially any programming language to draw turtle graphics with it. The desktop editor is built using electron. Download it at PicoTurtle Homepage.

I’ll make follow-up posts about how the program works, but here’s a dragon curve drawn using picoturtle…

And here’s the code… (based on the entry on dragon curve in wikipedia at https://en.wikipedia.org/wiki/Dragon_curve#Code)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from picoturtle import *
create_turtle()

### Your code goes here ###

def dragonCurve(order, length):
right(order * 45)
dragonCurveRecursive(order, length, 1)

def dragonCurveRecursive(order, length, sign):
if order == 0:
forward(length)
else:
rootHalf = (1 / 2) ** (1 / 2)
dragonCurveRecursive(order - 1, length * rootHalf, 1)
right(sign * -90)
dragonCurveRecursive(order - 1, length * rootHalf, -1)

pencolour(255, 0, 0)
penwidth(1.5)
back(150)
pendown()
dragonCurve(11, 300)

### Your code ends here ###

### Always stop the turtle
stop()

VS Code Extension - timed-themes

VS Code is now my primary IDE, it’s the IDE I use the most and really like. At home I use a laptop hooked to a large monitor. This setup is in a room with lots of windows and skylights. So during the day, the room lights up a lot and there are a lot of reflections on the screen. So I use a dark theme with lots of contrast during the day. But at night I use a lighter theme with less contrast. I used to switch manually with a two lines in my settings for the IDE theme which I would switch on or off.

This looked like something which could be easily automated and fun to do. And I wrote this extension called timed-themes which does just that. You specify two themes, and time ranges for the two themes in the configuration. The extension takes care of changing the theme at the appropriate times.

Here’s the url to the marketplace -> timed-themes

Install gcloud-sdk and Docker on Ubuntu 16.04 on VirtualBox

I use Windows 10 Home on my laptop, so docker integration is a challenge, espescially running all the builds written in shell scripts won’t run nicely on Ubuntu on Windows or MSYS2.

So I’ve had to fallback on installing ubuntu on virtualbox, with docker and gcloud-sdk. I’ve documented the steps below.

Get VirtualBox

Download VirtualBox

Install Ubuntu on VirtualBox

Get Xubuntu 16.04 ISO. Xubuntu is lightweight and provides a standard desktop. Ubuntu or other flavours should also work.

Download Xubuntu LTS

VirtualBox Guest Config

Config suggested for guest:

  1. Min Disk: 30GB
  2. RAM: 2GB
  3. LVM Partitioning
  4. Xubuntu desktop installer

Post-install

Enable shared clipboard Host-Guest and Reboot

Devices->Shared Clipboard->Host to Guest

Install VirtualBox Guest Tools

  1. Insert Guest Additions ISO
  2. Run /media/am/VBox_…/autorun.sh
1
2
cd /media/am/VBox_.../
sudo ./autorun.sh

Update and Upgrade

1
2
sudo apt-get update -y
sudo apt-get upgrade -y

Install aptitude, vim, emacs etc.

1
sudo apt-get install aptitude vim emacs -y

Install Docker

Install instructions are here Installl Docker CE on Ubuntu

Run docker as user

Add current user to docker group, and verify that it works

1
sudo usermod -aG docker $USER

Reboot ubuntu for changes to take effect.

1
sudo reboot

Verify that the changes work

1
docker run hello-world

See Post-install step for linux for further details.

(Optional) Install Docker Compose

See instructions at Install Docker Compose for Linux

Install gcloud-sdk

Install instructions are here Install gcloud-sdk on Ubuntu

SSH/github setup

Use this gist to setup multiple keys if required.
Github multiple ssh keys

(Optional) Install nodejs using package manager

1
2
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs

For details see instructions at Install nodejs via package manager

Copy the VM before using it

Take a backup of the VM folder if you have space, so that you don’t need to go through these steps again. You can just start from a clean copy where all tools are setup.

Exploring turtle graphics

This week I started exploring turtle graphics with my son as he is very interested in drawing and crafts (he calls his room “art room”). We wrote one program together in python’s turtle graphics library. It’s here - face.py , and this is what it looks like :)

So this got me reading and exploring a bit more about turtle graphics, especially since the tools and language python were not really easy enough to use for my son. So I decided to create my own turtle environment (currently I’m thinking about doing this in electron so that it’s available on all platforms - I use windows, linux and mac at home).

Here’s a list of turtle resources I compiled - Turtle Graphics Resources

More on this soon.

A GCD Game!

Recently I’ve again started reading Dijkstra’s book “A Discipline Of Programming”. In the very first chapter “Chapter 0 on executional abstraction”, he mentions a board game which when played correctly produces the GCD of two numbers. It is just an implementation of Euclid’s Algorithm. I wanted to write a program to play this game.

I’d also discovered a library to produce svg animations called vivus, which I wanted to try.

So here’s my implementation of the GCD game in html/javascript - GCD Board Game. Note that the numbers are provided using URL params to the html (x, and y are the param names.)