利用者:Ideasman42/Math

提供: wiki
移動先: 案内検索

Hex Root

A fun one, this is the 'hex', like the square, if you have a hexagon this is how to know the total number of hexagons in the center from the number on 1 side as well as the hex root.

import math

def hex(x):
    a = (((x - 1.0) * 3.0) * x) + 1.0
    return a

def hex_root(a):
    x = (3.0 + math.sqrt(9.0 - (12.0 * (1.0 - a)))) / 6.0
    return x

or more info http://en.wikipedia.org/wiki/Centered_hexagonal_number

Align Matrix Axis

Used this to create all possible axis flipping for blender python.

from mathutils import Vector, Matrix

def align_matrix(mat, axis, vec, eps=0.000001):
    if axis == 'X':
        ori = mat[2].copy()
        if abs(vec.dot(ori)) > (1.0 - eps):
            ori = mat[1].copy()
        x = vec
        y = ori.cross(x)
        z = x.cross(y)
    elif axis == 'Y':
        ori = mat[0].copy()
        if abs(vec.dot(ori)) > (1.0 - eps):
            ori = mat[2].copy()
        y = vec
        z = ori.cross(y)
        x = y.cross(z)
    elif axis == 'Z':
        ori = mat[1].copy()
        if abs(vec.dot(ori)) > (1.0 - eps):
            ori = mat[0].copy()
        z = vec
        x = ori.cross(z)
        y = z.cross(x)

    mat[0] = x.normalized()
    mat[1] = y.normalized()
    mat[2] = z.normalized()


def world_transform(axis, up):
    
    axis_vec = Vector()
    up_vec = Vector()

    axis_vec[ord(axis[-1]) - ord('X')] = -1.0 if axis.startswith('-') else 1.0
    up_vec[ord(up[-1]) - ord('X')] = -1.0 if up.startswith('-') else 1.0

    mat = Matrix()
    mat = mat.to_3x3()
    mat.identity()

    align_matrix(mat, 'Y', up_vec)
    align_matrix(mat, 'Z', axis_vec)

    # print(mat)
    return mat


import bpy
# bpy.context.object.matrix_world = world_transform('Z', '-X').to_4x4()
opts = ('X', 'Y', 'Z', '-X', '-Y', '-Z')

for a in opts:
    for b in opts:
        if a[-1] != b[-1]:
            mat = world_transform(a,  b)
            exp = tuple([tuple([j for j in k]) for k in mat])
            print("%s: %s," % ((a, b), exp))