Author Topic: Randomize Overlaid Circles  (Read 22736 times)

Cybermonkey

  • Administrator
  • *****
  • Posts: 0
Randomize Overlaid Circles
« on: February 28, 2016, 06:44:42 PM »
I ported one of the examples from Stephen's page (http://shawweb.myzen.co.uk/stephen/sdlbasic.htm) to AllegroBASIC:

Code: [Select]
import "keycodes.bas"

screen (800,600,"Randomize Overlaid Circles")
srnd (val(right(time,2)))
TEXTSIZE (2)
SIDE=15
do
cls
  FOR I=1  TO SCREENWIDTH
    FOR J=1 TO SCREENHEIGHT
      X=I*SIDE/600
      Y=J*SIDE/600
      C=FIX(X*X+Y*Y)
      D=C/2
      IF D-FIX(D)<0.11 THEN
        PSET (I+1,J+1)
      ENDIF
     NEXT
     IF WINDOWCLOSED=TRUE THEN END
     REDRAW
NEXT
INK (0,0,0)
fillrectangle (0,0,screenwidth,40)
INK (255,255,255)
DRAWTEXT (0,0,"ESC to end, R for random new one, S to zoom back")
DRAWTEXT (0,20, "to start, any other to zoom out.")
SYNC
  SIDE=SIDE*1.15
  INKEY
  SRND (TICKCOUNT)
IF KEYSTATE (KEY_R) THEN
   SIDE=FIX(RND*160)+1
   ENDIF
IF KEYSTATE (KEY_S) THEN
  SIDE=15
 ENDIF
 INK (255,255,255)
until (keystate (KEY_ESC)=true) or (windowclosed=true)
END

Cybermonkey

  • Administrator
  • *****
  • Posts: 0
Re: Randomize Overlaid Circles
« Reply #1 on: September 16, 2016, 08:28:51 PM »
I just learned that there was also a (much shorter) Simons' BASIC example for the Commodore 64. I ported this for CBR-BASIC (details will follow soon), as shown in the screenshots.

B+

  • Guest
Re: Randomize Overlaid Circles
« Reply #2 on: September 16, 2016, 11:00:51 PM »
So that's where Peter W got that code...

Well here is colorized version posted some time ago.

Code: [Select]
'pattern.bas for SmallBASIC 0.12.0 2015-11-27 Peter W & MGA/B+
'pixel version

f=4   '<================== 1 to 8? speed, the faster the less fine
s=500
while 1
  for i=0 to xmax/f
    for j=0 to ymax/f
      x=i*s/600
      y=j*s/600
      c= x*x+y*y
      d=c/2
      d=d-int(d)
      if d <0.25 then
        cc=rgbf(d*4,0,0)
      elif d<.5
        cc=rgbf(0,2*d,0)
      elif d<.75
        cc=rgbf(0,0,4/3*d)
      else
        cc=0
      fi   
      rect i*f,j*f step f,f,cc filled
      'pset i,j,cc
    next
  next
  showpage
  pause 'for snapshot
  s+=5
  if s>1000 then s=5
wend

ScriptBasic

  • Guest
Re: Randomize Overlaid Circles
« Reply #3 on: September 17, 2016, 03:21:01 AM »
This is my SDL_gfx circle demo.

Code: [Select]
' ScriptBasic GFX - Alpha Circles
 
IMPORT gfx.inc
 
scrn = gfx::Window(640, 480, "ScriptBasic GFX - Alpha Circles")
' Random Value Arrays
RANDOMIZE(gfx::Time())
FOR i = 0 TO 512
  rx[i] = RND() % 640
  ry[i] = 60 + RND() % 480 - 80
  rz[i] = RND() % 64
  rr[i] = RND() AND  255
  rg[i] = RND() AND  255
  rb[i] = RND() AND  255
  af = rx[i] / 640
  ra[i] = INT(255 * af)
NEXT
 
ts = gfx::Time()
FOR i = 0 TO 512
  gfx::filledCircleRGBA scrn, rx[i], ry[i], rz[i], rr[i], rg[i], rb[i], ra[i]
NEXT
te = gfx::Time()
gfx::stringColor scrn, 20, 15, "Time: " & FORMAT("%.4f",(te-ts)/1000) & " Seconds." & CHR(0), 0xffffffff
gfx::Update
WHILE gfx::KeyName(1) <> "+escape"
WEND
gfx::Close



B+

  • Guest
Re: Randomize Overlaid Circles
« Reply #4 on: September 17, 2016, 05:38:49 AM »

ScriptBasic

  • Guest
Re: Randomize Overlaid Circles
« Reply #5 on: September 17, 2016, 06:46:30 AM »
Quote
Really not random at all!

Hugh?

The circle(s) size, position, color and alpha channel are randomized.

B+

  • Guest
Re: Randomize Overlaid Circles
« Reply #6 on: September 17, 2016, 06:49:18 AM »
Quote
Really not random at all!

Hugh?

The circle(s) size, position, color and alpha channel are randomized.

Not your code, the original code from which Cyb sourced, the Connett Circles. They are not really random.
A random number generator (RND) is never used except to setup different screen shot.
« Last Edit: September 17, 2016, 06:54:19 AM by B+ »

B+

  • Guest
Re: Randomize Overlaid Circles
« Reply #7 on: September 17, 2016, 07:15:09 AM »
Here is SmallBASIC code rectangles translated to Naalaa pixels for exe:
Code: [Select]
'patterns.txt Naalaa [B+=MGA] from
'pattern.bas for SmallBASIC 0.12.0 2015-11-27 Peter W & MGA/B+
'pixel colorized version
constant:
xmax = 800
ymax = 600
hidden:
set window 100, 40, xmax, ymax
set redraw false
 
s# = 500.0
while 1
set colori 0xffffff
cls
set colori 0
for y = 0 to ymax
for x = 0 to xmax
a# = float(x) * s / 600.0
b# = float(y) * s / 600.0
c# = a# * a# + b# * b#
d# = c# / 2.0
d# = d# - float(int(d#))
if d# < 0.25
r = int(d# * 4.0 * 255.0)
set color r, 0, 0
elseif d# < 0.5
g = int(d# * 2.0 * 255.0)
set color 0, g, 0
elseif d# < 0.75
bl = int(d# * 4.0 / 3.0 * 255.0)
set color 0, 0, bl
else
set color 0, 0, 0
endif
draw pixel x, y
next
next
redraw
wait 200
if keydown(27, true) then end
s# = s# + 5.0
if s# > 1000.0 then s# = 5.0
wend

Nothing random about it!

Peter

  • Guest
Re: Randomize Overlaid Circles
« Reply #8 on: September 17, 2016, 09:27:32 AM »
BaCon pixel version where I have tried to stick to the original code as much as possible.

Code: [Select]
'pattern.bas for SmallBASIC 0.12.0 2015-11-27 Peter W & MGA/B+
'pixel version
'ported to BaCon by PvE - sep 2016 - use '-z' option to compile small letters

include canvas-gd.bac
option vartype double

window("Pattern",800,600)
f=1   : '<================== 1 to 8? speed, the faster the less fine
s=500
for frame=1 to 16
  for i=0 to WIDTH/f
    for j=0 to HEIGHT/f
      x=i*s/600
      y=j*s/600
      c= x*x+y*y
      d=c/2
      d=d-round(d)
      if d <0.25 then
        ink(d*4,0,0,255)
      elif d<.5 then
        ink(0,2*d,0,255)
      elif d<.75 then
        ink(0,0,4/3*d,255)
      else
        cc=0
      fi
      'rect i*f,j*f step f,f,cc filled
      'pset i,j,cc
      pixel(i,j)
    next
  next
  sync
  delay(500) : 'for snapshot
  incr s,5
  if s>1000 then s=5
next


Tomaaz

  • Guest
Re: Randomize Overlaid Circles
« Reply #9 on: September 17, 2016, 03:47:53 PM »
Python (using graphics.py):

Code: [Select]
from graphics import *
xmax = 800
ymax = 600
win = GraphWin('Circles', xmax, ymax)
win.autoflush = False
win.getMouse()
while 1:
s = 500
for x in range(0, xmax + 1):
for y in range(0, ymax + 1):
a = x * s / 600.0
b = y * s / 600.0
c = a * a + b * b
d = c / 2.0
d = d - int(d)
if d < 0.25:
kol = color_rgb(d * 4, 0, 0)
elif d < 0.5:
kol = color_rgb(0, 2 * d, 0)
elif d < 0.75:
kol = color_rgb(0, 0, 4 / 3.0 * d)
else:
kol = color_rgb(255, 0, 0)
win.plot(x, y, kol)
print x
win.flush()
win.getMouse()
if s == 1000:
s = 500
else:
s += 5

B+

  • Guest
Re: Randomize Overlaid Circles
« Reply #10 on: September 17, 2016, 05:12:22 PM »
Thanks Tomaaz, I've yet to figure out graphics on Python, PyGame is disaster!   :(

Anyway, although I like red allot, the SmallBASIC code from which you are coloring with d numbers was using RGBF scales not RGB.

To translate RGBF to RGB, multiply by 255 as all RGBF numbers run from 0 to 1, or 0 % to 100% of 255

Append:
Tomaaz code defaults to red in the else section, I have no idea how Peter is getting red in his snapshots.
« Last Edit: September 17, 2016, 05:37:56 PM by B+ »

Tomaaz

  • Guest
Re: Randomize Overlaid Circles
« Reply #11 on: September 17, 2016, 05:35:52 PM »
Thanks Tomaaz, I've yet to figure out graphics on Python, PyGame is disaster!   :(

Pygame may seem a bit complex and bloated for someone who comes from BASIC (or EGSL or NaaLaa) background. If you want to start with graphics in Python, have a look at:

graphics.py

It's based on Tkinter and very easy to use. Works with Python2, 3 and PyPy (JIT compiler for Python).

Website, download + documentation
Alternative documentation

GASP

I've been using it only with Python 2 as it is aviable in Ubuntu's reposirories. It's a bit outdated but still works and can be used for learning.

Tutorial

Processing.py

This is a Python version of Processing (that is based on Java). It comes with a dedicated IDE (the same Processing comes - you just need to add Python mode to it).

Website

Turtle

A Python implementation of classic Logo language. It's based on Tkinter. Extremely easy to use.

Turtle

Sorry about a little bit of off-topic. ;)

B+

  • Guest
Re: Randomize Overlaid Circles
« Reply #12 on: September 17, 2016, 05:54:51 PM »
Oh for Windows and graphics, don't start IDLE from Start Menu! Hmm... that may explain some of my problems.

Thanks Tomaaz!

Mike Lobanovsky

  • Guest
Re: Randomize Overlaid Circles
« Reply #13 on: September 17, 2016, 06:08:42 PM »
Hehe, may I boast a little? :)

Here's the same on the FBSL GDI Canvas, cleaned of some dead code that's hiding in the original, as I'm seeing it in my editor:

Code: [Select]
#AppType Console // use console to examine listing
#Option Implicit // don't declare variables
#Include "Canvas.inc"

With Canvas
  .Window("Interference", 400, 300)
  .Font("Baccarat", 24, 1)
  .ForeColor(0, 255, 255)
  Do
    s = 500
    For frame = 1 To 16
      For i = 0 To .Width
        For j = 0 To .Height
          x = i * s / 600
          y = j * s / 600
          c = x ^ 2 + y ^ 2
          d = c / 2
          d = d - Round(d)
          .PSet(i, j, RGB(4 * d, 0, 0))
        Next
        If Not .hWnd Then Exit Do
      Next
      .Write(10, 10, "Great Vibes!")
      .Refresh()
      Wait(200)
      If Incr(s, 5) > 1000 Then s = 5
    Next
  Loop
End With

and the attached image shows how the listing would look in the console (i.e. if the #AppType Console mode is active) when run from its original .FBS file (Windows) and a precompiled .EXE (Ubuntu and Mac OS X Wine) if the .EXE isn't exepacked, or password-protected, or both. If it is then the listing won't be seen at all but the code will still run. The .EXE that's attached in the zip below is neither exe-packed nor protected, so its console output will be similar to Wine. The differences are due to the .EXE listing being actually decompiled on the fly from the .EXE itself where its formatting and indentation are minimized and all the PP macros, if any, are already resolved to literals. In the default #AppType GUI mode the listing is of course unavailable.

As it happens, a 800x600 pxs window is a bit too much for FBSL BASIC's interpreted PSet() so my window is smaller but responsive and fast, albeit still a little CPU intensive. As I said, I'm developing my Canvas based on your examples, so if I ever need to fill its entire surface with triply nested PSet() loops again, I will have to add a BASIC-to-C translator to the CCanvas class to go fully DynC JIT compiled. ;D

ScriptBasic

  • Guest
Re: Randomize Overlaid Circles
« Reply #14 on: September 17, 2016, 08:29:34 PM »
Quote
I will have to add a BASIC-to-C translator to the CCanvas class to go fully DynC JIT compiled.

That would be cool!

Nice job adding the text feature to your CCanvas class.

« Last Edit: September 17, 2016, 08:31:52 PM by John »