def dot(A, B):
sum = 0
for a, b in zip(A, B):
sum += a * b
print "dot(%s, %s) = %d" % (A, B, sum)
return sum
def mul3x4(A, B):
out = [
[None for x in xrange(4)]
for y in xrange(3)]
for c in xrange(4):
B_col = [B[y][c] for y in xrange(3)]
for r, A_row in enumerate(A):
out[r][c] = dot(A_row, B_col)
return out
a = [
[2, -3, 1],
[1, 1, -1],
[-1, 1, -3]]
b = [
[ 2, 6, 0, 1],
[-1, 4, 1, 0],
[ 0, 5, -3, 0]]
print mul3x4(a, b)
python/수학/3x4 행렬 곱하기
Posted at 2010/01/30 19:45// Posted in python/pymathpython/샘플링된 정보를 이용해 중간 위치 예측하는 방정식 만들기
Posted at 2008/08/14 23:01// Posted in python/pymath
게임에서 선형 보간으로 하면 촌스러운 느낌이됩니다. ~(-_-)~
가속 감속을 주는 방정식을 만들고 싶을때 사용하는
뉴턴 제차분을 이용한 방정식 만들기 프로그램 입니다.
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 를 사용하는 것이 좋은듯 합니다.
가속 감속을 주는 방정식을 만들고 싶을때 사용하는
뉴턴 제차분을 이용한 방정식 만들기 프로그램 입니다.
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 를 사용하는 것이 좋은듯 합니다.
python/파이썬에서 두점의 거리계산을 간단히!
Posted at 2008/08/14 17:04// Posted in python/pymath
게임을 만들다보면 두점 사이의 방향이나 거리를 구해야 하는 일이 많습니다.
이거 하나 하자고 벡터 클래스 만들기도 귀찮아서 아래와 같은 코드를 작성하곤 했는데...
속도는 둘째치고, 코드량의 압박이 장난이 아닙니다.
오늘 몬스터 날리기를 구현하던 도중 불현듯 재밌는 아이디어가 떠오르더군요
바로 복소수!! 입니다.
(x, y) 좌표 연산이나 복소수 연산이나 2차원 연산이라는 점에서 동일하거든요
와하하 너무 좋아요
이거 하나 하자고 벡터 클래스 만들기도 귀찮아서 아래와 같은 코드를 작성하곤 했는데...
import math
src = (sx, sy)
dst = (dx, dy)
scale = 2.0
delta = dst[0] - src[0], dst[1] - src[0]
deltaLen = math.sqrt(delta[0] * delta[0], delta[1] * delta[1])
deltaDir = (delta[0] / deltaLen, delta[1] / deltaLen)
newPos = deltaDir[0] * scale, deltaDir[1] * scale
속도는 둘째치고, 코드량의 압박이 장난이 아닙니다.
오늘 몬스터 날리기를 구현하던 도중 불현듯 재밌는 아이디어가 떠오르더군요
바로 복소수!! 입니다.
(x, y) 좌표 연산이나 복소수 연산이나 2차원 연산이라는 점에서 동일하거든요
짠 ~
src = (sx, sy)
dst = (dx, dy)
scale = 2.0
delta = complex(*dst) - complex(*src)
deltaLen = abs(delta)
deltaDir = delta / abs(delta)
newPos = delta / abs(delta) * scale
와하하 너무 좋아요


python 을 좋아하는 게임 프로그래머