Qbasicnews.com
October 22, 2019, 03:31:50 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Koch Curve  (Read 1679 times)
Deleter
Na_th_an
*****
Posts: 1292



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

Although I dont understand it 100% yet (actual algorithim was stolen from wikipedia Tongue) 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
' calculate adjusted side-length
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]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!