[Android Apps and Games] Tamasaka:
Polygons and stars

Tintinnuntius' "n-gon" and acadoid's included "Filled square" inspired me to code this script. Thank you.

This script draws a regular polygon ABC...n where A is the start point and B is the end point of selection box.

Parameter 1 is the number of corners.

Parameter 2 is to draw a n-pointed star. Larger number of this parameter makes thinner star.

Parameter 3 is the switch for filling.

Parameter 4 is the switch to snap start point (corner A) to grid. The grid has two types. Coarse grid is the same as the standard grid of acadoid's tools. Fine grid is 1/5 of the coarse grid.

Parameter 5 is the switch to snap end point (corner B) to grid. Coarse grid is same as that for start point. Fine grid is 1/5 of the coarse grid based on the start point (cornar A).

Snapping selection has 9 patterns (3 × 3) in which the pattern "free start point and fine gridded end point" gives easily the same size and the same oriented polygon at any position.

Unfortunately it is not so useful in organic chemistry yet.;)

/* Polygon ver. 1.05 */
corners = 5;
star = 0;
fill = 0;
snapstart = 0;
snapend = 0;
if (LN.getNumberOfParameters() >= 1)
corners = LN.getParameter(1); /* Number of corners, 3 ≤ Parameter(1) ≤ 12, 10 steps */
if (LN.getNumberOfParameters() >= 2)
star = LN.getParameter(2); /* Star, 0 ≤ Parameter(2) ≤ 3, 4 steps */
if (LN.getNumberOfParameters() >= 3)
fill = LN.getParameter(3); /* Fill (0:open, 1:filled), 0 ≤ Parameter(3) ≤ 1, 2 steps */
if (LN.getNumberOfParameters() >= 4)
snapstart = LN.getParameter(4); /* Snap start point to grid (0:free, 1:fine, 2:coarse), 0 ≤ Parameter(4) ≤ 2, 3 steps */
if (LN.getNumberOfParameters() >= 5)
snapend = LN.getParameter(5); /* Snap end point to grid (0:free, 1:fine, 2:coarse), 0 ≤ Parameter(5) ≤ 2, 3 steps */

x1 = LN.getX1();
y1 = LN.getY1();
x2 = LN.getX2();
y2 = LN.getY2();

if (snapstart > 0 || snapend > 0) {
width = LN.getWidth();
scale = LN.getScale();
step = width / 150 + scale * width / 25;
finestep = step / 5;
if (snapstart == 1) {
x1 = finestep/2 + Math.floor((x1 - finestep/2) / finestep + 0.5) * finestep;
y1 = finestep/2 + Math.floor((y1 - finestep/2) / finestep + 0.5) * finestep;
}
else if (snapstart == 2) {
x1 = step/2 + Math.floor((x1 - step/2) / step + 0.5) * step;
y1 = step/2 + Math.floor((y1 - step/2) / step + 0.5) * step;
}
if (snapend == 1) {
x2 = x1 + Math.floor((x2 - x1 - finestep/2) / finestep + 0.5) * finestep;
y2 = y1 + Math.floor((y2 - y1 - finestep/2) / finestep + 0.5) * finestep;
}
else if (snapend == 2) {
x2 = step/2 + Math.floor((x2 - step/2) / step + 0.5) * step;
y2 = step/2 + Math.floor((y2 - step/2) / step + 0.5) * step;
}
}

theta = Math.PI - 2 * Math.PI / corners;
side = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
direction = Math.atan2(y2 - y1, x2 - x1);
radius = side / 2 / Math.cos(theta/2);
x0 = x1 + radius * Math.cos(direction - theta/2);
y0 = y1 + radius * Math.sin(direction - theta/2);
phi1 = direction - theta/2 + Math.PI ;

LN.setClip(x0 - radius, y0 - radius, x0 + radius, y0 + radius);
for (xs = x1, ys = y1, i = 1; i <= corners; i++) {
xe = x0 + radius * Math.cos(phi1 - i * Math.PI * 2 / corners);
ye = y0 + radius * Math.sin(phi1 - i * Math.PI * 2 / corners);
xm = (star * x0 + xs + xe) / (star + 2);
ym = (star * y0 + ys + ye) / (star + 2);
if (fill) {
x0b = x0 - (xs + xe - 2 * x0) / 10;
y0b = y0 - (ys + ye - 2 * y0) / 10;
LN.drawFilledQuadrangle(x0b, y0b, xs, ys, xm, ym, xe, ye);
}
else {
LN.drawLine(xs, ys, xm, ym);
LN.drawLine(xm, ym, xe, ye);
}
xs = xe;
ys = ye;
}