Alternatively, join us on Discord.

[Python 2.7] I made a vector library :3

Started by Thecoder1998, May 06, 2015, 01:46:11 pm

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Thecoder1998

May 06, 2015, 01:46:11 pm Last Edit: May 15, 2015, 09:46:48 am by Thecoder1998
hi guys, i made a vectory library in python :)
i'm looking for some more things to implement, maybe you guys could come up with some ideas

from numpy import matrix, arctan, arccos, sqrt
def vec(a, b, c = None, d = None):
    if c == None and d == None:
        return matrix('%s;%s' % (a, b))
    elif c != None and d == None:
        return matrix('%s;%s;%s' % (a, b, c))
    else:
        return matrix('%s;%s;%s;%s' % (a, b, c, d))
def dim(v):
    return v.shape[0]
def comp(v, a):
    return v.item(a - 1, 0)
def dot(v1, v2):
    if dim(v1) == dim(v2):
        dot = 0
        for i in range (dim(v1)):
            dot = dot + comp(v1, i) * comp(v2, i)
        return dot
    else:
        return "error"
def cross(v1, v2):
    if dim(v1) == 3 and dim(v2) == dim(v1):
        u1 = comp(v1, 1)
        u2 = comp(v1, 2)
        u3 = comp(v1, 3)
        w1 = comp(v2, 1)
        w2 = comp(v2, 2)
        w3 = comp(v2, 3)
        n1 = u2 * w3 - u3 * w2
        n2 = u3 * w1 - u1 * w3
        n3 = u1 * w2 - u2 * w1
        return vec(n1, n2, n3)
    else:
        return "error"
def stp(v1, v2, v3):
    if dim(v1) == dim(v2) and dim(v2) == dim(v3):
        return dot(v1, cross(v2, v3))
    else:
        return "error"
def vtp(v1, v2, v3):
    if dim(v1) == dim(v2) and dim(v2) == dim(v3):
        return cross(v1, cross(v2, v3))
    else:
        return "error"
def length(v):
    return sqrt(dot(v, v))
def angle(v1, v2):
    if dim(v1) == dim(v2):
        return arccos(dot(v1, v2) / (length(v1) * length(v2)))
    else:
        return "error"
def printv(v):
    if v != "error":
        if dim(v) == 2:
            a = comp(v, 1)
            b = comp(v, 2)
            vec = '(%s, %s)' % (a, b)
        if dim(v) == 3:
            a = comp(v, 1)
            b = comp(v, 2)
            c = comp(v, 3)
            vec = '(%s, %s, %s)' % (a, b, c)
        if dim(v) == 4:
            a = comp(v, 1)
            b = comp(v, 2)
            c = comp(v, 3)
            d = comp(v, 4)
            vec = '(%s, %s, %s, %s)' % (a, b, c, d)
        print vec
    else:
        print "error"
def printpol(v):
    if v != "error":
        if dim(v) == 2:
            x = comp(v, 1)
            y = comp(v, 2)
            r = length(v)
            theta = arctan(y / x)
            vec = matrix('%s;%s' % (r, theta))
        elif dim(v) == 3:
            x = comp(v, 1)
            y = comp(v, 2)
            z = comp(v, 3)
            r = length(v)
            theta = arctan(y / x)
            phi = arccos(z / r)
            vec = matrix('%s;%s;%s' % (r, theta, phi))
        printv(vec)
    else:
        print "error"

EDIT: Added the .py file so people can be lazy

brentmaas

Nice. Maybe you could add the .py file for download so other people can be lazy.
Lel I glitched Omni

Powered by EzPortal