Author Topic: 2D drawstars() drawthistle()  (Read 4942 times)

Offline sf-in-sf

  • Mr. Drawsprite
  • **
  • Posts: 93
  • Artist F.P. Brixey
    • View Profile
    • My computed art project
2D drawstars() drawthistle()
« on: 2013-May-07 »
Hi! Here is a rough template to draw all sorts of stars, flowers, etc. Try, look, tweak, have fun!
Nightly EDIT:
drawthistle (outcommented) is an invitation to experiment. I'm surprised that 2 different functions for x and y still works. For non-integer multiples of beta, the thistle doesn't loop on itself in the worse case -nothing terrible.
Sorry, I haven't tried MEM2SPRITE yet, in the hope the plotting goes faster. Please let me know if anyone gets good results.
Code: (glbasic) [Select]
// --------------------------------- //
// Project: cog_A1
// Start: Tuesday, May 07, 2013
// IDE Version: 10.244

// SETCURRENTDIR("Media") // go to media files

SYSTEMPOINTER TRUE
GLOBAL scrx%=666, scry%=666 //your custom size please.
// For PC use menu;project;options;
GETSCREENSIZE scrx, scry
SETSCREEN scrx,scry,0
CREATESCREEN 1,1,scrx,scry
// the drawing surface.
GLOBAL cnt%

//****************************************************
WHILE TRUE
//press F12 to view long lines of code.
LOCAL alpha=(RND(1999)/1000.0 -1.0)+0.00000003
// additive when >0
// interpolated when <0, ='mix' ='cross-fade'
// ...in my opinion.
LOCAL color%=RGB(128*RND(2)-1,128*RND(2)-1,128*RND(2)-1)
LOCAL smoothedge = 2.0 +13*RND(3)
LOCAL thickness=14*RND(3) // (0 means filled shape)

drawstars_a(RND(scrx), RND(scry), 4+RND(300), thickness, color, alpha, smoothedge,5+RND(7), 0.01+RND(44)*0.01)

USESCREEN -1
ALPHAMODE -1
DRAWSPRITE 1,0,0
SHOWSCREEN
INC cnt,1
IF cnt >7
cnt=0
SEEDRND (GETTIMERALL()) //shuffle the cards.
ENDIF

//FOR more clarity:
DEBUG alpha ; DEBUG "\n"
// please activate the 'bug' icon
// SLEEP 2012 // additional time

// SLEEP 800 +RND(3333)
//SLEEP 787// Merry dream, liner!
WEND
//****************************************************


FUNCTION drawstars_a: cx,cy,r,thick,col%,a,smoo,freq%,amp
// adjust the params to make a cog, a flower, some rounded n-gons, a star...
// with some luck I even got an accurate David's star. (freq=6 I guess).
USESCREEN 1
IF smoo <0 THEN smoo =0
LOCAL halfsmoo =smoo*0.5
LOCAL th =thick*0.5
LOCAL ra  = (r+halfsmoo+th)
LOCAL ra2  = (r+halfsmoo+th)*(r+halfsmoo+th) //outermost
LOCAL rb2  = (r+th)*(r+th) //
LOCAL rc2  = (r-th)*(r-th) //
LOCAL rd2  = (r-th-halfsmoo)*(r-th-halfsmoo) //innermost
LOCAL rd  = (r-th-halfsmoo)

LOCAL d2,d1,x,y,beta,sinbeta,cosbeta
LOCAL r2 =r*r
LOCAL w_out =ra-(r+th) +0.000001//r2_ -r2
LOCAL w_in  =(r-th)-rd +0.000001

FOR xxx=0 TO scrx
FOR yyy=0 TO scry
x=xxx ; y=yyy

d2=(x-cx)*(x-cx) +(y-cy)*(y-cy) //*2.0

IF d2>0
d1=SQR(d2)
cosbeta=(x-cx)/d1
sinbeta=(y-cy)/d1

IF cosbeta >= 0
beta=ASIN(sinbeta) // +90 -> -90
ELSE
beta=180-ASIN(sinbeta)
ENDIF
// IF x=300 ; DEBUG beta ; DEBUG "   \n" ; ENDIF

// Draw star:
x=cx +((x-cx)*(1+amp*SIN(freq*beta)))
y=cy +((y-cy)*(1+amp*SIN(freq*beta))) // wavy lens effect
//or Draw thistle:
//x=cx +((x-cx)*(1+amp*0.5*(SIN(freq*beta)+SIN(freq*beta*2))))
//y=cy +((y-cy)*(1+amp*0.5*(SIN(freq*beta)+SIN(freq*beta*5.42167)+SIN(freq*beta*1.5))))

d2=(x-cx)*(x-cx) +(y-cy)*(y-cy) //again, after lens.
ENDIF


IF d2 <= r2 //internal
IF thick>0
IF d2 >= rc2
//plot it plain.
ALPHAMODE a
SETPIXEL xxx,yyy,col
ELSEIF d2 <=  rd2
// ignore it
ELSE
// interpolate, blend it.
ALPHAMODE (SQR(d2) -rd)*a/w_in
SETPIXEL xxx,yyy,col
ENDIF
ELSE // thick<=0, fill the shape.
ALPHAMODE a
SETPIXEL xxx,yyy,col
ENDIF
ELSE //external

IF d2 < rb2
//plot it plain.
ALPHAMODE a
SETPIXEL xxx,yyy,col
ELSEIF d2 >=  ra2
// ignore it
ELSE
// interpolate, blend it.
ALPHAMODE (ra -SQR(d2))*a/w_out
SETPIXEL xxx,yyy,col
ENDIF
ENDIF
NEXT
NEXT
ENDFUNCTION

// P.S. On the day the atom is a cube I will start believing in the square pixel.
« Last Edit: 2013-May-09 by sf-in-sf »
On the day the atom is a cube I will start believing in the square pixel.

Offline Marmor

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 925
  • 96A285CC
    • View Profile
    • my youtube channel
Re: 2D drawstars()
« Reply #1 on: 2013-May-07 »
great stuff thx a lot  :good:

Offline CW

  • Mr. Polyvector
  • ***
  • Posts: 155
    • View Profile
Re: 2D drawstars()
« Reply #2 on: 2013-May-07 »
Very cool.

(Do the kids still say 'cool' these days? Oh dear.)
Hmm.. Ok, How about..

Ain't SUCK'n!! (tm)  :good:
-CW
« Last Edit: 2013-May-07 by CW »

Offline sf-in-sf

  • Mr. Drawsprite
  • **
  • Posts: 93
  • Artist F.P. Brixey
    • View Profile
    • My computed art project
Re: 2D drawstars() drawthistle()
« Reply #3 on: 2013-May-09 »
Thanks guys. See the last update I just posted, switch to the other 2 lines x= and y=.
On the day the atom is a cube I will start believing in the square pixel.