// Turtle graphics library // Installation: Copy to the macros folder and restart ImageJ // Example1: http://wsr.imagej.net/macros/TurtleGraphics.ijm // Example2: http://wsr.imagej.net/macros/TurtleGraphicsPatterns.ijm // Author: Jerome Mutterer function home() { x1=0;y1=0; toUnscaled(x1,y1); turtleLineTo(x1,y1); Property.set('turtle.x',0); Property.set('turtle.y',0); Property.set('turtle.heading',0); displayTurtle(); } function xCor() { x=Property.get('turtle.x'); return parseFloat(x); } function clean() { run('Select All'); run('Clear'); run('Select None'); } function dot(x,y) { toUnscaled(x,y); makePoint(x,y); run('Draw'); displayTurtle(); } function yCor() { y=Property.get('turtle.y'); return parseFloat(y); } function shownP() { turtleHidden=Property.get('turtle.hidden'); return !turtleHidden; } function heading() { h = Property.get('turtle.heading'); return parseFloat(h); } function pos() { x=xCor(); y=yCor(); return ''+x+','+y; } function setPos(p) { position = split(p,','); setPosXY(position[0],position[1]); displayTurtle(); } function setPosXY(x,y) { Property.set('turtle.x',x); Property.set('turtle.y',y); toUnscaled(x,y); turtleLineTo(x,y); displayTurtle(); } function setX(x) { Property.set('turtle.x',x); y=yCor(); toUnscaled(x,y); turtleLineTo(x,y); displayTurtle(); } function setY(y) { Property.set('turtle.y',y); x=xCor(); toUnscaled(x,y); turtleLineTo(x,y); displayTurtle(); } function hideTurtle() { Property.set('turtle.hidden',1); run('Remove Overlay'); } function showTurtle() { Property.set('turtle.hidden',0); } function penDown() { Property.set('turtle.pendown',1); } function penUp() { Property.set('turtle.pendown',0); } function setHeading(head) { Property.set('turtle.heading',head); displayTurtle(); } function turtleLineTo(a,b) { pendown = Property.get('turtle.pendown'); if (pendown>0) lineTo(a,b); else moveTo(a,b); } function back(distance) { x=xCor(); y=yCor(); head=heading(); toUnscaled(x,y); alpha= Math.toRadians(180+180-head); x1=x+distance*sin(alpha); y1=y+distance*cos(alpha); turtleLineTo(x1, y1); toScaled(x1,y1); Property.set('turtle.x',x1); Property.set('turtle.y',y1); displayTurtle(); } function forward(distance) { back(-1*distance); } function turtleGraphics() { ijtg='ImageJ Turtle Graphics'; if (isOpen(ijtg)) { selectWindow(ijtg); origin = ""+getWidth/2+","+getHeight/2; run('Properties...', 'origin='+origin+' invert'); } else { newImage(ijtg, 'RGB white', 512, 512, 1); run('Properties...', 'origin=256,256 invert'); } run('Remove Overlay'); } function clearScreen() { turtleGraphics(); setForegroundColor(0, 0, 0); setBackgroundColor(255, 255, 255); clean(); Property.set('turtle.x',0); Property.set('turtle.y',0); Property.set('turtle.heading',0); Property.set('turtle.hidden',0); Property.set('turtle.pendown',1); displayTurtle(); } function left(alpha) { head=parseFloat(heading()); Property.set('turtle.heading',head-alpha); displayTurtle(); } function right(alpha) { left(-1*alpha); displayTurtle(); } function displayTurtle() { turtleHidden=Property.get('turtle.hidden'); if (turtleHidden) return; turtleGraphics(); x=xCor(); y=yCor(); head=heading(); toUnscaled(x,y); moveTo(x,y); alpha= Math.toRadians(180-head); makeArrow(x,y, x+15*sin(alpha), y+15*cos(alpha), 'notched medium'); Roi.setStrokeColor('gray'); run('Add Selection...'); }