| 369 https://math.stackexchange.com/a/476311 |
369 https://math.stackexchange.com/a/476311 |
| 370 ''' |
370 ''' |
| 371 v1, v2, v3 = polygon.vertices[:3] |
371 v1, v2, v3 = polygon.vertices[:3] |
| 372 a, b = v3 - v2, v1 - v2 |
372 a, b = v3 - v2, v1 - v2 |
| 373 normal = cross_product(a, b).normalized() |
373 normal = cross_product(a, b).normalized() |
| 374 v = cross_product(normal, Vector(0, 0, 1)) |
|
| 375 cosine = dot_product(normal, Vector(0, 0, 1)) |
|
| 376 v_matrix = Matrix3x3([ |
374 v_matrix = Matrix3x3([ |
| 377 0, -v.z, v.y, |
375 0, 0, -normal.x, |
| 378 v.z, 0, -v.x, |
376 0, 0, -normal.y, |
| 379 -v.y, v.x, 0, |
377 normal.x, normal.y, 0, |
| 380 ]) |
378 ]) |
| 381 try: |
379 try: |
| 382 rotation_matrix = Matrix3x3() |
380 rotation_matrix = Matrix3x3() |
| 383 rotation_matrix += v_matrix |
381 rotation_matrix += v_matrix |
| 384 rotation_matrix += (v_matrix @ v_matrix) * (1 / (1 + cosine)) |
382 rotation_matrix += (v_matrix @ v_matrix) * (1 / (1 + normal.z)) |
| 385 except ZeroDivisionError: |
383 except ZeroDivisionError: |
| 386 rotation_matrix = Matrix3x3() |
384 rotation_matrix = Matrix3x3() |
| 387 full_matrix = complete_matrix(rotation_matrix, Vertex(0, 0, 0)) |
385 full_matrix = complete_matrix(rotation_matrix, Vertex(0, 0, 0)) |
| 388 return Polygon([ |
386 return Polygon([ |
| 389 transform(vertex = vertex, transformation_matrix = full_matrix) |
387 transform(vertex = vertex, transformation_matrix = full_matrix) |