Code:
// RegPolygon()
// 4/7/2016 DrD
//
// Regular polygon, (equal sides, and equal angles)
//
// Finds:
// 1. Side length (a); a:=2*r*TAN(180/n), a:=2*R*SIN(180/n), a:=SQRT((4*A*TAN(180/n))n)
// 2. Inradius (apothem: Distance from center to midpoint of a side) (r); r:=1/2*a*COT(180/n), r:=R*COS(180/n), r:=SQR(A*n*TAN(180/n))
// 3. Circumradius (Distance from center to a vertex) (R); R:=1/2*a*CSC(180/n), R:=r*SEC(180/n), R:=SQRT(2*A/(n*SIN(360/n)))
// 4. Area (A); A:=1/4*n*a^2*COT(180/n), A:=n*r^2*TAN(180/n), A:=1/2*n*R^2*SIN(180/n)
// 5. Perimeter (p); p:=n*a, p:=2*n*R*sin(180/n)
//
// Provide the number of sides (n), select one of the unknowns (a,r,R,A), and it's dimension.
EXPORT RegPolygon()
BEGIN
LOCAL a,r,p,R,A;
LOCAL n,k,Sidelength,Apothem,Circumradius,Area,Perimeter; // n=number of sides, k=known parameter, cb(x)=checkboxes for (a,r,R,A,p)
LOCAL white:=rgb(255,255,255),
black:=rgb(0,0,0),
red:=rgb(255,0,0),
green:=rgb(0,255,0),
blue:=rgb(0,0,255);
HAngle:=1;
input ({ {n, [0], {60,20,0}},
{k, [0], {60,20,1}},
{Perimeter, 4, {45,21,3}},
{Apothem, 4, {75,21,3}},
{Circumradius, 4, {45,21,4}},
{Area, 4, {75,21,4}},
{Sidelength, 4, {45,21,5}} },
"Regular Polygon Parameters",
{"Number of sides ", "(Check box) Dimension"},
{"How many sides?", "(Checked box) Value?"},
{6,5},
{6,5} );
IF Perimeter THEN
a:=k/n;r:=1/2*a*COT(180/n);R:=1/2*a*CSC(180/n);A:=1/4*n*a^2*COT(180/n);
END;
IF Sidelength THEN
a:=k;r:=1/2*a*COT(180/n);R:=1/2*a*CSC(180/n);A:=1/4*n*a^2*COT(180/n);
END; // Sidelength given
IF Apothem THEN
r:=k;a:=2*r*TAN(180/n);R:=r*SEC(180/n);A:=n*r^2*TAN(180/n);
END; // Inradius (Distance from center to Apothem (midpoint of side))given
IF Circumradius THEN
R:=k;a:=2*R*SIN(180/n);r:=R*COS(180/n);A:=1/2*n*R^2*SIN(360/n);
END; // Circumradius (Distance from center to vertex) given
IF Area THEN
A:=k;a:=SQRT((4*A*TAN(180/n))/n);r:=SQRT(A/(n*TAN(180/n)));R:=SQRT(2*A/(n*SIN(360/n)));
END; // Area given
p:=n*a; // Perimeter
// == Data display ==
DIMGROB_P(G1,320,240); // Define working space (full screen)
RECT(G1); // Clear working space
LINE_P(G1,160,120,100*COS(0)+160,100*SIN(0)+120,green); // Circum Radius line
ARC_P(G1,160,120,100,green); // Draw unit circumcircle (radius 100)
TEXTOUT_P("Circumradius = " + ROUND(R,2),G1,165,105,2,green); // Label Circumradius
LINE_P(G1,160,120,r/R*100*COS(180/n)+160,r/R*100*SIN(180/n)+120,blue); // Inradius (Apothem) line
ARC_P(G1,160,120,r/R*100,blue); // Draw unit Inircle (radius r/R*100)
TEXTOUT_P("Apothem = " + ROUND(r,2),G1,r/R*100*COS(180/n)+100,r/R*100*SIN(180/n)+120,2,blue); // Label Apothem radius
TEXTOUT_P("Number of Sides = " + n,G1,100,4); // Label Number of Sides
TEXTOUT_P("Area = " + ROUND(A,2),G1,120,220); // Label Number of Area
TEXTOUT_P("Side Length = " + ROUND(a,2),G1,200,r/R*100*SIN(180/n)+20,2,red); // Label Side Length
TEXTOUT_P("Ext. Angles = " + ROUND((180-360/n),1) + "°",G1,200,20,2,red); // Label Ext Angles
TEXTOUT_P("Int. Angles = " + ROUND((360/n),1) + "°",G1,r/R*100*COS(180/n)+20,r/R*100*SIN(180/n)+10,2,red); // Label Int Angles
TEXTOUT_P("Perimeter = " + ROUND(p,2),G1,10,25,2,red); // Label Number of Sides
FOR I from 0 to 360 STEP 360/n do
LINE_P(G1,100*COS(I)+160,100*SIN(I)+120,100*COS(I+360/n)+160,100*SIN(I+360/n)+120,red); // n Sides
END;
BLIT_P(G0,0,0,320,240,G1,0,0,320,240); // Transfer working space to display space
WAIT;
Print;
Print( " SUMMARY:" + CHAR(10) + CHAR(10) +
" Number of sides = " + n + CHAR(10) +
" Side length = " + ROUND(a,2) + CHAR(10) +
" Perimeter = " + ROUND(p,2) + CHAR(10) +
" Area = " + ROUND(A,2) + CHAR(10) +
CHAR(10) +
" Circumradius = " + ROUND(R,2) + CHAR(10) +
" Inradius (Apothem) = " + ROUND(r,2) + CHAR(10) +
CHAR(10) +
" Interior Angles = " + 360/n + "°" + CHAR(10) +
" Exterior Angles = " + (180-360/n) + "°" );
END;