Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/awk -f
- function begin_SVG() {
- print "<?xml version='1.0' encoding='UTF-8'?>";
- print " <!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>";
- print "";
- print "<svg xmlns='http://www.w3.org/2000/svg'";
- print " xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:ev='http://www.w3.org/2001/xml-events'";
- print " version='1.1' baseProfile='tiny'";
- print " width='800px' height='800px'";
- print " viewBox='-1.02 -1.02 2.04 2.04'";
- print " >";
- print "";
- print " <rect x='-1.01' y='-1.01' width='2.02' height='2.02' stroke='black' stroke-width='0.25%' fill='none' />";
- print " <g stroke='black' stroke-width='0.2%' fill='none' transform='scale( 1 -1 )' stroke-linecap='round' >";
- print "";
- }
- function end_SVG() {
- print "";
- print " </g>";
- print "</svg>";
- print "";
- }
- function initTurtle() {
- last_action="";
- stack = 1;
- X[stack] = 0.0;
- Y[stack] = 0.0;
- A[stack] = 90.0;
- L[stack] = 1.0;
- D[stack] = 90.0;
- C[stack] = 0;
- setFactor();
- }
- function push() {
- stack++;
- X[stack] = X[stack-1]; # X position
- Y[stack] = Y[stack-1]; # Y position
- A[stack] = A[stack-1]; # moving Angle
- F[stack] = F[stack-1]; # Factor
- L[stack] = L[stack-1]; # Length
- D[stack] = D[stack-1] # Delta angle
- C[stack] = C[stack-1]; # Colour
- }
- function pop() {
- closePath();
- stack--;
- if (stack < 1) {
- stack = 1;
- }
- openPath();
- }
- function getColour(c) {
- if ( c == 0 ) return "Black";
- if ( c == 1 ) return "Red";
- if ( c == 2 ) return "Green";
- if ( c == 3 ) return "Yellow";
- if ( c == 4 ) return "Blue";
- if ( c == 5 ) return "Magenta";
- if ( c == 6 ) return "Cyan";
- if ( c == 7 ) return "White";
- if ( c ~ /^#[0-9A-Fa-f]+$/ ) return c;
- return "YellowGreen";
- }
- function setColour(c) {
- closePath();
- C[stack] = c;
- openPath();
- }
- function nextColour() {
- setColour((C[stack]+1)%7);
- }
- function mulLength() {
- L[stack] *= F[stack];
- }
- function divLength() {
- L[stack] /= F[stack];
- }
- function turnLeft() {
- updateSVG("turn");
- A[stack] += D[stack];
- }
- function turnRight() {
- updateSVG("turn");
- A[stack] -= D[stack];
- }
- function mulAngle() {
- D[stack] *= F[stack];
- }
- function divAngle() {
- D[stack] /= F[stack];
- }
- function incFactor() {
- F[stack]++;
- }
- function decFactor() {
- F[stack]--;
- }
- function setFactor() {
- F[stack] = 2;
- }
- function openPath() {
- print " <path stroke='" getColour(C[stack]) "' d='" ;
- # print " M " X[stack] "," Y[stack] " " ;
- last_action = "move"
- }
- function closePath() {
- updateSVG("");
- print " ' />" ;
- }
- function updateSVG(action) {
- if ( action != last_action ) {
- if ( last_action == "move" ) {
- printf(" M %10.5f,%10.5f\n", X[stack], Y[stack]);
- } else
- if ( last_action == "draw" ) {
- printf(" L %10.5f,%10.5f\n", X[stack], Y[stack]);
- }
- last_action = action;
- }
- }
- function move() {
- updateSVG("move");
- X[stack] += L[stack] * cos(A[stack]*PI/180.0);
- Y[stack] += L[stack] * sin(A[stack]*PI/180.0);
- }
- function draw() {
- updateSVG("draw");
- X[stack] += L[stack] * cos(A[stack]*3.1415927/180.0);
- Y[stack] += L[stack] * sin(A[stack]*3.1415927/180.0);
- }
- BEGIN {
- PI = 3.14159265359;
- IGNORECASE = 1;
- initTurtle();
- begin_SVG();
- openPath();
- }
- # for each line in the source code
- {
- # split line into characters
- split($0, chars, "");
- # for each character
- for ( i=1; i<=length(chars); ++i) {
- if (chars[i] == "F") draw();
- else if (chars[i] == "X") move();
- else if (chars[i] == "*") mulLength();
- else if (chars[i] == "/") divLength();
- else if (chars[i] == "+") turnLeft();
- else if (chars[i] == "-") turnRight();
- else if (chars[i] == ">") mulAngle();
- else if (chars[i] == "<") divAngle();
- else if (chars[i] == "!") incFactor();
- else if (chars[i] == "?") decFactor();
- else if (chars[i] == "$") setFactor();
- else if (chars[i] == "[") push();
- else if (chars[i] == "]") pop();
- else if (chars[i] == "@") nextColour();
- else if (chars[i] == "#") { setColour(substr($0,i,7)); i+=6; continue; }
- else if (chars[i] >= 0 \
- && chars[i] <= 7) setColour(chars[i]);
- }
- }
- END {
- closePath();
- end_SVG();
- }
Advertisement
Add Comment
Please, Sign In to add comment