Basicprogramming(.org) > Code and examples

Fun Shapes with Pedal Equation

(1/3) > >>

B+:
Beautiful! Rick, this is art!  ;D

SdlBasic version:

--- Code: ---' Fun Shapes with Pedal Equation.sdlbas [B+=MGA] 2017-03-30
' from Fun shapes from Pedal Eq.bas  SmallBASIC 0.12.8 [B+=MGA] 2017-03-30
' inpired by Andy Amaya's screen shots and reference to Superformula
' Here is some experimenting with the pedal equation

const xmax = 800
const ymax = 600
setdisplay(xmax, ymax, 32, 1)
setcaption("Fun Shapes with the Pedal Equation")
autoback(-2)

const pi = acos(-1)
randomize(timer)

function fx(ra, mx, my, e)
'mx, my multipliers of angle
'e exponent of pedal
'mx, my simple multipliers of the ra
fx = ( ( cos(mx * ra) )  ^  e + ( sin(my * ra) ) ^ e) ^ .5
end function

sub polarPlotter(x, y, ra, rdist, fillTF)
'x, y think of as the origin of the plot
'rdist = radial distance from the "origin"
x1 = x + rdist * cos(ra)
y1 = y + rdist * sin(ra)
if fillTF then : line(x, y, x1, y1) : else : circle(x1, y1, 2) : end if
end sub

scale = 200 : fillTF = 0 : cn = 1
for e = 2 to 10
for m1 = 1 to 10
for m2 = 1 to 10
if m1 <> m2 then
cls
ink(0x00bbee)
'circle xmax/2, ymax/2, scale color 12  'for frame of reference red circle with radius 100
text(10, 10, 18, "e = " + str(e) + "  m1 = " + str(m1) + "  m2 = " + str(m2) )
if rnd(11) = 1 then
r = (rnd(10001)/10000) ^ 2 : g =  (rnd(10001)/10000)  ^ 2 : b = (rnd(10001)/10000)  ^ 2
for s = 200 to 0 step -1
ink(rgb( 127+127 * sin(r * cn), 127 + 127 * sin(g * cn), 127 + 127 * sin(b * cn) ) )
cn += .1
for a = 0 to 2 * pi  step .001
d = fx(a, m1, m2, e)
polarPlotter(xmax/2, ymax/2, a, s * d, 0)
next
screenswap
next
else
for a = 0 to 2 * pi  step .001
d = fx(a, m1, m2, e)
polarPlotter(xmax/2, ymax/2, a, scale * d, fillTF)
next
screenswap
end if
wait(400)
end if
next
next
next

--- End code ---

SmallBASIC version:

--- Code: ---' Fun shapes from Pedal Eq.bas  SmallBASIC 0.12.8 [B+=MGA] 2017-03-30
' inpired by Andy Amaya's screen shots and reference to Superformula
' Here is some experimenting with the pedal equation

randomize timer

func fx(ra, mx, my, e)
'mx, my multipliers of angle
'e exponent of pedal
'mx, my simple multipliers of the ra
fx = ( ( cos(mx * ra) ) ^ e + ( sin(my * ra) ) ^ e) ^ .5
end

sub polarPlotter(x, y, ra, rdist, fillTF)
'x, y think of as the origin of the plot
'rdist = radial distance from the "origin"
local l
l.x1 = x + rdist * cos(ra)
l.y1 = y + rdist * sin(ra)
if fillTF then line x, y, l.x1, l.y1 else circle l.x1, l.y1, 1 filled
end

scale = 200 : fillTF = 0
for e = 2 to 10
for m1 = 1 to 10
for m2 = 1 to 10
if m1 <> m2 then
cls
color 11
'circle xmax/2, ymax/2, scale color 12  'for frame of reference red circle with radius 100
locate 1, 1 : ? "e = ";e;"  m1 = ";m1;"  m2 = ";m2
if rnd < .1 then
r = rnd ^ 2 : g = rnd ^ 2 : b = rnd ^ 2
for s = 200 to 0 step -1
cc = rgb(127+127*sin(r+cn), 127+127*sin(g*cn), 127+127*sin(b*cn))
cn += .1
color cc
for a = 0 to 2 * pi  step .001
d = fx(a, m1, m2, e)
polarPlotter xmax/2, ymax/2, a, s * d, 0
next
showpage
next
else
for a = 0 to 2 * pi  step .001
d = fx(a, m1, m2, e)
polarPlotter xmax/2, ymax/2, a, scale * d, fillTF
next
showpage
end if
delay 200
end if
next
next
next

--- End code ---

Rick3137:
Nice Work. :)

Peter:
Very nice indeed! My port to BaCon, as usual I tried to stay as close to the original code as possible, though I had to rename 'y1' because this is a C function (Bessel calculation). Also, I changed 'fx' to a 'def fn', as this function only contained one line.

--- Code: ---' Fun Shapes with Pedal Equation.sdlbas [B+=MGA] 2017-03-30
' from Fun shapes from Pedal Eq.bas  SmallBASIC 0.12.8 [B+=MGA] 2017-03-30
' inpired by Andy Amaya's screen shots and reference to Superformula
' Here is some experimenting with the pedal equation
' Port to BaCon by PvE - April 2017 - compile using the '-j' option.

include canvas.bac

option vartype float

const xmax = 800
const ymax = 600
window("Fun Shapes with the Pedal Equation", xmax, ymax)

'mx, my multipliers of angle
'e exponent of pedal
'mx, my simple multipliers of the ra
def fn fx(ra, mx, my, e) = pow(( pow(( cos(mx * ra) ), e) + pow(( sin(my * ra) ), e) ), .5)

sub polarPlotter(x, y, ra, rdist, fillTF)
'x, y think of as the origin of the plot
'rdist = radial distance from the "origin"
x1 = x + rdist * cos(ra)
y_1 = y + rdist * sin(ra)
if fillTF then : line(x, y, x1, y_1) : else : circle(x1, y_1, 2, 2, 0) : end if
end sub

thescale = 200 : fillTF = 0 : cn = 1
for e = 2 to 10
for m1 = 1 to 10
for m2 = 1 to 10
if m1 <> m2 then
ink(0,0,0,255)
cls
ink(0x00,0xbb,0xee,0xff)
'circle xmax/2, ymax/2, scale color 12  'for frame of reference red circle with radius 100
text("e = " & str\$(e) & "  m 1 = " & str\$(m1) & "  m 2 = " & str\$(m2) , 10, 18)
if random(11) = 1 then
r = pow((random(10001)/10000), 2) : g =  pow((random(10001)/10000), 2) : b = pow((random(10001)/10000), 2)
for s = 200 to 0 step -1
ink(127+127 * sin(r * cn), 127 + 127 * sin(g * cn), 127 + 127 * sin(b * cn), 255 )
incr cn, .1
for a = 0 to 2 * pi step .001
d = fx(a, m1, m2, e)
polarPlotter(xmax/2, ymax/2, a, s * d, 0)
next
sync
next
else
for a = 0 to 2 * pi step .001
d = fx(a, m1, m2, e)
polarPlotter(xmax/2, ymax/2, a, thescale * d, fillTF)
next
sync
end if
sleep 400
end if
next
next
next

--- End code ---

B+:

--- Code: ---' Fun shapes from Pedal Eq 2.bas  SmallBASIC 0.12.8 [B+=MGA] 2017-04-06
' inpired by Andy Amaya's screen shots and reference to Superformula
' Here is some experimenting with the pedal equation

'2017-04-06 now using lightning fast thick line and
' 2nd color fill method, midInk along with Plasma method

randomize(timer)
const cx = 350
const cy = 350

func rand(n1, n2)
local hi, lo
if n1 > n2 then hi = n1 : lo = n2 else hi = n2 : lo = n1
rand = (rnd * (hi - lo + 1)) \ 1 + lo
end

'ink the color that is percent between the first color and 2nd color
sub midInk(r1, g1, b1, r2, g2, b2, percent)
dr = (r2 - r1) / 100 : dg = (g2 - g1) / 100 : db = (b2 - b1) / 100
color rgb(r1 + dr * percent, g1 + dg * percent, b1 + db * percent)
end

'new kick butt fast tline!!!
sub tline(x1, y1, x2, y2, thick) 'this draws a little rectangle
local arr, r, dx, dy, perpA1, perpA2
dim arr()
r = thick/2
dx = x2 - x1
dy = y2 - y1
perpA1 = atan2(dy, dx) + pi/2
perpA2 = perpA1 - pi
arr << x1 + r * cos(perpA1) 'corner 1
arr << y1 + r * sin(perpA1)
arr << x2 + r * cos(perpA1) 'corner 2
arr << y2 + r * sin(perpA1)
arr << x2 + r * cos(perpA2) 'corner 3
arr << y2 + r * sin(perpA2)
arr << x1 + r * cos(perpA2) 'corner 4
arr << y1 + r * sin(perpA2)
arr << x1 + r * cos(perpA1) 'back to first corner
arr << y1 + r * sin(perpA1)
drawpoly arr filled
end

func fx(ra, mx, my, e)
'mx, my multipliers of angle
'e exponent of pedal
'mx, my simple multipliers of the ra
fx = ( ( cos(mx * ra) ) ^ e + ( sin(my * ra) ) ^ e) ^ .5
end

locate 1, 1 : ? "A moment..."
showpage
scale = 200 : fillTF = 0
while 1
e = rand(1, 15) : m1 = rand(1, 15) : m2 = rand(1, 15)
if m1 <> m2 then
cls
if rnd < .5 then 'randomly select fill method
cmode = 1 'plasma mode
r = rnd ^ 2 : g = rnd ^ 2 : b = rnd ^ 2 : cn = .1
ms = "Plasma"
else
cmode = 0 'midInk mode
r1 = rand(0, 255) : g1 = rand(0, 255) : b1 = rand(0, 255)
r2 = rand(0, 255) : g2= rand(0, 255) : b2 = rand(0, 255)
ms = "midInk"
end if
color 11 'label drawing
locate 1, 1 : ? "e = ";e;"  m1 = ";m1;"  m2 = ";m2;"  Fill Method: ";ms
for s = 200 to 0 step -1
if cmode then
cc = rgb(127+127*sin(r*cn), 127+127*sin(g*cn), 127+127*sin(b*cn))
cn += .1
color cc
else
midInk r1, g1, b1, r2, g2, b2, s/2
end if
for a = 0 to 2 * pi+.011  step .01
d = fx(a, m1, m2, e)
x = int(cx + d * s * cos(a))
y = int(cy + d * s * sin(a))
if a = 0 then
lastx = x : lasty = y
else
tline lastx, lasty, x, y, 3  'gets most holes
lastx = x: lasty = y
end if
'showpage 'watch shapes being formed, very s l o w l y
next
next
showpage
'delay 200  'not needed
end if
wend

--- End code ---

Hard to beat Plasma fill method!

Mike Lobanovsky:
Isn't it supposed to be called "peTal", not "pedal", equation?  ;)