SuperEllipse

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
neil
Posts: 594
Joined: Mar 17, 2022 23:26

SuperEllipse

Post by neil »

Here's an example of a Superellipse I found.
https://en.wikipedia.org/wiki/Superellipse

Code: Select all

Const scr_x = 800       ' screen 800 x 800
Const scr_y = 600
Const m_x = scr_x \ 2   ' middle of screen
Const m_y = scr_y \ 2

Sub superellipse(a As Long, b As Long, n As Double)

    ReDim As Long y(0 To a)
    Dim As Long x

    y(0) = b ' value for x = 0
    y(a) = 0 ' value for x = a

    '(0,0) is in upper left corner

    PSet (m_x, m_y - y(0)) ' set starting point

    For x = 1 To a-1
        y(x) = Int( Exp( Log(1 - ((x / a) ^ n)) / n ) * b )
        Line - ((m_x + x), (m_y - y(x)))
    Next

    For x = a To 0 Step -1
        Line - ((m_x + x), (m_y + y(x)))
    Next

    For x = 0 To a
        Line - ((m_x - x), (m_y + y(x)))
    Next

    For x = a To 0 Step -1
        Line - ((m_x - x), (m_y - y(x)))
    Next

End Sub

' ------=< MAIN >=------

ScreenRes scr_x, scr_y, 32

Dim As Long   a = 200
Dim As Long   b = 150
Dim As Double n = 2.5

superellipse(a, b, n)

' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "Press any key to end program"
Sleep
End
Here's DeepAI's Chat version.

Code: Select all

screenres 800, 600, 32
Const screenwidth = 800
Const screenheight = 600
dim as integer xc, yc
dim as double a, b, m, n
Const pi = 3.1459265

xc = screenwidth / 2
yc = screenheight / 2
a = 200
b = 100
m = 2.5
n = 2.5

for i as double = -pi to pi step 0.01
    dim as double x = a * abs(cos(i)) ^ (2 / m) * sgn(cos(i))
    dim as double y = b * abs(sin(i)) ^ (2 / n) * sgn(sin(i))
    pset (xc + x, yc + y), rgb(255, 255, 255)
next

sleep
Post Reply