How to resolve the algorithm Brownian tree step by step in the Ring programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Brownian tree step by step in the Ring programming language

Table of Contents

Problem Statement

Generate and draw a   Brownian Tree.

A Brownian Tree is generated as a result of an initial seed, followed by the interaction of two processes. Because of the lax rules governing the random nature of the particle's placement and motion, no two resulting trees are really expected to be the same, or even necessarily have the same general shape.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Brownian tree step by step in the Ring programming language

Source code in the ring programming language

# Project : Brownian tree

load "stdlib.ring"
load "guilib.ring"

paint = null

new qapp 
        {
        win1 = new qwidget() {
                  setwindowtitle("")
                  setgeometry(100,100,800,600)
                  label1 = new qlabel(win1) {
                              setgeometry(10,10,800,600)
                              settext("")
                  }
                  new qpushbutton(win1) {
                          setgeometry(150,500,100,30)
                          settext("draw")
                          setclickevent("draw()")
                  }
                  show()
        }
        exec()
        }

func draw
        p1 = new qpicture()
               color = new qcolor() {
               setrgb(0,0,255,255)
        }
        pen = new qpen() {
                 setcolor(color)
                 setwidth(1)
        }
        paint = new qpainter() {
                   begin(p1)
                   color = new qcolor()
                   color.setrgb(255,0,0,255)    
                   pen = new qpen() {
                   setcolor(color)
                   setwidth(1)}
                   setpen(pen)

        browniantree()

        endpaint()
        }
        label1 { setpicture(p1) show() }
        return

func browniantree()
        numparticles = 3000
        canvas = newlist(210,210)
        canvas[randomf() * 100][randomf() * 200] = 1 
        for i = 1 to numparticles
             x = floor((randomf() * 199)) + 1 
             y = floor((randomf() * 199)) + 1
             if x = 1
                x = 2
             ok
             if y = 1
                y = 2
             ok
             while canvas[x+1][y+1]+canvas[x][y+1]+canvas[x+1][y]+canvas[x-1][y-1]+canvas[x-1][y]+canvas[x][y-1] = 0 
                     x = x + floor((randomf() * 2)) + 1     
                     y = y + floor((randomf() * 2)) + 1 
                     if x = 1
                        x = 2
                     ok
                     if y = 1
                        y = 2
                     ok
                     if x < 1 or x > 200 or y < 1 or y > 200 
                        x = floor((randomf()  * 199)) + 1 
                        y = floor((randomf()  * 199)) + 1
                        if x = 1
                           x = 2
                        ok
                        if y = 1
                           y = 2
                        ok
                     ok
             end
             canvas[x][y] = 1
             paint.drawpoint(x,y)
             paint.drawpoint(x,y+1)
             paint.drawpoint(x,y+2)
        next 

func randomf()
       decimals(10)
       str = "0."
       for i = 1 to 10
            nr = random(9)
            str = str + string(nr)
       next
       return number(str)

  

You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Modular inverse step by step in the Delphi programming language
You may also check:How to resolve the algorithm Happy numbers step by step in the Racket programming language
You may also check:How to resolve the algorithm Search a list step by step in the PHP programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the Liberty BASIC programming language