Quantcast
Channel: SCN : Document List - ABAP Development
Viewing all articles
Browse latest Browse all 935

Calculate Mandelbrot set in ABAP

$
0
0

If you feel bored by crunching through business data, you could create some nice images in ABAP. For example, a fractal. Below is a simple program for calculating a Mandelbrot set. Just add a screen 0100 with a single custom controller, named CUSTOM_CONTAINER. Note that GUI status is not set, so buttons will not be active. You should add them yourself if you need.

 

mb.jpg

 

REPORT  ZTEST_FRAC.

TYPES:
   BEGIN OF lty_data,
     data(2556) TYPE x,
   END OF lty_data.

TYPES:
   BEGIN OF lty_cpx,
     x TYPE f,
     y TYPE f,
   END OF lty_cpx.

DATA custom_container TYPE REF TO cl_gui_custom_container.
DATA picture TYPE REF TO cl_gui_picture.
DATA bitmap TYPE xstring.
DATA bitmap_url TYPE char255.
DATA lt_data TYPE TABLE OF lty_data.

DATA:
   BEGIN OF bmp_info,
     bfType(2) TYPE x VALUE '424D', " BM
     bfSize(4) TYPE x VALUE '36100E00', " 921654=640*480*3+54 bytes
     bfReserved1(2) TYPE x VALUE '0000',
     bfReserved2(2) TYPE x VALUE '0000',
     bfOffBits(4) TYPE x VALUE '36000000', " 54=14+40 bytes
     biSize(4) TYPE x VALUE '28000000', " 40 bytes
     biWidth(4) TYPE x VALUE '80020000', " 640 pixels
     biHeight(4) TYPE x VALUE 'E0010000', " 480 pixels
     biPlanes(2) TYPE x VALUE '0100', " 1
     biBitCount(2) TYPE x VALUE '2000', " 32 bits
     biCompression(4) TYPE x VALUE '00000000',
     biSizeImage(4) TYPE x VALUE '00100E00', " 921600=640*480*3 bytes
     biXPelsPerMeter(4) TYPE x VALUE '00000000',
     biYPelsPerMeter(4) TYPE x VALUE '00000000',
     biClrUsed(4) TYPE x VALUE '000000',
     biClrImportant(4) TYPE x VALUE '000000',
   END OF bmp_info.

DATA:
   BEGIN OF bmp_color,
     r TYPE x VALUE '00',
     g TYPE x VALUE '00',
     b TYPE x VALUE '00',
     a TYPE x VALUE '00',
   END OF bmp_color.

DATA c1 TYPE lty_cpx.
DATA c2 TYPE lty_cpx.
DATA max_l TYPE f.
DATA max_n TYPE i.
DATA c0 TYPE lty_cpx.
DATA z TYPE lty_cpx.
DATA z_temp TYPE lty_cpx.
DATA n TYPE i.
DATA l TYPE f.
DATA dx TYPE i.
DATA dy TYPE i.

DATA lt_points TYPE TABLE OF i.

c1-x = '-0.9'. c1-y = '0.2'. " bottom-left point
c2-x = '-0.84'. c2-y = '0.26'. " top-right point
max_l = 2 * 2. " square of maximum distance, i.e. "infinity"
max_n = 256. " maximum number of iterations
dx = 640. " pixel width (note: bitmap has it hardcoded)
dy = 480. " pixel height (note: bitmap has it hardcoded)

* Calculate Mandelbrot set
DO dy TIMES.
   c0-y = c1-y + sy-index * ( c2-y - c1-y ) / dy.
   DO dx TIMES.
     c0-x = c1-x + sy-index * ( c2-x - c1-x ) / dx.
     z = c0.
     l = 0.
     n = 0.
     WHILE l < max_l AND n < max_n.
       z_temp-x = z-x * z-x - z-y * z-y + c0-x.
       z_temp-y = 2 * z-x * z-y + c0-y.
       z = z_temp.
       l = z-x * z-x + z-y * z-y.
       ADD 1 TO n.
     ENDWHILE.
     IF n >= max_n.
       n = 0.
     ENDIF.
     APPEND n TO lt_points.
   ENDDO.
ENDDO.

* Fill BMP header
CONCATENATE
     bmp_info-bfType
     bmp_info-bfSize
     bmp_info-bfReserved1
     bmp_info-bfReserved2
     bmp_info-bfOffBits
     bmp_info-biSize
     bmp_info-biWidth
     bmp_info-biHeight
     bmp_info-biPlanes
     bmp_info-biBitCount
     bmp_info-biCompression
     bmp_info-biSizeImage
     bmp_info-biXPelsPerMeter
     bmp_info-biYPelsPerMeter
     bmp_info-biClrUsed
     bmp_info-biClrImportant
   INTO bitmap IN BYTE MODE.

* Fill BMP pixel array
LOOP AT lt_points INTO n.

   bmp_color-b = n.

   CONCATENATE bitmap
       bmp_color-b
       bmp_color-g
       bmp_color-r
       bmp_color-a
     INTO bitmap IN BYTE MODE.

ENDLOOP.

* Prepare and output bitmap in SAP GUI
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
   EXPORTING
     BUFFER                = bitmap
   TABLES
     BINARY_TAB            = lt_data.

CALL FUNCTION 'DP_CREATE_URL'
      EXPORTING
         type                 = 'IMAGE'
         subtype              = 'X-UNKNOWN'
      TABLES
         data                 = lt_data
      CHANGING
         url                  = bitmap_url.

CREATE OBJECT custom_container
   EXPORTING
     container_name = 'CUSTOM_CONTAINER'.

CREATE OBJECT picture
   EXPORTING parent = custom_container.

picture->load_picture_from_url( exporting url = bitmap_url ).

CALL SCREEN 0100.


Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>