Filling sourrounding cells in a 3D vector - vector

I need to fill a 3d vector in a way that, once x,y,z are fixed, the code has to write numbers in all the nearest neighbours (i.e. all the cells with x+/-1, y+/-1, z+/-1). Of course I don't want the cells outside the boundaries to be filler. So far I've tried the following approach
void setSphericalSource(int x, int y, int z, float sigma, vector<vector<vector<float>>>& vec, float maxAct) {
float total_activity = 0.0;
vec[z][y][x] = maxAct ; //hotspot
total_activity += maxAct;
if (x > 0 && x < vec[0][0].size() && y > 0 && y < vec[0].size() && z > 0 && z < vec.size()) {
//cells with face in common
vec[z][y][x + 1] = maxAct/2;
vec[z][y][x - 1] = maxAct/2;
vec[z][y + 1][x] = maxAct/2;
vec[z][y - 1][x] = maxAct/2;
vec[z + 1][y][x] = maxAct/2;
vec[z - 1][y][x] = maxAct/2;
//cells with edge in common
vec[z][y + 1][x + 1] = maxAct/3;
vec[z][y + 1][x - 1] = maxAct/3;
vec[z][y - 1][x + 1] = maxAct/3;
vec[z][y - 1][x - 1] = maxAct/3;
vec[z + 1][y][x + 1] = maxAct/3;
vec[z + 1][y][x - 1] = maxAct/3;
vec[z - 1][y][x + 1] = maxAct/3;
vec[z - 1][y][x - 1] = maxAct/3;
vec[z + 1][y + 1][x] = maxAct/3;
vec[z + 1][y - 1][x] = maxAct/3;
vec[z - 1][y + 1][x] = maxAct/3;
vec[z - 1][y - 1][x] = maxAct/3;
//cells with a vertex in common
vec[z + 1][y + 1][x + 1] = maxAct/4;
vec[z + 1][y + 1][x - 1] = maxAct/4;
vec[z + 1][y - 1][x - 1] = maxAct/4;
vec[z - 1][y - 1][x - 1] = maxAct/4;
vec[z + 1][y - 1][x + 1] = maxAct/4;
vec[z - 1][y + 1][x + 1] = maxAct/4;
vec[z - 1][y - 1][x + 1] = maxAct/4;
vec[z - 1][y + 1][x - 1] = maxAct/4;
}
}
In practice I first check if x,y,z belong to the inner part of the 3d vector, then I go to the external cells and so on until a spherical volume around the chosen center is filled (skipping the cells that exceed the boundaries).
The problem rises when I want to set a spherical source with the center on a face, on an edge or on a vertex of the 3D grid. Is there a better way than repeating the code above for each face, each vertex and each corner?

Is there a better way of repeating the code above for each face, each vertex and each corner?
I take you actually meant "Is there a better way than repeating the code above for each face, each vertex and each corner?" - Here is another way; it's up to the reader to judge whether it's better:
for (int k = -1; k <= 1; ++k)
for (int j = -1; j <= 1; ++j)
for (int i = -1; i <= 1; ++i)
{
int d = abs(i)+abs(j)+abs(k)+1;
x += i, y += j, z += k;
if (0 <= x && x < vec[0][0].size()
&& 0 <= y && y < vec[0] .size()
&& 0 <= z && z < vec .size()
) vec[z][y][x] = maxAct/d;
x -= i, y -= j, z -= k;
}

Related

Different colors plotting fixed points in NSolve

I am plotting fixed points of a system of differential equations in terms of a parameter. The code is
PX1Y1 = (1 - s1) (y1 + y2);
PX1Y2 = 0;
PX1X2 = 0;
PX2Y1 = 0;
PX2Y2 = s2 (y1 + y2);
PX2X1 = 0;
PY1X1 = s1 (x1 + x2);
PY1Y2 = 0;
PY1X2 = 0;
PY2X1 = 0;
PY2X2 = (1 - s2) (x1 + x2);
PY2Y1 = 0;
x1eq = -x1 (PX1Y1 + PX1Y2 + PX1X2) + x2 PX2X1 + y1 PY1X1 + y2 PY2X1;
x2eq = -x2 (PX2Y1 + PX2Y2 + PX2X1) + x1 PX1X2 + y1 PY1X2 + y2 PY2X2;
y1eq = -y1 (PY1X1 + PY1Y2 + PY1X2) + x1 PX1Y1 + x2 PX2Y1 + y2 PY2Y1;
y2eq = -y2 (PY2X1 + PY2X2 + PY2Y1) + x1 PX1Y2 + x2 PX2Y2 + y1 PY1Y2;
Xsimp[x1_, x2_, y1_, y2_] = Simplify[x1eq + x2eq]
Xeq = Simplify[
Xsimp[X/2 + \[Omega]/2,
X/2 - \[Omega]/2, (1 - X)/2 - (\[Omega] - \[Gamma])/2, (1 - X)/
2 + (\[Omega] - \[Gamma])/2]]
\[Omega]simp[x1_, x2_, y1_, y2_] = Simplify[x1eq - x2eq];
\[Omega]eq =
Simplify[\[Omega]simp[X/2 + \[Omega]/2,
X/2 - \[Omega]/2, (1 - X)/2 - (\[Omega] - \[Gamma])/2, (1 - X)/
2 + (\[Omega] - \[Gamma])/2]]
Manipulate[
Row[{
paramsplot = {s1 -> a, s2 -> b};
sol = NSolve[
{Xeq == 0, \[Omega]eq == 0} /. paramsplot,
{X, \[Omega]}, Reals
];
Plot[
X /. sol, {\[Gamma], -1, 1},
AxesLabel -> {"\[Gamma]", "X fixed points"},
ImageSize-> 300,
PlotRange -> {{-1, 1}, {0, 1}}
],
Plot[
\[Omega] /. sol, {\[Gamma], -1, 1},
AxesLabel -> {"\[Gamma]", "\[Omega] fixed points"},
ImageSize -> 300,
PlotRange -> {{-1, 1}, {-1, 1}}]
}],
{{a, 0.6,"s1 (0, 1)"}, 0, 1},
{{b, 0.4, "s2 (0, 1)"}, 0, 1}
]
And I get this
Is there any way of plotting each fixed point in a different colour? In the sense that what is green, so to say, in the X graph corresponds to what is green in the omega graph, and so on.
Thank you!

Given a position and rotation, how can I find a point that extends X distance from it?

I have a point in 3D space, and that point has an orientation. I want to get the end point of a line X distance from the origin point, following in orientation. How would I do this?
The answer doesn't have to be specific to any library, but I am using Three.JS.
In regards to Three.JS, depending on what you're starting with, or your comfort, there are several solutions.
Object3D
It provides you with extra utility methods that lets Three.js figure out the math for you:
// Create an Object3D
const element = new THREE.Object3D();
// Set position and orientation
element.position.copy(startPosition);
element.rotation.copy(eulerRotations);
// Move "forward" by the desired distance
element.translateZ(distance);
// Now we have our final position!
console.log(element.position);
I think the key to your question is the Object3D.translateZ() method, you can read more about it in the docs.
Vector3
Internally, what Object3D just did was Vector3 math. If you're only dealing with points and orientations, it might make more sense to use Vector3 directly:
const finalPosition = new Vector3(0, 0, 1)
.applyQuaternion(quaternionRotations)
.multiplyScalar(distance)
.add(startPosition);
Math
If you only want the math, this is what Three.JS is doing under the hood:
let x = 0;
let y = 0;
let z = 1;
const ix = quaternionW * x + quaternionY * z - quaternionZ * y;
const iy = quaternionW * y + quaternionZ * x - quaternionX * z;
const iz = quaternionW * z + quaternionX * y - quaternionY * x;
const iw = - quaternionX * x - quaternionY * y - quaternionZ * z;
x = ix * quaternionW + iw * - quaternionX + iy * - quaternionZ - iz * - quaternionY;
y = iy * quaternionW + iw * - quaternionY + iz * - quaternionX - ix * - quaternionZ;
z = iz * quaternionW + iw * - quaternionZ + ix * - quaternionY - iy * - quaternionX;
x = x * distance + originalPositionX;
y = y * distance + originalPositionY;
z = z * distance + originalPositionZ;
Which can be simplified to this:
function ray(position, distance, direction) {
const dy2 = 2 * direction.y;
const dx2 = 2 * direction.x;
const x = position.x + distance * (dy2 * direction.w + dx2 * direction.z);
const y = position.y + distance * (dy2 * direction.z - dx2 * direction.w);
const z =
position.z +
distance *
(-1 * Math.pow(direction.y, 2) +
Math.pow(direction.z, 2) +
Math.pow(direction.w, 2) -
Math.pow(direction.x, 2));
return {x, y, z};
}

Finding RuntimeWarning: overflow encountered in double_scalars while running numerical schemes for fluid dynamic study

i'm writing a code to solve Hyperbolic differential equations with different numerica methods such as Lax-Friederichs, Lax-Wendroff and Upwind scheme. During the calculation i often obtain this type of error:
RuntimeWarning: overflow encountered in double_scalars
that seems to disappear when i reduce the dimensions of matrix. Here i attach my code:
for i in range (0,nt):
#inlet
rho[0,i] = P_inlet/(R*T_inlet)
u[0,i] = u_inlet
P[0,i] = P_inlet
T[0,i] = T_inlet
Ac[0,0] = A_var_list[0]
Q1[0,i] = rho[0,i]
Q2[0,i] = rho[0,i] * u[0,i]
Q3[0,i] = (1/2)*(rho[0,i])*(u[0,i]**2) + (P[0,i]/(k-1))
F1[0,i] = rho[0,i] * u[0,i]
F2[0,i] = (1/2)*(rho[0,i])*(u[0,i]**2) + P[0,i]
F3[0,i] = u[0,i] * ((1/2)*(rho[0,i])*(u[0,i]**2) + (k*P[0,i]/(k-1)))
#outlet
rho[nx-1,i] = rho_outlet
P[nx-1,i] = P_outlet
u[nx-1,i] = u_outlet
T[nx-1,i] = T_outlet
Q1[nx-1,i] = rho[nx-1,i]
Q2[nx-1,i] = rho[nx-1,i]*u[nx-1,i]
Q3[nx-1,i] = (1/2)*rho[nx-1,i]*u[nx-1,i] + (P[nx-1,i]/(k-1))
F1[nx-1,i] = rho[nx-1,i] * u[nx-1,i]
F2[nx-1,i] = (1/2)*rho[nx-1,i]*(u[nx-1,i]**2) + P[nx-1,i]
F3[nx-1,i] = u[nx-1,i] * ((1/2)*(rho[nx-1,i])*(u[nx-1,i]**2) + (k*P[nx-1,i]/(k-1)))
#manifold
for i in range (1,nx-1):
rho[i,0] = P_inlet/(R*Tw[i])
u[i,0] = u_inlet
P[i,0] = P_inlet
Ac[i,0] = A_var_list[i]
Q1[i,0] = rho[i,0]
Q2[i,0] = rho[i,0] * u[i,0]
Q3[i,0] = (1 / 2) * (rho[i,0]) * (u[i,0] ** 2) + (P[i,0] / (k - 1))
F1[i, 0] = rho[i, 0] * u[i, 0]
F2[i, 0] = (1 / 2) * (rho[i, 0]) * (u[i, 0] ** 2) + P[i, 0]
F3[i, 0] = u[i, 0] * ((1 / 2) * (rho[i, 0]) * (u[i, 0] ** 2) + (k * P[i, 0] / (k - 1)))
S1[i, 0] = -rho[i, 0] * u[i, 0] * (Ac[i, 0] - Ac[i - 1, 0])
S2[i, 0] = -(rho[i, 0] * ((u[i, 0] ** 2) / (Ac[i, 0])) * (Ac[i, 0] - Ac[i - 1, 0])) - (
(frict_fact * np.pi * rho[i, 0] * d[i] * u[i, 0] ** 2) / (2 * Ac[i, 0]))
S3[i, 0] = - (u[i, 0] * (rho[i, 0] * ((u[i, 0] ** 2) / 2) + (k * P[i, 0] / (k - 1))) * (
(Ac[i, 0] - Ac[i - 1, 0]) / Ac[i, 0])) + (Lambda * np.pi * d[i] * (Tw[i] - T[i, 0]) / Ac[i, 0])
def Upwind():
for n in range (0,nt-1):
for i in range (1,nx):
Q1[i,n+1] = Q1[i-1,n]-((F1[i,n] - F1[i-1,n])/Dx)*Dt + (S1[i,n]-S1[i-1,n])*Dt
Q2[i, n + 1] = Q2[i-1, n] - ((F2[i, n] - F2[i - 1, n]) / Dx) * Dt + (S2[i, n] - S2[i - 1, n]) * Dt
Q3[i, n + 1] = Q3[i-1, n] - ((F3[i, n] - F3[i - 1, n]) / Dx) * Dt + (S3[i, n] - S3[i - 1, n]) * Dt
rho[i, n+1] = Q1[i, n+1]
u[i, n+1] = Q2[i, n+1] / rho[i, n+1]
P[i, n+1] = (Q3[i, n+1] - 0.5 * rho[i, n+1] * u[i, n+1] ** 2) * (k - 1)
T[i, n+1] = P[i, n+1] / (R * rho[i, n+1])
F1[i,n+1] = Q2[i,n+1]
F2[i,n+1] = rho[i,n+1]*((u[i,n+1]**2)/2) +P[i,n+1]
F3[i, n + 1] = u[i, n + 1] * (
(rho[i, n + 1] * ((u[i, n + 1] ** 2) / 2)) + (k * P[i , n + 1] / (k - 1)))
S1[i, n + 1] = -rho[i, n + 1] * u[i, n + 1] * (Ac[i, 0] - Ac[i-1, 0])
S2[i, n + 1] = - (rho[i, n + 1] * (
(u[i, n + 1] ** 2) / (Ac[i, 0])) * (Ac[i, 0] - Ac[i-1, 0])) - ((
(frict_fact * np.pi * rho[i, n + 1] * d[i] * (u[i, n + 1] ** 2)) / (2 * Ac[i, 0])))
S3[i, n + 1] = -(u[i, n + 1] * (
rho[i, n + 1] * ((u[i, n + 1] ** 2) / 2) + (k * P[i, n + 1] / (k - 1))) * (
(Ac[i , 0] - Ac[i-1, 0]) / Ac[i, 0])) + (
Lambda * np.pi * d[i ] * (Tw[i] - T[i, 0]) / Ac[i, 0])
plt.figure(1)
plt.plot(P[:, nt - 1])
plt.figure(2)
plt.plot(u[:, nt - 1])
def Lax_Friedrichs():
for n in range (1,nt):
for i in range (1,nx-1):
F1_m1 = 0.5 * (F1[i, n - 1] + F1[i - 1, n - 1])
F2_m1 = 0.5 * (F2[i, n - 1] + F2[i - 1, n - 1])
F3_m1 = 0.5 * (F3[i, n - 1] + F3[i - 1, n - 1])
S1_m1 = 0.5 * (S1[i, 0] + S1[i - 1, 0])
S2_m1 = 0.5 * (S2[i, 0] + S2[i - 1, 0])
S3_m1 = 0.5 * (S3[i, 0] + S3[i - 1, 0])
F1_p1 = 0.5 * (F1[i + 1, n - 1] + F1[i, n - 1])
F2_p1 = 0.5 * (F2[i + 1, n - 1] + F2[i, n - 1])
F3_p1 = 0.5 * (F3[i + 1, n - 1] + F3[i, n - 1])
S1_p1 = 0.5 * (S1[i + 1, n - 1] + S1[i, n - 1])
S2_p1 = 0.5 * (S2[i + 1, n - 1] + S2[i, n - 1])
S3_p1 = 0.5 * (S3[i + 1, n - 1] + S3[i, n - 1])
Q1[i, n] = 0.5 * (Q1[i - 1, n - 1] + Q1[i + 1, n - 1]) - Dt/Dx * (F1_p1 - F1_m1) + (S1_p1 - S1_m1) * Dt
Q2[i, n] = 0.5 * (Q2[i - 1, n - 1] + Q2[i + 1, n - 1]) - Dt/Dx * (F2_p1 - F2_m1) + (S2_p1 - S2_m1) * Dt
Q3[i, n] = 0.5 * (Q3[i - 1, n - 1] + Q3[i + 1, n - 1]) - Dt/Dx * (F3_p1 - F3_m1) + (S3_p1 - S3_m1) * Dt
rho[i, n] = Q1[i, n]
u[i, n] = Q2[i, n] / rho[i, n]
P[i, n] = (Q3[i, n] - 0.5 * rho[i, n] * u[i, n] ** 2) * (k - 1)
T[i, n] = P[i, n] / (R * rho[i, n])
F1[i, n] = Q2[i, n]
F2[i, n] = rho[i, n] * ((u[i, n] ** 2) / 2) + P[i, n]
F3[i, n] = u[i, n] * (
(rho[i, n] * ((u[i, n] ** 2) / 2)) + (k * P[i, n] / (k - 1)))
S1[i, n] = -rho[i, n] * u[i, n] * (Ac[i, 0] - Ac[i - 1, 0])
S2[i, n] = - (rho[i, n] * (
(u[i, n] ** 2) / (Ac[i, 0])) * (Ac[i, 0] - Ac[i - 1, 0])) - ((
(frict_fact * np.pi * rho[i, n] * d[i] * (u[i, n] ** 2)) / (2 * Ac[i, 0])))
S3[i, n] = -(u[i, n] * (
rho[i, n] * ((u[i, n] ** 2) / 2) + (k * P[i, n] / (k - 1))) * (
(Ac[i, 0] - Ac[i - 1, 0]) / Ac[i, 0])) + (
Lambda * np.pi * d[i] * (Tw[i] - T[i, 0]) / Ac[i, 0])
# Plot
plt.figure(1)
plt.plot(P[:, nt - 1])
plt.figure(2)
plt.plot(u[:, nt - 1])
def Lax_Wendroff():
for n in range (0,nt-1):
for i in range (1,nx-1):
Q1_plus_half = (1 / 2) * (Q1[i, n] + Q1[i + 1, n]) - (Dt / (2 * Dx)) * (F1[i + 1, n] - F1[i, n]) + (
S1[i + 1, n] - S1[i, n]) * Dt
Q1_less_half = (1 / 2) * (Q1[i, n] + Q1[i - 1, n]) - (Dt / (2 * Dx)) * (F1[i, n] - F1[i - 1, n]) + (
S1[i, n] - S1[i - 1, n]) * Dt
Q2_plus_half = (1 / 2) * (Q2[i-1, n] + Q2[i + 1, n]) - (Dt / (2 * Dx)) * (F2[i + 1, n] - F2[i, n]) + (
S2[i + 1, n] - S2[i, n]) * Dt
Q2_less_half = (1 / 2) * (Q2[i, n] + Q2[i - 1, n]) - (Dt / (2 * Dx)) * (F2[i, n] - F2[i - 1, n]) + (
S2[i, n] - S2[i - 1, n]) * Dt
Q3_plus_half = (1 / 2) * (Q3[i, n] + Q3[i + 1, n]) - (Dt / (2 * Dx)) * (F3[i + 1, n] - F3[i, n]) + (
S3[i + 1, n] - S3[i, n]) * Dt
Q3_less_half = (1 / 2) * (Q3[i, n] + Q3[i - 1, n]) - (Dt / (2 * Dx)) * (F3[i, n] - F3[i - 1, n]) + (
S3[i, n] - S3[i - 1, n]) * Dt
rho_less_half = Q1_less_half
u_less_half = Q2_less_half / rho_less_half
P_less_half = (Q3_less_half - ((1 / 2) * rho_less_half * (u_less_half ** 2) / 2)) * (k - 1)
F1_less_half = rho_less_half * u_less_half
F2_less_half = rho_less_half * ((u_less_half ** 2) / 2) + P_less_half
F3_less_half = u_less_half * ((rho_less_half * ((u_less_half ** 2) / 2)) + (k * P_less_half / (k - 1)))
rho_plus_half = Q1_plus_half
u_plus_half = Q2_plus_half / rho_plus_half
P_plus_half = (Q3_plus_half - ((1 / 2) * rho_plus_half * (u_plus_half ** 2) / 2)) * (k - 1)
F1_plus_half = rho_plus_half * u_plus_half
F2_plus_half = rho_plus_half * ((u_plus_half ** 2) / 2) + P_plus_half
F3_plus_half = u_plus_half * ((rho_plus_half * ((u_plus_half ** 2) / 2)) + (k * P_plus_half / (k - 1)))
# I termini sorgente da mettere dentro l'equazione finale di Q li calcolo come medie delle variabili nel condotto
S1_less_half = 0.5 * (S1[i - 1, n] + S1[i, n])
S2_less_half = 0.5 * (S2[i - 1, n] + S2[i, n])
S3_less_half = 0.5 * (S3[i - 1, n] + S3[i, n])
S1_plus_half = 0.5 * (S1[i + 1, n] + S1[i, n])
S2_plus_half = 0.5 * (S2[i + 1, n] + S2[i, n])
S3_plus_half = 0.5 * (S3[i + 1, n] + S3[i, n])
"""S1_less_half = Q1_less_half + F1_less_half
S2_less_half = Q2_less_half + F2_less_half
S3_less_half = Q3_less_half + F3_less_half
S1_plus_half = Q1_plus_half + F1_plus_half
S2_plus_half = Q2_plus_half + F2_plus_half
S3_plus_half = Q3_plus_half + F3_plus_half"""
Q1[i , n + 1] = Q1[i, n] - (Dt / Dx) * (F1_plus_half - F1_less_half) - (S1_plus_half - S1_less_half) * Dt
Q2[i, n + 1] = Q2[i, n] - (Dt / Dx) * (F2_plus_half - F2_less_half) - (S2_plus_half - S2_less_half) * Dt
Q3[i, n + 1] = Q3[i, n] - (Dt / Dx) * (F3_plus_half - F3_less_half) - (S3_plus_half - S3_less_half) * Dt
rho[i, n + 1] = Q1[i, n + 1]
u[i, n + 1] = Q2[i, n + 1] / rho[i, n + 1]
P[i, n + 1] = (Q3[i, n + 1] - 0.5 * rho[i, n + 1] * (u[i, n + 1] ** 2)) * (k - 1)
F1[i, n + 1] = rho[i, n + 1] * u[i, n + 1]
F2[i, n + 1] = rho[i, n + 1] * ((u[i, n + 1] ** 2) / 2) + P[i, n + 1]
F3[i, n+1] = u[i, n+1] * (
(rho[i, n+1] * ((u[i, n+1] ** 2) / 2)) + (k * P[i, n+1] / (k - 1)))
S1[i, n+1] = -rho[i, n+1] * u[i, n+1] * (Ac[i, 0] - Ac[i - 1, 0])
S2[i, n+1] = - (rho[i, n+1] * (
(u[i, n+1] ** 2) / (Ac[i, 0])) * (Ac[i, 0] - Ac[i - 1, 0])) - ((
(frict_fact * np.pi * rho[i, n+1] * d[i] * (u[i, n+1] ** 2)) / (2 * Ac[i, 0])))
S3[i, n+1] = -(u[i, n+1] * (
rho[i, n+1] * ((u[i, n+1] ** 2) / 2) + (k * P[i, n+1] / (k - 1))) * (
(Ac[i, 0] - Ac[i - 1, 0]) / Ac[i, 0])) + (
Lambda * np.pi * d[i] * (Tw[i] - T[i, 0]) / Ac[i, 0])
# Plot
plt.figure(1)
plt.plot(P[:, nt - 1])
plt.figure(2)
plt.plot(u[:, nt - 1])
I'm pretty sure that's a matter of indices but i havent't found the solution yet. Hope you can help me.

Integrating Norm of vectors

I have two vectors which I want to integrate in Matematica. Let the vectors be
r = {x, y};
Q = {x1, y1};
then I write this command
Integrate[
1/Norm[-((a*Q)/c) + r],
{a, 0, 1},
Assumptions -> (a*x1)/c > x && x ->
Real && (a*x1)/c ->
Real && x > 0 && (a*y1)/c -> Real && (a*y1)/c > y && y > 0
]
Where c is a positive constant. The output yields the same
Integrate[1/Norm[-((a Q)/c) + r], {a, 0, 1},
Assumptions -> (a x1)/c > 0 && (a x1)/c > x && x ->
Real && (a x1)/c -> Real && x > 0 && (a y1)/c > y && y > 0]
Could you please tell me where I am making a mistake?
I would be grateful if you could help me,
Thanks
r = {x, y};
Q = {x1, y1};
Integrate[1/Sqrt[(-((a*Q)/c) + r).(-((a*Q)/c) + r)], {a, 0, 1},
Assumptions -> Element[{x, y, x1, y1, a, c}, Reals]]
Returns:
(*
(1/Sqrt[x1^2 + y1^2])c (-Log[c (-x x1 - y y1 +Sqrt[(x^2 + y^2) (x1^2 + y1^2)])]+
Log[x1^2 + y1^2 - c (x x1 + y y1) +
(c Sqrt[(x1^2 + y1^2) (x1^2 + c^2 (x^2 + y^2) + y1^2 - 2 c (x x1 + y y1))])/
Abs[c]])
*)

Google Maps API V3 - Polygon SMOOTHED edges

Is it possible to smooth the lines/edges for a polygon? It's currently very sharp and angular and it would be great if those angles actually had curvature to them. Any ideas?
Add additional points into your polygon. The more points that are plotted, the more gradual the curve will be.
Here is a smoothing algorithm based on bSpline that worked for me on Android inspired by https://johan.karlsteen.com/2011/07/30/improving-google-maps-polygons-with-b-splines/
public List<LatLng> bspline(List<LatLng> poly) {
if (poly.get(0).latitude != poly.get(poly.size()-1).latitude || poly.get(0).longitude != poly.get(poly.size()-1).longitude){
poly.add(new LatLng(poly.get(0).latitude,poly.get(0).longitude));
}
else{
poly.remove(poly.size()-1);
}
poly.add(0,new LatLng(poly.get(poly.size()-1).latitude,poly.get(poly.size()-1).longitude));
poly.add(new LatLng(poly.get(1).latitude,poly.get(1).longitude));
Double[] lats = new Double[poly.size()];
Double[] lons = new Double[poly.size()];
for (int i=0;i<poly.size();i++){
lats[i] = poly.get(i).latitude;
lons[i] = poly.get(i).longitude;
}
double ax, ay, bx, by, cx, cy, dx, dy, lat, lon;
float t;
int i;
List<LatLng> points = new ArrayList<>();
// For every point
for (i = 2; i < lats.length - 2; i++) {
for (t = 0; t < 1; t += 0.2) {
ax = (-lats[i - 2] + 3 * lats[i - 1] - 3 * lats[i] + lats[i + 1]) / 6;
ay = (-lons[i - 2] + 3 * lons[i - 1] - 3 * lons[i] + lons[i + 1]) / 6;
bx = (lats[i - 2] - 2 * lats[i - 1] + lats[i]) / 2;
by = (lons[i - 2] - 2 * lons[i - 1] + lons[i]) / 2;
cx = (-lats[i - 2] + lats[i]) / 2;
cy = (-lons[i - 2] + lons[i]) / 2;
dx = (lats[i - 2] + 4 * lats[i - 1] + lats[i]) / 6;
dy = (lons[i - 2] + 4 * lons[i - 1] + lons[i]) / 6;
lat = ax * Math.pow(t + 0.1, 3) + bx * Math.pow(t + 0.1, 2) + cx * (t + 0.1) + dx;
lon = ay * Math.pow(t + 0.1, 3) + by * Math.pow(t + 0.1, 2) + cy * (t + 0.1) + dy;
points.add(new LatLng(lat, lon));
}
}
return points;
}

Resources