Thursday 17 October 2019

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!

SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Certifications

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( ).

SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Certifications

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.



SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Certifications

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( ).

SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Certifications

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]` )
    )
).

SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Certifications

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.

SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Certifications

It was good.

No comments:

Post a Comment