问题
I'm trying to calculate 3D polygon's normal using Newell's approach. The problem I'm facing is that the value of z is always returned in positive even though the polygon is facing -z. However, when it's facing -z it also flips the values of x and y as well so if actual value of x is -x it'll be +x and same goes for the y. I can't figure out why that is happening. I hope someone can point out what I'm doing wrong. Here's what I wrote so far (using PHP):
$verticies =
[
[57.36, 30.98, 0.0],
[52.57, 39.04, 2.76],
[58.00, 38.33, 10.50],
[59.89, 31.16, 4.77],
[62.28, 30.75, 8.01],
[64.70, 26.11, 6.46],
[64.90, 21.54, 1.21]
];
for ($i = 0; $i < count($verticies); $i++)
{
//current vertex
$pi = $verticies[$i];
//next vertex
$pj = $verticies[($i+1) % count($verticies)];
//0 = x, 1 = y, 2 = z
$nx += ((($pi[2]) + ($pj[2])) * (($pj[1]) - ($pi[1])));
$ny += ((($pi[0]) + ($pj[0])) * (($pj[2]) - ($pi[2])));
$nz += ((($pi[1]) + ($pj[1])) * (($pj[0]) - ($pi[0])));
}
echo $nx.', '.$ny.', '.$nz;
//Current Result = -192.665, -145.6139, 115.1547
//Expected Result = -192.665, -145.6139, -115.1547
Any help would be appreciated. Thanks...
回答1:
This code gives 0, 2, -2
for rectangle
[
[0, 0, 0],
[1, 0, 0],
[1, 1, 1],
[0, 1, 1],
];
and 0, -2, 2
for rectangle with reversed vertex order, so I think code is right
来源:https://stackoverflow.com/questions/59124545/issue-with-calculating-3d-polygons-face-normal-using-newells-algorithm