Ejemplo n.º 1
0
    def test_mul_scale_3d(self):
        v = (1., 2., 3.)
        n = scale(v, 2)
        self.assertEqual(n, (2, 4, 6))

        n = scale(n, 1/2.)
        self.assertEqual(n, v)
Ejemplo n.º 2
0
    def test_mul_scale_2d(self):
        v = (4, 5)
        n = scale(v, 1.23)
        self.assertEqual(n, (4.92, 6.15))

        n = scale(n, 1 / 1.23)
        self.assertEqual(n, v)
Ejemplo n.º 3
0
    def test_mul_scale_2d(self):
        v = (4, 5)
        n = scale(v, 1.23)
        self.assertEqual(n, (4.92, 6.15))

        n = scale(n, 1/1.23)
        self.assertEqual(n, v)
Ejemplo n.º 4
0
    def test_mul_scale_3d(self):
        v = [1., 2., 3.]
        n = scale(v, 2)
        self.assertEqual(n, [2, 4, 6])

        n = scale(n, 1 / 2.)
        self.assertEqual(n, v)
Ejemplo n.º 5
0
    def test_mul_scale_2d(self):
        v = [4, 5]
        n = scale(v, 1.23)
        self.assertEqual(n, [4.92, 6.15])

        n = scale(n, 1 / 1.23)
        self.assertEqual(n, v)
Ejemplo n.º 6
0
    def test_mul_scale_3d(self):
        v = [1., 2., 3.]
        n = scale(v, 2)
        self.assertEqual(n, [2, 4, 6])

        n = scale(n, 1/2.)
        self.assertEqual(n, v)
Ejemplo n.º 7
0
    def test_mul_scale_3d(self):
        v = (1., 2., 3.)
        n = scale(v, 2)
        self.assertEqual(n, (2, 4, 6))

        n = scale(n, 1 / 2.)
        self.assertEqual(n, v)
Ejemplo n.º 8
0
    def test_mul_scale_2d(self):
        v = [4, 5]
        n = scale(v, 1.23)
        self.assertEqual(n, [4.92, 6.15])

        n = scale(n, 1/1.23)
        self.assertEqual(n, v)
Ejemplo n.º 9
0
 def bounce2(self, p2):
     """ More complicated bounce, should give better angles.
     Takes in the particle it is bouncing against and updates its speed too """
     
     avgSpeed = vector.scale(vector.absAdd(self.speed, p2.speed), 0.5) # Average speed
     # Find the normalised vector from p1 to p2
     n = vector.unit(vector.subtract(self.position, p2.position))
     
     self.speed = vector.multiply(n, avgSpeed)
     p2.speed = vector.scale(vector.multiply(n, avgSpeed), -1)
     
     self.limitSpeed()
     p2.limitSpeed()
Ejemplo n.º 10
0
    def bounce2(self, p2):
        """ More complicated bounce, should give better angles.
        Takes in the particle it is bouncing against and updates its speed too """

        avgSpeed = vector.scale(vector.absAdd(self.speed, p2.speed),
                                0.5)  # Average speed
        # Find the normalised vector from p1 to p2
        n = vector.unit(vector.subtract(self.position, p2.position))

        self.speed = vector.multiply(n, avgSpeed)
        p2.speed = vector.scale(vector.multiply(n, avgSpeed), -1)

        self.limitSpeed()
        p2.limitSpeed()
Ejemplo n.º 11
0
    def create_lr_matrix(self):
        """
    create the left and right gaussian matrix
        :param input_matrix: list of rows (lists of numbers)
        :return: dictionary with the keys "l_matrix" and "r_matrix" and the matrices (list of rows) as values
        """
        input_matrix = self._matrix
        l_matrix = create_standard(len(input_matrix))

        for i in range(len(input_matrix)):

            divisor = float(1) / input_matrix[i][i]
            input_matrix[i] = [divisor * j for j in input_matrix[i]]
            l_matrix[i][i] *= divisor

            for remaining_rows in range(i + 1, len(input_matrix)):
                scalar = float(
                    input_matrix[remaining_rows][i]) / input_matrix[i][i]
                input_matrix[remaining_rows] = \
                    vector.subtract(input_matrix[remaining_rows], vector.scale(scalar, input_matrix[i]))

                l_matrix[remaining_rows][i] -= scalar

        self._l_matrix = l_matrix
        self._r_matrix = input_matrix
Ejemplo n.º 12
0
def ray_colour(r, s):
    normal = sphere.normal(s, r[0])
    # calculate angle to ray
    dot = abs(vector.dot_product(r[1], normal) / 2.0)
    # apply specular
    dot = dot * s[3]
    # scale colour by the brightness
    colour = vector.scale(s[2], dot)
    # clamp to 255
    colour = [min(colour[0], 255), min(colour[1], 255), min(colour[2], 255)]
    return colour
Ejemplo n.º 13
0
def cast_ray(r):
    # walk the ray the distance to the nearest object
    start = r[0]
    for s in range(MAX_STEPS):
        if vector.length(vector.subtract(r[0], start)) > MAX_DISTANCE:
            break
        d, s = min_distance(r[0])
        if d < TOLERANCE:
            return ray_colour(r, s)
        r[0] = vector.add(r[0], vector.scale(r[1], d))
    return 0, 0, 0
Ejemplo n.º 14
0
def main():

    # create 3 random vectors
    v1 = [random.random(), random.random(), random.random()]
    v2 = [random.random(), random.random(), random.random()]
    v3 = [random.random(), random.random(), random.random()]

    # Print out the 3 vectors
    print("v1 = ", v1)
    print("v2 = ", v2)
    print("v3 = ", v3)

    #Print magnitude of 3 vectors
    print("v1 magnitude = ", vc.mod(v1))
    print("v2 magnitude = ", vc.mod(v2))
    print("v3 magnitude = ", vc.mod(v3))

    #print addition, dot & cross products of v1 and v2
    print("v1 + v2 =", vc.addv(v1, v2))
    print("v1 . v2 =", vc.dot_prod(v1, v2))
    print("v1 x v2 =", vc.crossprod(v1, v2))

    #vector identities
    #1
    identity1part1 = vc.crossprod(v1, v2)
    identity1part2 = vc.crossprod(vc.scale(v2, -1), v1)
    if vc.same(identity1part1, identity1part2) == True:
        print("Identity 1 is correct.")

        #2
    identity2part1 = vc.crossprod(v1, vc.addv(v2, v3))
    identity2part2 = vc.addv(vc.crossprod(v1, v2), vc.crossprod(v1, v3))
    if vc.same(identity2part1, identity2part2) == True:
        print("Identity 2 is correct.")

        #3
    identity3part1 = vc.crossprod(v1, vc.crossprod(v2, v3))
    identity3part2 = vc.subv((vc.scale(v2, vc.dot_prod(v1, v3))),
                             (vc.scale(v3, vc.dot_prod(v1, v2))))
    if vc.same(identity3part1, identity3part2) == True:
        print("Identity 3 is correct.")
Ejemplo n.º 15
0
    def test_vec_scale_2d(self):
        f = 2.5
        x, y = 4, 5
        v = Vector(x, y)
        d = Vector(f * x, f * y)

        n = v * f
        self.assertEqual(d, n)

        n = n * (1 / f)
        self.assertEqual(v, n)

        n = f * v
        self.assertEqual(d, n)

        n = (1 / f) * n
        self.assertEqual(v, n)

        n = scale(v, f)
        self.assertEqual(d, n)

        n = scale(n, (1 / f))
        self.assertEqual(v, n)
Ejemplo n.º 16
0
    def test_vec_scale_2d(self):
        f = 2.5
        x, y = 4, 5
        v = Vector(x, y)
        d = Vector(f * x, f * y)

        n = v * f
        self.assertEqual(d, n)

        n = n * (1/f)
        self.assertEqual(v, n)

        n = f * v
        self.assertEqual(d, n)

        n = (1/f) * n
        self.assertEqual(v, n)

        n = scale(v, f)
        self.assertEqual(d, n)

        n = scale(n, (1/f))
        self.assertEqual(v, n)
Ejemplo n.º 17
0
    def test_vec_scale_3d(self):
        f = 2
        x, y, z = 1., 2., 3.

        v = Vector(x, y, z)
        d = Vector(f * x, f * y, f * z)

        n = v * f
        self.assertEqual(d, n)

        n = n * (1. / f)
        self.assertEqual(v, n)

        n = f * v
        self.assertEqual(d, n)

        n = (1. / f) * n
        self.assertEqual(v, n)

        n = scale(v, f)
        self.assertEqual(d, n)

        n = scale(n, (1. / f))
        self.assertEqual(v, n)
Ejemplo n.º 18
0
    def test_vec_scale_3d(self):
        f = 2
        x, y, z = 1., 2., 3.

        v = Vector(x, y, z)
        d = Vector(f * x, f * y, f * z)

        n = v * f
        self.assertEqual(d, n)

        n = n * (1./f)
        self.assertEqual(v, n)

        n = f * v
        self.assertEqual(d, n)

        n = (1./f) * n
        self.assertEqual(v, n)

        n = scale(v, f)
        self.assertEqual(d, n)

        n = scale(n, (1./f))
        self.assertEqual(v, n)
Ejemplo n.º 19
0
def circleArcAroundPivot(pivot, start, end):
    pivotToStart = vector.translate(vector.minus(pivot))(start)
    pivotToEnd = vector.translate(vector.minus(pivot))(end)
    normalToPlane = vector.crossproduct(pivotToStart,pivotToEnd)
    firstRadialDirection = vector.crossproduct(normalToPlane, pivotToStart)
    
    planeCoefficients = pivotToEnd
    def dotByPlaneCoefficients(vec):
        output = 0
        for i in range(0,3):
            output += planeCoefficients[i] * vec[i]
        return output
    
    t = (dotByPlaneCoefficients(end) - dotByPlaneCoefficients(start))/(dotByPlaneCoefficients(firstRadialDirection))
    center = vector.translate(start)(vector.scale(firstRadialDirection,t))
    return circleArc(center, start, end)
Ejemplo n.º 20
0
def circleArc(center, start, end):
    radToStart = vector.translate(vector.minus(center))(start)
    radToEnd = vector.translate(vector.minus(center))(end)
    radiusSquared = vector.norm(radToStart)*vector.norm(radToEnd)
    radius = math.sqrt(radiusSquared)
    
    innerProductValue = vector.innerproduct(radToStart, radToEnd)
    angle = math.acos(innerProductValue/radiusSquared)
    
    firstUnitRadial = vector.unit(radToStart)
    secondUnitRadial = vector.unit(vector.translate(vector.scale(radToStart,-innerProductValue/radiusSquared))(radToEnd))
    
    def CN(t):
        point = vector.translate(center)(vector.translate(vector.scale(firstUnitRadial,radius*math.cos(t/radius)))(vector.scale(secondUnitRadial,radius*math.sin(t/radius))))
        tangent = vector.translate(vector.scale(firstUnitRadial,-math.sin(t/radius)))(vector.scale(secondUnitRadial,math.cos(t/radius)))
        output = [point,tangent]
        #output.append([math.cos(t/radius),math.sin(t/radius),0])
        return output

    return TwistingAxis(angle*radius,CN)
Ejemplo n.º 21
0
    def create_lr_matrix(self):
        """
    create the left and right gaussian matrix
        :param input_matrix: list of rows (lists of numbers)
        :return: dictionary with the keys "l_matrix" and "r_matrix" and the matrices (list of rows) as values
        """
        input_matrix = self._matrix
        l_matrix = create_standard(len(input_matrix))

        for i in range(len(input_matrix)):

            divisor = float(1) / input_matrix[i][i]
            input_matrix[i] = [divisor * j for j in input_matrix[i]]
            l_matrix[i][i] *= divisor

            for remaining_rows in range(i + 1, len(input_matrix)):
                scalar = float(input_matrix[remaining_rows][i]) / input_matrix[i][i]
                input_matrix[remaining_rows] = \
                    vector.subtract(input_matrix[remaining_rows], vector.scale(scalar, input_matrix[i]))

                l_matrix[remaining_rows][i] -= scalar

        self._l_matrix = l_matrix
        self._r_matrix = input_matrix
Ejemplo n.º 22
0
 def CN(t):
     point = vector.translate(center)(vector.translate(vector.scale(firstUnitRadial,radius*math.cos(t/radius)))(vector.scale(secondUnitRadial,radius*math.sin(t/radius))))
     tangent = vector.translate(vector.scale(firstUnitRadial,-math.sin(t/radius)))(vector.scale(secondUnitRadial,math.cos(t/radius)))
     output = [point,tangent]
     #output.append([math.cos(t/radius),math.sin(t/radius),0])
     return output
Ejemplo n.º 23
0
 def test_mul_scale_zero(self):
     v = [0] * 3
     n = scale(v, 2)
     self.assertEqual(n, v)
Ejemplo n.º 24
0
 def integrate(self, dt):
     self.acc = [0, 0]
     self.acc = vector.scale(self.force, 1 / self.mass)
     self.vel = vector.add_scaled(self.vel, self.acc, dt)
     self.pos = vector.add_scaled(self.pos, self.vel, dt)
     self.force = [0, 0]
Ejemplo n.º 25
0
 def test_vec_scale_zero(self):
     f = 2
     v = Vector(0, 0, 0)
     self.assertEqual(f * v, v)
     self.assertEqual(v * f, v)
     self.assertEqual(scale(v, f), v)
Ejemplo n.º 26
0
 def move(self, direction):
     """direction should be a 2D vector"""
     
     self.rect.center = add(self.rect.center, scale(direction, self.speed))
Ejemplo n.º 27
0
 def test_mul_scale_zero(self):
     v = (0, 0, 0)
     n = scale(v, 2)
     self.assertEqual(n, v)
Ejemplo n.º 28
0
 def test_vec_scale_zero(self):
     f = 2
     v = Vector(0, 0, 0)
     self.assertEqual(f * v, v)
     self.assertEqual(v * f, v)
     self.assertEqual(scale(v, f), v)
Ejemplo n.º 29
0
def cutSegment(start, end, startScale, endScale):
    segStart = vector.translate(start)(vector.scale(vector.translate(vector.minus(start))(end),startScale))
    segEnd = vector.translate(start)(vector.scale(vector.translate(vector.minus(start))(end),endScale))
    return segment(segStart, segEnd)
Ejemplo n.º 30
0
 def CN(t):
     output = [vector.translate(start)(vector.scale(tng,t)),tng]
     #output.append(normal)
     return output
Ejemplo n.º 31
0
 def findActualEndPoint(vec):
     unitVec = vector.unit(vec)
     return vector.translate(pivot)(vector.scale(vector.unit(vec),smallerNorm*smoothingFactor))
Ejemplo n.º 32
0
 def test_mul_scale_zero(self):
     v = [0] * 3
     n = scale(v, 2)
     self.assertEqual(n, v)
Ejemplo n.º 33
0
 def ev(self, t):
     return [vector.translate(vector.scale(self.tng,t))(self.start),self.tng,[0,0,0]]
Ejemplo n.º 34
0
 def reverse(self):
     l = self.length
     oldCN = self.CN
     self.CN = lambda t: [oldCN(l - t)[0], vector.scale(oldCN(1-t)[1],-1), oldCN(1-t)[2]]
Ejemplo n.º 35
0
    def move(self, direction):
        """direction should be a 2D vector"""

        self.rect.center = add(self.rect.center, scale(direction, self.speed))
Ejemplo n.º 36
0
 def test_mul_scale_zero(self):
     v = (0, 0, 0)
     n = scale(v, 2)
     self.assertEqual(n, v)