Qbasicnews.com
August 10, 2020, 10:07:03 PM
 Pages: [1]
 Author Topic: Koch Curve  (Read 1810 times)
Deleter
Na_th_an

Posts: 1293

 « on: November 28, 2005, 10:05:13 PM »

Although I dont understand it 100% yet (actual algorithim was stolen from wikipedia ) I did manage to make this FB program to draw it.
Change iter to change level to which its drawn. change s to change the size.
Code:

DECLARE SUB DPOINT (v AS INTEGER)
DECLARE SUB DRIGHT (v AS INTEGER)
DECLARE SUB DLEFT (v AS INTEGER)
DECLARE SUB DDRAW (v AS SINGLE)
DECLARE SUB DMOVE (v AS SINGLE)

CONST pi=3.14'159265358979323846264338327950288419716939937510582097494459

SCREENRES 800,600
DIM SHARED AS SINGLE x,y,iter,s
DIM SHARED AS INTEGER ang
x=300
y=250
iter=4
s=243*2

FOR temp = 1 TO iter
s=s/3
NEXT

' place pointer
DPOINT 150
DMOVE 140
DPOINT 0
' start
GOSUB SIDE
DRIGHT 120
GOSUB SIDE
DRIGHT 120
GOSUB SIDE
' finished.
SLEEP
END

' main loop
SIDE:
GOSUB F
DLEFT 60
GOSUB F
DRIGHT 120
GOSUB F
DLEFT 60
GOSUB F
RETURN
' forward
F:
IF iter > 1 THEN
' GOSUB deeper depending on level
iter=iter-1
GOSUB SIDE
iter=iter+1
ELSE
' or just do a single line
DDRAW s
END IF
RETURN

SUB DPOINT (v AS INTEGER)
ang=v
END SUB

SUB DRIGHT (v AS INTEGER)
ang-=v
END SUB

SUB DLEFT (v AS INTEGER)
ang+=v
END SUB

SUB DDRAW (v AS SINGLE)
DIM AS SINGLE nx, ny
nx=x+(COS(ang*pi/180)*v)
ny=y+(-SIN(ang*pi/180)*v)
LINE(x,y)-(nx,ny)
x=nx
y=ny
END SUB

SUB DMOVE (v AS SINGLE)
x+=(COS(ang*pi/180)*v)
y+=(-SIN(ang*pi/180)*v)
END SUB
 Logged

 Pages: [1]