|
Post by David Drake on Feb 27, 2020 6:42:17 GMT -5
The code below is for the Rosetta Code task "Super ellipse" ( rosettacode.org/wiki/Superellipse). This one was mathematically interesting. I ended up adapting the Julia code already on the site. The final effect is a nifty graphic. A superellipse is a geometric figure defined as the set of all points (x, y) with abs((x/a)^n)+abs((y/b)^n)=1 where n, a, and b are positive numbers.
Task Draw a superellipse with n = 2.5, and a = b = 200
I reworked the Julia version to work and added a loop with a spread on n values. Please comment and make suggestions. 'Reworked the Julia version to work and added a loop with a spread on n values
[start] nomainwin UpperLeftX=1:UpperLeftY=1 WindowWidth=800:WindowHeight=600 open "Super Ellipse" for graphics_nf_nsb as #1 #1 "trapclose [q];down;fill black;flush;color green;size 1"
n=1.5 a=200 b=200
for n = 0.1 to 5 step .1 na=2/n t=.01 for i = 0 to 314 xp=a*sign(cos(t))*abs((cos(t)))^na+350 yp=b*sign(sin(t))*abs((sin(t)))^na+275 t=t+.02 #1 "set ";xp;" ";yp next i next n
'plot only the super ellipse for the task n=2.5 na=2/n t=.01 #1 "color white;size 4" for i = 0 to 314 xp=a*sign(cos(t))*abs((cos(t)))^na+350 yp=b*sign(sin(t))*abs((sin(t)))^na+275 t=t+.02 #1 "set ";xp;" ";yp next i wait
[q] close #1 end
function sign(x) if x<0 then sign=1 if x>0 then sign=-1 if x=0 then sign=0 end function
|
|
|
Post by Brandon Parker on Feb 27, 2020 8:52:47 GMT -5
If you were so inclined, you could change the sign function to remove the If...Then statements. This is not as clear, but the code does create a size reduction. It's just another way of doing the same thing, but without the If...Then statements.
function sign(x) sign = (x < 0) + ((x > 0) * -1) end function
{:0)
Brandon Parker
|
|
|
Post by tsh73 on Feb 27, 2020 9:20:04 GMT -5
sign = (x < 0) - (x > 0) LOL
|
|
|
Post by David Drake on Feb 27, 2020 11:54:38 GMT -5
sign = (x < 0) - (x > 0) LOL You guys are too much! Maybe we should just ask Carl to add a sgn() command like other languages.
|
|
|
Post by David Drake on Mar 2, 2020 6:59:41 GMT -5
Unless there are other comments, I plan to post this to Rosetta Code later today.
|
|
|
Post by Brandon Parker on Mar 2, 2020 14:26:01 GMT -5
Go for it!!
{:0)
Brandon Parker
|
|