GLBasic forum

Codesnippets => Math => Topic started by: Quentin on 2008-Jan-10

Title: Schranzors Rand und Gernots Kreis
Post by: Quentin on 2008-Jan-10
Code: GLBasic [Select]
LIMITFPS 20
fac = 50
WHILE TRUE
  FOR i = 0 TO 359
    x = fac * SIN(i) + 320
    y = fac * COS(i) + 240
    SETPIXEL x + RAND(-5, 5), y + RAND(-5, 5), RGB(128, 128, 128)
    SETPIXEL x + RAND(-10, 10), y + RAND(-10, 10), RGB(176, 176, 176)
    SETPIXEL x + RAND(-20, 20), y + RAND(-20, 20), RGB(208, 208, 208)
    SETPIXEL x + RAND(-30, 30), y + RAND(-30, 30), RGB(240, 240, 240)
    SETPIXEL x + RAND(-40, 40), y + RAND(-40, 40), RGB(255, 255, 255)
  NEXT
  INC fac, 10
  IF fac > 500 THEN fac = 10
  SHOWSCREEN
WEND


FUNCTION RAND: minimum, maximum
  RETURN minimum + RND(-minimum + maximum)
ENDFUNCTION
Title: Schranzors Rand und Gernots Kreis
Post by: Schranz0r on 2008-Jan-10
cool ^^
Title: Re: Schranzors Rand und Gernots Kreis
Post by: Hemlos on 2008-Aug-15
Very Cool, i like both, the rand function and the sprites.

Not to be picky, but this is the math section afterall...
Standard Math for SIN and COS are as follows:

COS()x
SIN()y

If you switch them, your angles get transformed incorrectly, and result in a 90 degree transformation.

Typically all 0 angle naturally point to the right, as with all standard math,  writing, and technology.

In this case it doesnt matter because it is used in a full circles.
If you use this function with standard angles and non circles,
 it is recommended you standard the math for debugging and cross referencing information.

SIN COS TAN Defined:
http://www.gcseguide.co.uk/sin,_cos,_tan.htm (http://www.gcseguide.co.uk/sin,_cos,_tan.htm)

Also i made a tutorial in this math section for using SIN and COS in circular math.
Title: Re: Schranzors Rand und Gernots Kreis
Post by: aonyn on 2011-Feb-15
Hi Hemlos,

Thanks for posting that link to your math guide.
I just looked through several sections, and it is all very well explained and easy to follow.

regards,
Dave
Title: Re: Schranzors Rand und Gernots Kreis
Post by: monono on 2011-Feb-15
Nice combination! Easy and good looking.

I am going to shamelessly use this thread to advert my beloved little rand function =D

Code: GLBasic [Select]
FUNCTION QRND: b, e=0 , scale=1
        LOCAL n = ABS(e-b)
        RETURN b+ n/(scale*n)*RND(scale*n)
ENDFUNCTION
 

Examples:
QRND(20) - nothing special, like RND(20)
QRND(20,100) - numbers between 20 and 100
QRND(20,100,10) -numbers between 20 and 100 and 10 steps from one number to the next one  20,20.1...99.9,100
and so on..
Works with negative numbers, too.

How cool is that, uh? =D
Title: Re: Schranzors Rand und Gernots Kreis
Post by: Moebius on 2011-Feb-16
Useful, but with one problem:
QRND(100) returns values from 100-200 instead of 0-100...
QRND(n) returns values from n to 2*n instead of 0 to n...

Try this instead:
Code: GLBasic [Select]
FUNCTION QRND: b, e=0 , scale=1
        LOCAL n = ABS(e-b)
        RETURN e + n/(scale*n)*RND(scale*n)
ENDFUNCTION
 

I just replaced the b with the e  :P