Skip to Content
Technical Articles
Author's profile photo Frederik Hudak

AbapTurtle – make something pretty in abap (and possibly win prizes!)

Introduction

In order for our legacy to continue, we should be thinking about raising the next generation of ABAP programmers. In my experience, talent can easily be attracted by using shiny or colorful things.

Turtle graphics are a popular method of visual programming. If only we had something like this in ABAP! Well, we do now.

Note that the library does not actually have an animated turtle. It just generates the resulting image as an svg and shows it in a browser window. The advantage of this approach is that no turtles were harmed or even present during the making of abapTurtle.

Examples

Basic usage

You draw turtle graphics using code.

The library offers the classic turtle movement instructions forward, back, right and left which are all you really need (well, also some loops).

For example, to draw a square:

DATA(turtle) = zcl_turtle=>new( width = 640 height = 480 ).
turtle->goto( x = 100 y = 100 ).
DO 4 TIMES.
  turtle->forward( 100 ).
  turtle->right( 90 ).
ENDDO.
turtle->show( ).

Or something more complicated:

DATA(turtle) = zcl_turtle=>new( height = 800 width = 800 ).
DATA(polygons) = 15.
DATA(polygon_sides) = 10.
turtle->goto( x = 200 y = 200 ).

DATA(current_polygon) = 0.
WHILE current_polygon < polygons.

  " draw a regular polygon
  DATA(current_polygon_side) = 0.
  DATA(side_length) = 50.
  WHILE current_polygon_side < polygon_sides.
    turtle->forward( side_length ).
    turtle->right( 360 / polygon_sides ).
    current_polygon_side = current_polygon_side + 1.
  ENDWHILE.

  " rotate before painting next polygon
  turtle->right( 360 / polygons ).

  current_polygon = current_polygon + 1.
ENDWHILE.

A random color is chosen for each line. This behavior as well as the colors are customizable.

If you compare the image above with the abapTurtle logo, you can clearly see that abapTurtle is easily capable of outperforming estabilished competing tools such as mspaint.

Less basic usage

SVG

You can also directly generate svg shapes such as lines, polygons or text without having to move the turtle.

turtle->circle( center_x = 50 center_y = 50 radius = 100 ).

Look at the readme to see what else is available.

L-Systems

Define an initial state, a number of iterations and a set of replacement rules which will be applied in each iteration. Finally, the symbols are translated into instructions and executed.

For example: the rule F -> F+F

will be expanded from F to F+F, to F+F+F+F, to F+F+F+F+F+F+F+Fover 3 iterations. Finally, Fis interpreted as forwardand +as right.

This is not exactly hard to use because you can just try some parameters at random and check the results.

Using this method, you can generate complex images very easily:

DATA(turtle) = zcl_turtle=>new( height = 800 width = 600 ).
turtle->goto( x = 200 y = 200 ).

DATA(parameters) = VALUE zcl_turtle_lsystem=>params(
  initial_state = `F`
  move_distance = 10
  rotate_by = 90
  num_iterations = 3
  rewrite_rules = VALUE #(
    ( from = `F` to = `F+F-F-F+F` )
    )
).

DATA(lsystem) = zcl_turtle_lsystem=>new(
  turtle = turtle
  parameters = parameters ).

lsystem->execute( ).
lsystem->show( ).

 

DATA(parameters) = VALUE zcl_turtle_lsystem=>params(
  initial_state = `F`
  move_distance = 10
  rotate_by = 21
  num_iterations = 4
  rewrite_rules = VALUE #(
    ( from = `F` to = `FF-[+F+F+F]+[-F-F+F]` )
    )
).

 

Contest

To promote our engineering culture as well as to get people to make something nice in ABAP for once, I am announcing a contest for the best picture created using abapTurtle.

Rules:

  • Make your submission as a pull request to the contest repository. You can make as many submissions as you want.
  • The competition will run until the end of October, after which all pull requests will be merged.
  • The pull request with the most positive emojis at the end of the competition wins.

I will personally make sure that the winner receives a framed picture of their submission as well as an empty bottle of wine with the label SAP Product Engineering, which I unfortunately drank before I got this idea.

It was good.

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Florian Henninger
      Florian Henninger

      That’s a cool one. I like and happy I don’t like wine?

      Author's profile photo Michael Keller
      Michael Keller

      Using programming languages for something they were not initially invented for is pure fun 🙂

      Author's profile photo Jacques Nomssi Nzali
      Jacques Nomssi Nzali

      Nice. I am now thinking about an abap Scheme based TurtleScript wrapper.

      Author's profile photo Frederik Hudak
      Frederik Hudak
      Blog Post Author

      Then we can make a TurtleStudio IDE and just rewrite everything using turtles. Finally some motivation to learn lisp!

      Author's profile photo Jacques Nomssi Nzali
      Jacques Nomssi Nzali

      I see there is a Racket implementation to target.

       

      Author's profile photo Sandra Rossi
      Sandra Rossi

      If you do it, and if it’s pedagogic enough, I would love to try it, to learn Scheme/Lisp (at least the basis).

      EDIT: hmmm... after reading your git Scheme home page (nicely written and presented), forget it, too much new concepts to learn, I think that would kill me 😀

      Author's profile photo Jacques Nomssi Nzali
      Jacques Nomssi Nzali

      Oh, do not worry, the concepts are really old ?

      Understanding them is fun. You will be writting your own interpreter in no time…

      For a nice, short guide check https://docs.racket-lang.org/guide/to-scheme.html