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);
}
}
}
}