Update fixed the line drawing routine the other day... but broke it more. Fixed the broken now, released new version. As an added bonus, have some dancing lines (logo api not required. "r" for new random lines, any other key except escape exits. Run on monitor with "monitor side dancinglines")
An interactive interpreter for the Logo turtle graphics language. Automatic monitor support, subroutines implemented with "to" and variables with "let."
Adapts to whatever the screen size is; uses 4 lines of the computer console for displaying the command center, but automatically detects an attached monitor and duplicates drawing there without the command center. Big monitors recommended, as the tiny dimensions of the computer's screen won't let you see very much.
Current Logo commands/operators:
Spoiler
Implemented commands
forward <dist> / fd <dist> - moves turtle forward <dist> units
left <angle> / lt <angle> - turns left <angle> degrees
right <angle> / rt <angle> - turns right <angle> degrees
penup / pu - picks the pen up, so movement won't draw
pendown / pd - puts the pen down so movement draws
penchar <char> / pc <char> - sets the character drawn by the pen
clean - erases the current drawing
home - returns the turtle to 0,0 angle 0
wait <time> - pauses execution for <time> seconds
repeat <count> [instructions...]
let [name1 value1 name2 value2 ... nameN valueN] - for variable assignment
Implemented Operators:
sum <a> <b> - addition
dif </b></a><b><a> <b> - subtraction
mul </b></a><b><a> <b> - multiplication
div </b></a><b><a> <b> - division
sqrt <val> returns square root of <val>
type "quit" to exit the interpreter.
A sample program:
to spiral :startDist :distStep :angleStep :count
let [d :startDist]
repeat :count [fd :D/>/> rt :angleStep let [d sum :)/>/> :diststep]]
end
spiral 1 .1 15 300
and here's a screenshot of the output of that program to an 8x6 monitor with setFontScale(.5)
Spoiler
Video of it in action, using the freshly-implemented clear, home, and wait commands to do animation! Hypnotic Spirals
Planned features:
Spoiler
more logo commands - ex. make, freeze, unfreeze, setposx, setposy,
user input/output commands
infix operators - ex, 1+1 instead of sum 1 1
lists (hard-coded for repeat and let currently)
if statement and conditional operators
saved programs
edit command, to launch an editor to create or edit a saved program
run command, to load and run a saved program
ability to run saved programs directly from shell, ex. "logo myLogoProgram"
Wishlist features
Compiler - convert saved logo programs into lua programs
Multiple turtles - will require rewriting to event-based model, fairly radical overhaul
full lua integration - lua command to execute lua code in logo programs, and moving logo command interpreter to logo api to allow executing logo commands from lua.
it depends on functions in a separate logo api, which it expects to be called simply logo. The api can be used independently of the logo interpreter program to do turtle-like vector drawing.
Installation note: logo interpreter expects logo API to be named "logo" and either be loaded already or in the current directory.
Version history:
Spoiler
logo api
v0.3.2 - http://pastebin.com/8jYYn667
fixed rather serious bug introduced in 0.3.1 that caused roughly half of lines to incorrectly draw as straight horizontal/vertical lines... />
v0.3.1 - http://pastebin.com/i4bCF3aM
replaced rough initial line algorithm with improved version based on bresenham's algorithm
v0.3 - http://pastebin.com/9QPphMV3
added home, clean
v0.2 - http://pastebin.com/RsT0tjzz
added support for multiple displays, drawing to regions of screens
v0.1 -
first version. Had forward(), moveTo(), turn(), turnTo(), penUp, penDown, and the line() function.
logo interpreter
v0.1 - http://pastebin.com/z7WT1wnY
first version, interpreter interface, monitor support, basic turtle commands and operators, repeat, let. Req. logo api v0.2 or later.
v0.2 -http://pastebin.com/x45p4kbX
added home, clean, and "to" command to define custom methods. Req. logo API 0.3 or later.
Just a minor update, replaced my initial late-night hacked-from-memory line drawing function with a better version implementing Bresenham's algorithm, so there are no longer large errors accumulated in long lines, they start and end where you would expect.
Not able to fix it until later tonight but just noticed there is a major bug in the "improved" line renderer causing many lines to be drawn very wrong. Embarrassing, this. Just ignore API 0.3.1 for now and use 0.3 if you check this out before I fix it.
Also did some tests in SMP and found the performance somewhat lacking; didn't seem to create lag or anything, but it wasn't receiving screen updates nearly as quickly as they were happening. Very disappointing, will do more testing later and see if there's anything I can do to improve it.
The screenshot is output of the sample program above it, actually, but the code's in logo, so it won't help you much with the math of spirals />
GopherAtl, on 09 September 2012 - 12:52 AM, said:
to spiral :startDist :distStep :angleStep :count
let [d :startDist]
repeat :count [fd :)/>/> rt :angleStep let [d sum :P/>/> :diststep]]
end
spiral 1 .1 15 300
in lua, using the logo API directly, this would be something like..
--[[ params:
startDist - the distance to move forward the first time
distStep - the amount to increase the distance by each time
angleStep - the angle, in degrees, to turn after each move
count - how many times to repeat
--]]
function spiral(startDist, distStep, angleStep, count)
--current distance
local dist=startDist
--repeat count times
for i=1,count do
--move forward dist, drawing line behind you
logo.forward(dist)
--turn angleStep degrees to the right
logo.turn(angleStep)
--increase dist by distStep
dist=dist+distStep
end
end
Not really much math in there, as you can see. Turtle graphics are very simple like that, heh.