게임에서 선형 보간으로 하면 촌스러운 느낌이됩니다. ~(-_-)~

가속 감속을 주는 방정식을 만들고 싶을때 사용하는
뉴턴 제차분을 이용한 방정식 만들기 프로그램 입니다.

def __newton_subcalc(sx, sy, ex, ey):
    return (ey - sy)/float(ex - sx)

def __newton_build(srcList, stack):
    """
    x   y
    1   11
    2   12  F[1,2] = (1, 11, 2, 12)
    3   13  F[2,3] = (2, 12, 3, 13) F[1,3] = (1, F[1,1], 3, F[2,3])
    """
    stack.append((srcList[0][2], srcList[-1][2]))

    if len(srcList) <= 1:
        return stack

    sx, mx, sy = srcList[0]
    dstList = []
    for mx, ex, ey in srcList[1:]:
        dstList.append((sx, ex, __newton_subcalc(sx, sy, ex, ey)))
        sx, sy = mx, ey
    return __newton_build(dstList, stack)

def newton_gen_polyf_items(srcList):
    srcList = [(cx, cx, cy) for cx, cy in srcList]

    stack = __newton_build(srcList, [])

    fc, bc = stack[0]
    fr = fc

    yield "%f" % (fr)

    count = 1
    for fc, bc in stack[1:]:
        cx, ct, cy = srcList[0]
        yield " * ".join(["%f * (x - %f)" % (fc, cx)] + ["(x - %f)" % (cx) for cx, ct, cy in srcList[1:count]])
        count += 1

def newton_gen_polyb_items(srcList):
    srcList = [(cx, cx, cy) for cx, cy in srcList]

    stack = __newton_build(srcList, [])

    revList = [] + srcList
    revList.reverse()

    fc, bc = stack[0]
    br = bc

    yield "%f" % (br)

    count = 1
    for fc, bc in stack[1:]:
        cx, ct, cy = revList[0]
        yield " * ".join(["%f * (x - %f)" % (bc, cx)] + ["(x - %f)" % (cx) for cx, ct, cy in revList[1:count]])
        count += 1

def newton_make_polyf(positions):
    return " + ".join((expr for expr in newton_gen_polyf_items(positions)))

def newton_make_polyb(positions):
    return " + ".join((expr for expr in newton_gen_polyb_items(positions)))

if __name__ == "__main__":
    positions = [
            (0.0,  0.3),
            (0.33, 0.7),
            (0.66, 0.9),
            (1.0,  1.0),
    ]

    print newton_make_polyf(positions)
    print newton_make_polyb(positions)

게임에서는 최종값이 중요하니 newton_make_polyb 를 사용하는 것이 좋은듯 합니다.
2008/08/14 23:01 2008/08/14 23:01
받은 트랙백이 없고, 댓글이 없습니다.

댓글+트랙백 RSS :: http://myevan.net/rss/response/117

댓글+트랙백 ATOM :: http://myevan.net/atom/response/117