Squares

General FreeBASIC programming questions.
Locked
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Squares

Post by Richard »

And a linear spiral like yours, but without an Atn() or trigonometry in the loop.
A negative rate of growth is neat.

Code: Select all

'----------------------------------
' linear spiral
'----------------------------------
Dim As Integer xres, yres, xctr, yctr
Screeninfo xres, yres
Screenres xres, yres, 4
xctr = xres \ 2
yctr = yres \ 2

'----------------------------------
Dim As Double turns, steps_per_turn, rate_of_growth
Dim As Double vx, vy, t, sint, cost, temp, k, radius

'----------------------------------
' set the spiral parameters here
turns = 7
steps_per_turn = 90
rate_of_growth = 50  ' per turn

' spiral starting point is relative to centre
vx = 50
vy = 0

'----------------------------------
' precompute
radius = Sqr( vx*vx + vy*vy )
vx /= radius ' normalise start point vector
vy /= radius

Const As Double TwoPi = 8 * Atn( 1 )
t = TwoPi / steps_per_turn  ' radians per step
cost =  Cos( t )
sint = -Sin( t )

'----------------------------------
' now go round in ever-changing circles
Pset( radius * vx + xctr, radius * vy + yctr ), 0

For t = 0 To turns * steps_per_turn
    k = radius + rate_of_growth * t / steps_per_turn
    temp = vx
    vx = temp * cost - vy * sint
    vy = temp * sint + vy * cost
    
    Line -( k * vx + xctr, k * vy + yctr ), 12
    
Next t

'----------------------------------
Sleep
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Let the compiler do the maths.

Code: Select all

screen 20
dim as double delta=.01,rad=350,start
do
    circle(1024\2,768\2),rad,12,start,start+delta
    rad-=delta*8
    start+=delta
loop until rad<=0

sleep  
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Richard
@Dodicat

Here's Pat_016.bas again , ( Trig Spiral ) , I simplified the formula a little.

I Made it so , the radius doesn't change with , fullcircle * md .. You can set md to any value , and the spiral stays the same size.

md ( multiplier divider ) = number of rings you want in the spiral...

Code: Select all


dim as single c2
dim as single s2
dim as single x2
dim as single y2
dim as single deg2
dim as single rad2

dim as integer xctr , yctr , radius , divisions , fullcircle

dim as integer xres , yres
'screen 19
screeninfo xres , yres
screenres xres , yres , 8 , 1 , 8

xctr = xres / 2
yctr = yres / 2

'radius = (xres*yres)/((xres+yres)*4)
radius = 100

divisions = 45

rad2 = atn( 1 ) / divisions
fullcircle = atn( 1 ) * 8 / rad2

dim as longint md = 8

do
   
        for deg2 = 0 to fullcircle * md step .1
   
            c2 = cos( deg2 * rad2 )
            s2 =  sin( deg2 * rad2 )
   
            x2 = radius * c2 * atn( deg2 / ( fullcircle * md ) )  * deg2 * rad2 / (md*2)
            y2 = radius * s2 * atn( deg2 / ( fullcircle * md ) )  * deg2 * rad2 / (md*2)
            
            pset( xctr + x2 , yctr + y2 ) , 9
            
        next

loop until inkey <>""

albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Spiral

Post by albert »

@Richard
@Dodicat

I modified the "Trig-Spiral" Pat_016.bas. ( Spiral Perfected )

Now it has a ( true radius ) , so what ever you set the radius to , the spiral will be twice the size...

Set md = ? , to number of rings you want in the spiral..

Code: Select all


dim as single c2
dim as single s2
dim as single x2
dim as single y2
dim as single deg2
dim as single rad2

dim as integer xctr , yctr , radius , divisions , fullcircle

dim as integer xres , yres
'screen 19
screeninfo xres , yres
screenres xres , yres , 8 , 1 , 8

xctr = xres / 2
yctr = yres / 2

'radius = (xres*yres)/((xres+yres)*4)
radius = 200

divisions = 45

rad2 = atn( 1 ) / divisions
fullcircle = atn( 1 ) * 8 / rad2

dim as longint md = 8  ' set value to number of rings you want in the spiral.

do
   
        for deg2 = 0 to fullcircle * md step .1
   
            c2 = cos( deg2 * rad2 )
            s2 =  sin( deg2 * rad2 )
   
            x2 = radius * c2 * atn( deg2 / ( fullcircle * md ) )  * deg2 * rad2 / ( md * md / ( md * 2 / 10 ) )
            y2 = radius * s2 * atn( deg2 / ( fullcircle * md ) )  * deg2 * rad2 / ( md * md / ( md * 2 / 10 ) )
            
            pset( xctr + x2 , yctr + y2 ) , 9
            
        next
    
        line( xctr , yctr ) - ( xctr + radius , yctr ) , 7
        
loop until inkey <>""

!!~~EDITED~~!!

You can make an oval spiral , by changing one of the ( md * 2 ) to ( md * ? ) ( 3 or 4 or 5 ect.. )
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

You can turn the spiral into an "India-Spiral" by adding 2 *c2's

Code: Select all


dim as single c2
dim as single s2
dim as single x2
dim as single y2
dim as single deg2
dim as single rad2

dim as integer xctr , yctr , radius , divisions , fullcircle

dim as integer xres , yres
'screen 19
screeninfo xres , yres
screenres xres , yres , 8 , 1 , 8

xctr = xres / 2
yctr = yres / 2

'radius = (xres*yres)/((xres+yres)*4)
radius = 200 ' set value to size of spiral you want.

divisions = 45
rad2 = atn( 1 ) / divisions
fullcircle = atn( 1 ) * 8 / rad2

dim as longint md = 8 ' set value to number of rings you want in the spiral.

do
   
        for deg2 = 0 to fullcircle * md step .1
   
            c2 = cos( deg2 * rad2 )
            s2 =  sin( deg2 * rad2 )
   
            x2 = radius * c2 * atn( deg2 / ( fullcircle * md ) )  * deg2 * rad2 / ( md * md / ( md * 2 / 10 ) ) * c2 * c2
            y2 = radius * s2 * atn( deg2 / ( fullcircle * md ) )  * deg2 * rad2 / ( md * md / ( md * 2 / 10 ) )
            
            pset( xctr + x2 , yctr + y2 ) , 9
            
        next
    
        line( xctr , yctr ) - ( xctr + radius , yctr ) , 7
        
loop until inkey <>""

dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

More spirals.

Code: Select all

 
Type spiral
    As Long x,y
    As Double rad,grade,ang
    As Ulong clr
    Declare Sub Draw
End Type

Sub spiral.draw
    Dim As Double start,delta=rad/3500
    Var o=rad
    Do
        Circle(x,y),rad,clr,start+ang,start+ang+delta
        rad-=delta*grade
        start+=delta
    Loop Until rad<=0
    rad=o
End Sub

sub setup(s() as spiral,da() as double)
For n As Long=Lbound(s) To Ubound(s)
    With s(n)
        .rad=15+Rnd*(50)
        Dim As boolean done
        Do
            done=true
            .x=.rad+Rnd*(1024-.rad*2)
            .y=.rad+Rnd*(768-.rad*2)
            For m As Long=1 To n-1
                Var d=Sqr((.x-s(m).x)^2 + (.y-s(m).y)^2)
                If d<(.rad+s(m).rad) Then done=false:Exit For
            Next m
        Loop Until done
        .grade=.rad/(10+rnd*25)
        .ang=Rnd
        .clr=Rgb(50+Rnd*205,50+Rnd*205,50+Rnd*205)
    End With
    da(n)=Rnd/10
Next n
end sub

randomize

Dim As spiral s(1 To 30)
Dim As Double da(1 To Ubound(s))

setup(s(),da())

Screen 20,32
windowtitle "A key to refresh - - <esc> to end"
dim as string key
Do
    key=inkey
    if len(key) then setup(s(),da())
    Screenlock
    Cls
    For n As Long=Lbound(s) To Ubound(s)
        s(n).ang+=da(n)
        s(n).draw
    Next
    Screenunlock
    Sleep 1,1
Loop Until key=chr(27)
Sleep 
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

I put my spiral formula into a circle and it doesn't make a spirals...I expected a ring of spirals.. but it turns into circular rings.

Hypno-Spirals.bas

I added a second radius , for future formulas..

If you look at the patterns for a few seconds , you can feel your brain changing , like hypnosis..

Code: Select all


'===========================================================================

dim as single c1,c2
dim as single s1,s2
dim as single x1,x2
dim as single y1,y2
dim as single deg1,deg2
dim as single rad

dim as integer xctr, yctr, divisions, fullcircle, toggle
dim as integer radius1 , radius2
dim as integer xres , yres

screeninfo xres , yres
screenres xres , yres , 8 , 1 , 8

xctr = xres / 2
yctr = yres / 2 -10

'radius = (xres*yres)/((xres+yres)*4)
radius1 = 300
radius2 = 100

divisions = 45
rad = atn( 1 ) / divisions
fullcircle = atn( 1 ) * 8 / rad

for deg1 = 0 to fullcircle step 45
    
            c1 = cos( deg1 * rad )
            s1 = sin( deg1 * rad )
   
            x1 = radius1 * c1
            y1 = radius1 * s1
        
        dim as longint md = 8 ' set value to number of rings you want in the spiral.
        for deg2 = 0 to fullcircle * md step .1

            c2 = cos( deg2 * rad )
            s2 =  sin( deg2 * rad )
   
            x2 = radius2 * c2 * atn( deg2 / ( fullcircle * md ) )  * deg2 * rad / ( md * md / ( md * 2 / 10 ) )
            y2 = radius2 * s2 * atn( deg2 / ( fullcircle * md ) )  * deg2 * rad / ( md * md / ( md * 2 / 10 ) )

        pset( xctr +x1 +x2 , yctr +y1 +y2 ) , 9
        pset( xctr -x1 -x2 , yctr +y1 +y2 ) , 9

        pset( xctr +x1 +x2 , yctr -y1 -y2 ) , 9
        pset( xctr -x1 -x2 , yctr -y1 -y2 ) , 9
           
    next
   
next

Sleep
System

Last edited by albert on Nov 11, 2019 23:42, edited 1 time in total.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

I found out why.. It doesn't make spirals..
I've got mirroring pset's

if you comment out , the bottom 3 pset's , it makes a circle of spirals. Like it's supposed to..

But i prefer the Hypno-Spirals code.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

I'll do some more doodles tomorrow ( Tuesday )
I've got a lot of patterns done , that i want to plug into some deg1 formulas.

Now ; with the second radius , i can make patterns of patterns..
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

I modified Abstract_0005.bas , and made a single circle for deg2..

It came out like Oriental Ying-Yang patterns. A compounded spiral.
Kinda plain , not to interesting.

Code: Select all


dim as single c1 , c2
dim as single s1 , s2
dim as single x1 , x2
dim as single y1 , y2
dim as single deg1 , deg2
dim as single rad1
dim as single rad2


dim as integer xctr, yctr, radius, divisions, fullcircle, toggle

dim as integer xres , yres
'screen 19
screeninfo xres , yres
screenres xres , yres , 8 , 1 , 8

xctr = xres / 2
yctr = yres / 2

radius = ( xres * yres ) / ( (xres + yres ) * 4 )
radius = 250

divisions = 45

rad1 = atn( 1 ) / divisions
rad2 = atn( 1 ) / ( divisions / 2 )
fullcircle = atn( 1 ) * 8 / rad1

toggle = 0
do
   
        for deg1 = 0 to fullcircle step .1
   
            c1=cos( deg1 * rad1 )
            s1=sin( deg1 * rad1 )
   
            x1=radius * c1 * atan2( deg1 ,  deg2 )
            y1=radius * s1 * atan2( deg1 ,  deg2 )
            
        for deg2 = 0 to fullcircle step 45
           
            c2=cos( deg2 * rad2 )
            s2=sin( deg2 * rad2 )
       
            x2=radius * c2 
            y2=radius * s2
             
            pset(xctr+x1+x2,yctr+y1+y2),9
            pset(xctr+-x1+x2,yctr+-y1+y2),9
            
        next
   
    next

loop until inkey <>""

I found out that atn() or atan2() are needed to make spirals..
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

Here's Abstract_0139.bas

Code: Select all


dim as single c1 , c2
dim as single s1 , s2
dim as single x1 , x2
dim as single y1 , y2
dim as single deg1 , deg2
dim as single rad1
dim as single rad2

dim as integer xctr , yctr, divisions , fullcircle 
dim as integer radius1 , radius2

dim as integer xres , yres
'screen 19
screeninfo xres , yres
screenres xres , yres , 8 , 1 , 8

xctr = xres / 2
yctr = yres / 2

radius1 = 250
radius2 = 50

divisions = 45

rad1 = atn( 1 ) / divisions
rad2 = atn( 1 ) / ( divisions / 2 )
fullcircle = atn( 1 ) * 8 / rad1

   
        for deg1 = 0 to fullcircle * 4 step 22.5
   
            c1 = cos( deg1 * rad1 )
            s1 = sin( deg1 * rad1 )
   
            x1 = radius1 * c1 * atan2( deg1 ,  deg2 )
            y1 = radius1 * s1 * atan2( deg1 ,  deg2 )
            
        for deg2 = 0 to fullcircle step .01
           
            c2 = cos( deg2 * rad2 )
            s2 = sin( deg2 * rad2 )
       
            x2 = radius2 * c2 * atan2( deg1 ,  deg2 ) * ( log( deg2 ) / 10 )
            y2 = radius2 * s2 * atan2( deg1 ,  deg2 ) * ( log( deg2 ) / 10 )
             
            pset( xctr +x1 +x2 , yctr +y1 +y2 ) , 9
            pset( xctr -x1 -x2 , yctr +y1 +y2 ) , 9
    
            pset( xctr +x1 +x2 , yctr -y1 -y2 ) , 9
            pset( xctr -x1 -x2 , yctr -y1 -y2 ) , 9

        next
   
    next

sleep
end

albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

Here's Abstract_0140.bas

Code: Select all


dim as single c1 , c2
dim as single s1 , s2
dim as single x1 , x2
dim as single y1 , y2
dim as single deg1 , deg2
dim as single rad1
dim as single rad2

dim as integer xctr , yctr, divisions , fullcircle 
dim as integer radius1 , radius2

dim as integer xres , yres
'screen 19
screeninfo xres , yres
screenres xres , yres , 8 , 1 , 8

xctr = xres / 2
yctr = yres / 2

radius1 = 250
radius2 = 100

divisions = 45

rad1 = atn( 1 ) / divisions
rad2 = atn( 1 ) / ( divisions / 2 )
fullcircle = atn( 1 ) * 8 / rad1

   
        for deg1 = 0 to fullcircle * 4 step 22.5
   
            c1 = cos( deg1 * rad1 )
            s1 = sin( deg1 * rad1 )
   
            x1 = radius1 * c1 * atan2( deg1 ,  deg2 )
            y1 = radius1 * s1 * atan2( deg1 ,  deg2 )
            
        for deg2 = 0 to fullcircle step .01
           
            c2 = cos( deg2 * rad2 )
            s2 = sin( deg2 * rad2 )
       
            x2 = radius2 * c2 * atan2( deg1 ,  deg2 ) * ( log( deg2 / deg1 ) / 20 )
            y2 = radius2 * s2 * atan2( deg1 ,  deg2 ) * ( log( deg2 / deg1 ) / 20 )
             
            pset( xctr +x1 +x2 , yctr +y1 +y2 ) , 9
            pset( xctr -x1 -x2 , yctr +y1 +y2 ) , 9
    
            pset( xctr +x1 +x2 , yctr -y1 -y2 ) , 9
            pset( xctr -x1 -x2 , yctr -y1 -y2 ) , 9

        next
   
    next

sleep
end

albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

Here's Abstract_141.bas

Code: Select all


dim as single c1 , c2
dim as single s1 , s2
dim as single x1 , x2
dim as single y1 , y2
dim as single deg1 , deg2
dim as single rad1
dim as single rad2

dim as integer xctr , yctr, divisions , fullcircle 
dim as integer radius1 , radius2

dim as integer xres , yres
'screen 19
screeninfo xres , yres
screenres xres , yres , 8 , 1 , 8

xctr = xres / 2
yctr = yres / 2

radius1 = 300
radius2 = 100

divisions = 45

rad1 = atn( 1 ) / divisions
rad2 = atn( 1 ) / ( divisions / 2 )
fullcircle = atn( 1 ) * 8 / rad1

   
        for deg1 = 0 to fullcircle * 4 step 22.5
   
            c1 = cos( deg1 * rad1 )
            s1 = sin( deg1 * rad1 )
   
            x1 = radius1 * c1 * atan2( deg1 , deg2 * c1 ) / 2
            y1 = radius1 * s1 * atan2( deg1 , deg2 * s1 ) / 2
            
        for deg2 = 0 to fullcircle step .01
           
            c2 = cos( deg2 * rad2 )
            s2 = sin( deg2 * rad2 )
       
            x2 = radius2 * c2 * atan2( deg1 ,  deg2 ) * ( log( deg2 / deg1 ) / 20 )
            y2 = radius2 * s2 * atan2( deg1 ,  deg2 ) * ( log( deg2 / deg1 ) / 20 )
             
            pset( xctr +x1 +x2 , yctr +y1 +y2 ) , 9
            pset( xctr -x1 -x2 , yctr +y1 +y2 ) , 9
    
            pset( xctr +x1 +x2 , yctr -y1 -y2 ) , 9
            pset( xctr -x1 -x2 , yctr -y1 -y2 ) , 9

        next
   
    next

sleep
end

albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

Here's Abstract_0142.bas

Code: Select all


dim as single c1 , c2
dim as single s1 , s2
dim as single x1 , x2
dim as single y1 , y2
dim as single deg1 , deg2
dim as single rad1
dim as single rad2

dim as integer xctr , yctr, divisions , fullcircle 
dim as integer radius1 , radius2

dim as integer xres , yres
'screen 19
screeninfo xres , yres
screenres xres , yres , 8 , 1 , 8

xctr = xres / 2
yctr = yres / 2

radius1 = 300
radius2 = 100

divisions = 45

rad1 = atn( 1 ) / divisions
rad2 = atn( 1 ) / ( divisions / 2 )
fullcircle = atn( 1 ) * 8 / rad1

   
        for deg1 = 0 to fullcircle * 4 step 22.5
   
            c1 = cos( deg1 * rad1 )
            s1 = sin( deg1 * rad1 )
   
            x1 = radius1 * c1 * atan2( deg1 , deg2 )
            y1 = radius1 * s1 * c1 * c1 * 3
            
        for deg2 = 0 to fullcircle step .01
           
            c2 = cos( deg2 * rad2 )
            s2 = sin( deg2 * rad2 )
       
            x2 = radius2 * c2 * ( log( deg2 / deg1 ) / 20 ) * atn( deg2 * c2 * deg1 )
            y2 = radius2 * s2 * ( log( deg2 / deg1 ) / 20 ) * atn( deg2 * s2 * deg1 )
             
            pset( xctr +x1 +x2 , yctr +y1 +y2 ) , 9
            pset( xctr -x1 -x2 , yctr +y1 +y2 ) , 9
    
            pset( xctr +x1 +x2 , yctr -y1 -y2 ) , 9
            pset( xctr -x1 -x2 , yctr -y1 -y2 ) , 9

        next
   
    next

sleep
end

Here's Abstract_0142-2.bas

Code: Select all


dim as single c1 , c2
dim as single s1 , s2
dim as single x1 , x2
dim as single y1 , y2
dim as single deg1 , deg2
dim as single rad1
dim as single rad2

dim as integer xctr , yctr, divisions , fullcircle 
dim as integer radius1 , radius2

dim as integer xres , yres
'screen 19
screeninfo xres , yres
screenres xres , yres , 8 , 1 , 8

xctr = xres / 2
yctr = yres / 2

radius1 = 300
radius2 = 300

divisions = 45

rad1 = atn( 1 ) / divisions
rad2 = atn( 1 ) / ( divisions / 2 )
fullcircle = atn( 1 ) * 8 / rad1

   
        for deg1 = 0 to fullcircle * 4 step 22.5
   
            c1 = cos( deg1 * rad1 )
            s1 = sin( deg1 * rad1 )
   
            x1 = radius1 * c1 * atan2( deg1 , deg2 )
            y1 = radius1 * s1 * c1 * c1 * 3
            
        for deg2 = 0 to fullcircle step .01
           
            c2 = cos( deg2 * rad2 )
            s2 = sin( deg2 * rad2 )
       
            x2 = radius2 * c2 * ( log( deg2 / deg1 ) / 20 ) * atn( deg2 * c2 * deg1 )
            y2 = radius2 * s2 * ( log( deg2 / deg1 ) / 20 ) * atn( deg2 * s2 * deg1 )
             
            pset( xctr +x1 +x2 , yctr +y1 +y2 ) , 9
            pset( xctr -x1 -x2 , yctr +y1 +y2 ) , 9
    
            pset( xctr +x1 +x2 , yctr -y1 -y2 ) , 9
            pset( xctr -x1 -x2 , yctr -y1 -y2 ) , 9

        next
   
    next

sleep
end

albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

Here's Abstract_0143.bas

Code: Select all


dim as single c1 , c2
dim as single s1 , s2
dim as single x1 , x2
dim as single y1 , y2
dim as single deg1 , deg2
dim as single rad1
dim as single rad2

dim as integer xctr , yctr, divisions , fullcircle 
dim as integer radius1 , radius2

dim as integer xres , yres
'screen 19
screeninfo xres , yres
screenres xres , yres , 8 , 1 , 8

xctr = xres / 2
yctr = yres / 2

radius1 = 300
radius2 = 300

divisions = 45

rad1 = atn( 1 ) / divisions
rad2 = atn( 1 ) / ( divisions / 2 )
fullcircle = atn( 1 ) * 8 / rad1

   
        for deg1 = 0 to fullcircle * 4 step 22.5
   
            c1 = cos( deg1 * rad1 )
            s1 = sin( deg1 * rad1 )
   
            x1 = radius1 * c1 * s1 * s1 * 3
            y1 = radius1 * s1 * c1 * c1 * 3
            
        for deg2 = 0 to fullcircle step .01
           
            c2 = cos( deg2 * rad2 )
            s2 = sin( deg2 * rad2 )
       
            x2 = radius2 * c2 * ( log( deg2 / deg1 ) / 20 ) * atn( deg2 * c2 * deg1 )
            y2 = radius2 * s2 * ( log( deg2 / deg1 ) / 20 ) * atn( deg2 * s2 * deg1 )
             
            pset( xctr +x1 +x2 , yctr +y1 +y2 ) , 9
            pset( xctr -x1 -x2 , yctr +y1 +y2 ) , 9
    
            pset( xctr +x1 +x2 , yctr -y1 -y2 ) , 9
            pset( xctr -x1 -x2 , yctr -y1 -y2 ) , 9

        next
   
    next

sleep
end

Locked