Basic forward and turn commands (LOGO).

ID: 1409

				
					using uCalcSoftware;

var uc = new uCalc();

static void MoveTurtle(uCalc.Callback cb) {
   var dist = cb.Arg(1);
   var uc_inst = cb.uCalc;
   var x = uc_inst.ItemOf("x").Value();
   var y = uc_inst.ItemOf("y").Value();
   var angle = uc_inst.ItemOf("angle").Value();
   var pen_down = uc_inst.ItemOf("pen_down").ValueBool();
   int new_x = Convert.ToInt32(x + dist * uc_inst.Eval("CosD(" + (angle).ToString() + ")"));
   int new_y = Convert.ToInt32(y + dist * uc_inst.Eval("SinD(" + (angle).ToString() + ")"));
   if (pen_down) {
      Console.WriteLine($"Drawing line to ({new_x},{new_y})");
   } else {
      Console.WriteLine($"Moving to ({new_x},{new_y})");
   }
   uc_inst.ItemOf("x").Value(new_x);
   uc_inst.ItemOf("y").Value(new_y);
}


// --- Setup ---
uc.DefineVariable("x = 0.0");
uc.DefineVariable("y = 0.0");
uc.DefineVariable("angle = 90.0");
uc.DefineVariable("pen_down = false");
uc.DefineConstant("PI = 3.1415926535");
uc.DefineFunction("CosD(a) = Cos(a * PI / 180)");
uc.DefineFunction("SinD(a) = Sin(a * PI / 180)");
uc.DefineFunction("Move(dist)", MoveTurtle);

var t = uc.ExpressionTransformer;
t.FromTo("FD {@Number:dist}", "Move({dist})");
t.FromTo("RT {@Number:deg}", "angle = angle - {deg}");
t.FromTo("PD", "pen_down = true");

// --- Script ---
var script = """

PD
FD 100
RT 90
FD 50

""";

uc.EvalStr(script);

Console.WriteLine($"Final Position: ({uc.Eval("x")}, {uc.Eval("y")})");
				
			
Drawing line to (0,100)
Drawing line to (50,100)
Final Position: (50, 100)
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

void ucalc_call MoveTurtle(uCalcBase::Callback cb) {
   auto dist = cb.Arg(1);
   auto uc_inst = cb.uCalc();
   auto x = uc_inst.ItemOf("x").Value();
   auto y = uc_inst.ItemOf("y").Value();
   auto angle = uc_inst.ItemOf("angle").Value();
   auto pen_down = uc_inst.ItemOf("pen_down").ValueBool();
   int new_x = x + dist * uc_inst.Eval("CosD(" + to_string(angle) + ")");
   int new_y = y + dist * uc_inst.Eval("SinD(" + to_string(angle) + ")");
   if (pen_down) {
      cout << "Drawing line to (" << new_x << "," << new_y << ")" << endl;
   } else {
      cout << "Moving to (" << new_x << "," << new_y << ")" << endl;
   }
   uc_inst.ItemOf("x").Value(new_x);
   uc_inst.ItemOf("y").Value(new_y);
}

int main() {
   uCalc uc;
   // --- Setup ---
   uc.DefineVariable("x = 0.0");
   uc.DefineVariable("y = 0.0");
   uc.DefineVariable("angle = 90.0");
   uc.DefineVariable("pen_down = false");
   uc.DefineConstant("PI = 3.1415926535");
   uc.DefineFunction("CosD(a) = Cos(a * PI / 180)");
   uc.DefineFunction("SinD(a) = Sin(a * PI / 180)");
   uc.DefineFunction("Move(dist)", MoveTurtle);

   auto t = uc.ExpressionTransformer();
   t.FromTo("FD {@Number:dist}", "Move({dist})");
   t.FromTo("RT {@Number:deg}", "angle = angle - {deg}");
   t.FromTo("PD", "pen_down = true");

   // --- Script ---
   auto script = R"(
PD
FD 100
RT 90
FD 50
)";

   uc.EvalStr(script);

   cout << "Final Position: (" << uc.Eval("x") << ", " << uc.Eval("y") << ")" << endl;
}
				
			
Drawing line to (0,100)
Drawing line to (50,100)
Final Position: (50, 100)
				
					Imports System
Imports uCalcSoftware
Public Module Program
   
   Public Sub MoveTurtle(ByVal cb As uCalc.Callback)
      Dim dist = cb.Arg(1)
      Dim uc_inst = cb.uCalc
      Dim x = uc_inst.ItemOf("x").Value()
      Dim y = uc_inst.ItemOf("y").Value()
      Dim angle = uc_inst.ItemOf("angle").Value()
      Dim pen_down = uc_inst.ItemOf("pen_down").ValueBool()
      Dim new_x As Integer = Convert.ToInt32(x + dist * uc_inst.Eval("CosD(" + (angle).ToString() + ")"))
      Dim new_y As Integer = Convert.ToInt32(y + dist * uc_inst.Eval("SinD(" + (angle).ToString() + ")"))
      If pen_down Then
         Console.WriteLine($"Drawing line to ({new_x},{new_y})")
      Else
         Console.WriteLine($"Moving to ({new_x},{new_y})")
      End If
      uc_inst.ItemOf("x").Value(new_x)
      uc_inst.ItemOf("y").Value(new_y)
   End Sub
   
   Public Sub Main()
      Dim uc As New uCalc()
      '// --- Setup ---
      uc.DefineVariable("x = 0.0")
      uc.DefineVariable("y = 0.0")
      uc.DefineVariable("angle = 90.0")
      uc.DefineVariable("pen_down = false")
      uc.DefineConstant("PI = 3.1415926535")
      uc.DefineFunction("CosD(a) = Cos(a * PI / 180)")
      uc.DefineFunction("SinD(a) = Sin(a * PI / 180)")
      uc.DefineFunction("Move(dist)", AddressOf MoveTurtle)
      
      Dim t = uc.ExpressionTransformer
      t.FromTo("FD {@Number:dist}", "Move({dist})")
      t.FromTo("RT {@Number:deg}", "angle = angle - {deg}")
      t.FromTo("PD", "pen_down = true")
      
      '// --- Script ---
      Dim script = "
PD
FD 100
RT 90
FD 50
"
      
      uc.EvalStr(script)
      
      Console.WriteLine($"Final Position: ({uc.Eval("x")}, {uc.Eval("y")})")
   End Sub
End Module
				
			
Drawing line to (0,100)
Drawing line to (50,100)
Final Position: (50, 100)