HomeSoftware
logo HomeProgrammingBasic

Programming

BASIC

Warthog-sdl

Warthog is an algorithm that I first saw published in an issue of the magazine "Investigación y ciencia" (Scientific American) in 1990. The algorithm is very simple. Consider a natural number F and the sequence of values defined by the function N^3/F with N=1,2,3,....,F. The algorithm consists of linking F segments of equal length so that the end point of each segment is the start point of the next one, and the orientation of the segment is defined by the fractional part of N^3/F (values from 0 to 1) where 0 represents a bearing of 0º and 1 represents a bearing of 360º. The result is surprising geometric figures with symmetry axes, which sometimes vaguely resemble insects, flowers, ghosts, jewels, and others. For each value of F, different figures are obtained, increasing in complexity as the number of segments increases, and certain patterns are reproduced. One wonders how it is possible that such a simple function yields such artistic results? And we come to the conclusion that nature uses mathematics to achieve its beauty and diversity.

warthog
Figure 1. Warthog F=1999
warthog
Figure 2. Warthog F=2531
warthog
Figure 3. Warthog F=2533
warthog
Figure 4. Warthog F=165765
warthog
Figure 5. Warthog F=165765 daleko

   Download code: warthog.sdlbas


// Warthog figures
// Adapted from 1990 article in "American Scientific"
// Tested first time in 90' using my ZX Spectrum 48K
// Pere Casellas
// version 20180520 using sdlBasic
// GNU GPL 3.0

setDisplay( 1000, 650, 16, 2 )
setcaption("Warthog")

// Initial variables
//for f =1 to 250
//printS(f)

// Change the parameter f to obtain incredible figures
f=1998

fprintS( "Segments number is:")
printS(f)

k=13  // segment units
x=450     //  initial x
y=125     // initial y
pi = 3.14159265

// Figure made of f segments
for n =1 to f

// Define colors
if n<=f/3 then
ink(rgb(255,0,0))
end if
if n>f/3 then
ink(rgb(0,255,0))
end if
if n>(2*f/3) then
ink(rgb(0,0,255))
end if

// Warthog function
zz=(n^3)/f
z=2*pi*(zz-int(zz))  // angle as fractional part of zz
x1=k*cos(z)
y1=k*sin(z)
xx=x+x1
yy=y+y1

// Draw segment
line(x,y,xx,yy)

// Define initial point
x=xx
y=yy

next

wait(100)
waitKey

cls

//next