[Android Apps and Games] Tamasaka:
p orbital

This script draws a 3D model of 2p orbital for chemistry. The lobe is not a real wave function, but a pseudo model for education.

Parameter 1 is size of lobe in arbitrary unit.

Parameter 2 selects color of front lobe from -9 to 9. 0 is black. 1 is white. 2 is light gray. 3 is dark gray. 4 is pale cyan. 5 is light yellow. The lobes of these colors are outlined by black and slightly transparent. 6 is light gray, 7 is dark gray, 8 is pale cyan and 9 is light yellow without outline. Color of negative number (from -1 to -9) fills the lobe with opaque color.

Parameter 3 selects color of back lobe from -9 to 9.

Parameter 4 is fatness of lobe in arbitrary unit. The default value is 0.5. Adequate value is from 0.4 to 1.

Parameter 5 is length of lobe in arbitrary unit. The default value is 0.8. Adequate value is from 0.4 to 1. The combination of fatness and length makes the shape of the lobe.

Parameter 6 is resolution of lobe. The lobe is drawn by superimposed filled circles. The default value of resolution is 12.

The center of the orbital i.e. the position of nucleus is snapped to the acadoid's standard grid. The direction of lobe is snapped to every 5 degrees. We can superimpose this tool to other graph paper tools or axis tools. When you place the orbital free from the grid, please use the ‘Cutter’ and paste.

Do not choice ‘Fast custom drawing tools rendering’ in ‘Settings’.

/* p orbital ver. 1.04 */
size = 2;
fcolor = 1;
bcolor = 3;
fatness = 0.5;
length = 0.8;
resolution = 12;

if (LN.getNumberOfParameters() >= 1)
size = LN.getParameter(1); /* Size, 1.0 ≤ Parameter(1) ≤ 3.0, 5 steps */
if (LN.getNumberOfParameters() >= 2)
fcolor = LN.getParameter(2); /* Color, front lobe (-9to9; 1:w, 2:lg, 3:dg, 4:c, 5:y), -9 ≤ Parameter(2) ≤ 9, 19 steps */
if (LN.getNumberOfParameters() >= 3)
bcolor = LN.getParameter(3); /* Color, back lobe (neg:opaque, ≥6:w/o outline), -9 ≤ Parameter(3) ≤ 9, 19 steps */
if (LN.getNumberOfParameters() >= 4)
fatness = LN.getParameter(4); /* Fatness (0.4 to 1.0), 0.4 ≤ Parameter(4) ≤ 1.0, 7 steps */
if (LN.getNumberOfParameters() >= 5)
length = LN.getParameter(5); /* Length (0.4 to 1.0), 0.4 ≤ Parameter(5) ≤ 1.0, 7 steps */
if (LN.getNumberOfParameters() >= 6)
resolution = LN.getParameter(6); /* Resolution, 5 ≤ Parameter(6) ≤ 20, 16 steps */

width = LN.getWidth();
scale = LN.getScale();
step = width / 150 + scale * width / 25;
finestep = step / 10;
astep = Math.PI / 36;
lobesize = size * step;
radius = lobesize * 2 / (3 * Math.sqrt(3));
area = radius * fatness + lobesize * length;

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

x1 = step/2 + Math.floor((x1 - step/2) / step + 0.5) * step;
y1 = step/2 + Math.floor((y1 - step/2) / step + 0.5) * step;
angle = Math.atan2(y2 - y1, x2 - x1);
ag = Math.floor(angle / astep + 0.5) * astep;
distance = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
dt = Math.floor(distance / finestep + 0.5) * finestep;
dg = Math.min(dt, lobesize);
xp = dg * Math.cos(ag);
yp = dg * Math.sin(ag);

LN.setClip(x1 - area, y1 - area, x1 + area, y1 + area);
LN.setSoftness(1);
drawColorLobe(x1, y1, -xp, -yp, lobesize, fatness, length, resolution, bcolor);
drawColorLobe(x1, y1, xp, yp, lobesize, fatness, length, resolution, fcolor);

function drawColorLobe (x0, y0, xp, yp, lsize, fat, len, res, col) {
if (col < 0) {
opaque = true;
col = -col;
}
else
opaque = false;
if (col >= 6) {
outline = false;
col = col - 4;
}
else
outline = true;
setLobeColor(col);
if (outline)
LN.setColor(0, 0, 0);
if (! opaque)
LN.setAlpha(0.7);
drawLobe(x0, y0, xp, yp, lsize, fat, len, res);
if (outline && col >= 1) {
setLobeColor(col);
if (! opaque) {
if (col == 1)
LN.setAlpha(0);
else
LN.setAlpha(0.7);
}
drawLobe(x0, y0, xp, yp, lsize, fat * 0.95, len, res);
}
}

function drawLobe(x0, y0, xp, yp, ls, fa, le, re) {
dp = 0;
ps = ls / re;
xs = xp / re;
ys = yp / re;
fll = fa / ls / ls;
for (i = 1; i < re * le; i++) {
dp += ps;
r = Math.abs(dp * (dp - ls) * (dp + ls) * fll);
LN.drawFilledCircle(x0 += xs, y0 += ys, r);
}
}

function setLobeColor(co) {
switch (co) {
default:
case 0:
LN.setColor(0, 0, 0);
break;
case 1:
LN.setColor(1, 1, 1);
break;
case 2:
LN.setColor(0.95, 0.95, 0.95);
break;
case 3:
LN.setColor(0.8, 0.8, 0.8);
break;
case 4:
LN.setColor(0.8, 1, 1);
break;
case 5:
LN.setColor(1, 1, 0.7);
break;
}
}