#BBC BASIC, 281 ASCII characters, tokenised filesize 236 #
BBC BASIC, 281 ASCII characters, tokenised filesize 236
#BBC BASIC, 281 ASCII characters, tokenised filesize 236 #
BBC BASIC, 281 ASCII characters, tokenised filesize 236
A centered s-gonal number is of the form (triangular number t[n])*s+1. I take an input a, then iterate through the triangular numbers until a-1 is not greater than 3*t[n]. If s is an integer, we clear the screen and plot the polygonal numberdrawing. After plotting, we wait for the user to press a key (the screen will be cleared again for the next polygonal numberdrawing if there is one.) To check if s s an integer, we store it in an integer variable s% (integer variables are identified by the % suffix) and compare the two. In this program I use casting to integer variables several times as a means of converting reals to integers.
In order to avoid trying to do too much in the first IF statement, it is natural to put the plotting operation in a function, and having done that it may as well be recursive. The function plots the wedge as a binary tree with overlapping branches. This is short, but highly inefficent (exponential time in number of rings.) With the largest sizes some points are plotted literally millions of times. The worst case is 976, where there are 25 triangular rings, which is expected to take several hours to run :-) This only happens for a very small number of inputs and should beis fixable for a few extra characters (currently 15) but I don't see speed as essential to the rules.
A centered s-gonal number is of the form (triangular number t[n])*s+1. I take an input a, then iterate through the triangular numbers until a-1 is not greater than 3*t[n]. If s is an integer, we clear the screen and plot the polygonal number. After plotting, we wait for the user to press a key (the screen will be cleared again for the next polygonal number if there is one.) To check if s s an integer, we store it in an integer variable s% (integer variables are identified by the % suffix) and compare the two. In this program I use casting to integer variables several times as a means of converting reals to integers.
In order to avoid trying to do too much in the first IF statement, it is natural to put the plotting operation in a function, and having done that it may as well be recursive. The function plots the wedge as a binary tree with overlapping branches. This is short, but highly inefficent (exponential time in number of rings.) With the largest sizes some points are plotted literally millions of times. The worst case is 976, where there are 25 triangular rings, which is expected to take several hours to run :-) This only happens for a very small number of inputs and should be fixable for a few extra characters.
A centered s-gonal number is of the form (triangular number t[n])*s+1. I take an input a, then iterate through the triangular numbers until a-1 is not greater than 3*t[n]. If s is an integer, we clear the screen and plot the drawing. After plotting, we wait for the user to press a key (the screen will be cleared again for the next drawing if there is one.) To check if s s an integer, we store it in an integer variable s% (integer variables are identified by the % suffix) and compare the two. In this program I use casting to integer variables several times as a means of converting reals to integers.
In order to avoid trying to do too much in the first IF statement, it is natural to put the plotting operation in a function, and having done that it may as well be recursive. The function plots the wedge as a binary tree with overlapping branches. This is short, but highly inefficent (exponential time in number of rings.) With the largest sizes some points are plotted literally millions of times. The worst case is 976, where there are 25 triangular rings, which is expected to take several hours to run :-) This only happens for a very small number of inputs and is fixable for a few extra characters (currently 15) but I don't see speed as essential to the rules.
#BBC BASIC, 281 ASCII characters, tokenised filesize 236 #
Emulator at http://www.bbcbasic.co.uk/bbcwin/bbcwin.html
Newlines are required and included in the score.
c=400INPUTa:n=1t=0REPEATt+=n:s=(a-1)/t:r%=c/n/2CLS:MOVEc,c:s%=s:IFs%=s u=r%*2:v=0FORp=1TOs%q%=r%*COS(p/s%*2*PI)x=q%*2q%=r%*SIN(p/s%*2*PI)y=q%*2q=FNt(n)u=x:v=y:NEXT:q=GET
n+=1UNTILs<=3END
DEFFNt(d)PLOT153,8,0MOVEBY-8,0IFd MOVEBY u,v:q=FNt(d-1)DRAWBY x-u,y-v:q=FNt(d-1)MOVEBY-x,-y
=0
A centered s-gonal number is of the form (triangular number t[n])*s+1. I take an input a, then iterate through the triangular numbers until a-1 is not greater than 3*t[n]. If s is an integer, we clear the screen and plot the polygonal number. After plotting, we wait for the user to press a key (the screen will be cleared again for the next polygonal number if there is one.) To check if s s an integer, we store it in an integer variable s% (integer variables are identified by the % suffix) and compare the two. In this program I use casting to integer variables several times as a means of converting reals to integers.
The polygon is plotted wedge by wedge. The radial vectors for the first two points (u,v) and (x,y) are calculated. All the code for this must be on the same line as the IF statement. Unfortunately the resolution of the plotting of this BBC basic emulator is only to the nearest multiple of 2, so in order to avoid problems we ensure that the vector parameters are multiples of 2 (by storing the values of the trig calculations in an integer variable q% then multiplying by 2. (the vectors themselves are real variables, to save characters.) There is also a variable q which serves only to accept return values of functions (required for syntax reasons.)
In order to avoid trying to do too much in the first IF statement, it is natural to put the plotting operation in a function, and having done that it may as well be recursive. The function plots the wedge as a binary tree with overlapping branches. This is short, but highly inefficent (exponential time in number of rings.) With the largest sizes some points are plotted literally millions of times. The worst case is 976, where there are 25 triangular rings, which is expected to take several hours to run :-) This only happens for a very small number of inputs and should be fixable for a few extra characters.
Ungolfed Code
c=400 :REM Approx half the height of the screen area
INPUTa
n=1 :REM Number of rings
t=0 :REM t=triangular number n
REPEAT
t+=n :REM Update t to the next triangular number
s=(a-1)/t :REM Number of sides of polygon
r%=c/n/2 :REM Choose a good length for (half)the radius of the first ring.
CLS:MOVEc,c :REM Clear screen and move graphics cursor to middle
s%=s :REM Store the number of sides in an integer variable
IFs%=s :REM If real variable and integer variable are equal (all the following indent MUST be on the same line as the IF)
u=r%*2:v=0: :REM initialise (u,v) to 360 deg.
FORp=1TOs%: :REM Loop through the sides.
q%=r%*COS(p/s%*2*PI): :REM Calculate x/2, assign to integer variable
x=q%*2: :REM assign to x, ensuring x is a multiple of 2.
q%=r%*SIN(p/s%*2*PI): :REM ditto y.
y=q%*2: :REM ditto y.
q=FNt(n): :REM Plot the wedge from n down to 0 (the first row is the centre dot, which will be overdrawn s times)
u=x:v=y: :REM Reinitialise u and v with the last values of x and y
NEXT: :REM in order to plot the next wedge.
q=GET :REM Allow user to view output, wait for keypress.
n+=1 :REM increment n ready to try the next triangular number
UNTILs<=3 :REM If number of sides already <=3
END :REM exit program.
DEFFNt(d) :REM BBC basic puts the nut & bolt functions at the end!
PLOT153,8,0:MOVEBY-8,0 :REM Draw a filled circle radius 8 centred at the current graphics cursor position. After drawing, return cursor to centre of circle.
IFd :REM if this is not the final iteration (all the following indent MUST be on the same line as the IF)
MOVEBY u,v: :REM move to the point where the first circle of the next ring is to be drawn
q=FNt(d-1): :REM draw the circle and the rest of the branch
DRAWBY x-u,y-v: :REM draw the concentric line to the second circle of the next ring
q=FNt(d-1): :REM draw the circle and the rest of the branch
MOVEBY-x,-y :REM return cursor to where it was before calling the function
=0 :REM supply a return value to exit the function.
Output, 31 dots
I know the dots are small, but that comes in handy for the larger numbers.
enter image description here