How to use the vector.Vector function in vector

To help you get started, weโ€™ve selected a few vector examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github inasafe / inasafe / safe / storage / test_io.py View on Github external
P = numpy.array([[106.7922547, -6.2297884],
                         [106.7924589, -6.2298087],
                         [106.7924538, -6.2299127],
                         [106.7922547, -6.2298899],
                         [106.7922547, -6.2297884]])

        C = calculate_polygon_centroid(P)

        # Check against reference centroid from qgis
        reference_centroid = [106.79235602697445, -6.229849764722536]
        msg = 'Got %s but expected %s' % (str(C), str(reference_centroid))
        assert numpy.allclose(C, reference_centroid, rtol=1.0e-8), msg

        # Store centroid to file (to e.g. check with qgis)
        out_filename = unique_filename(prefix='test_centroid', suffix='.shp')
        V = Vector(data=None,
                   projection=DEFAULT_PROJECTION,
                   geometry=[C],
                   name='Test centroid')
        V.write_to_file(out_filename)
github douban / dpark / examples / kmeans.py View on Github external
def parseVector(line):
    return Vector(list(map(float, line.strip().split(' '))))
github mikaelho / scripter / vector.py View on Github external
''' As `steps_to`, but returns unique points rounded to the nearest integer. '''
    previous = Vector(0,0)
    for step in self.steps_to(other, step_magnitude):
      rounded = round(step)
      if rounded != previous:
        previous = rounded
        yield rounded
    

if __name__ == '__main__':
  v = Vector(x = 1, y = 2)
  v2 = Vector(3, 4)
  v += v2
  assert str(v) == '[4, 6]'
  assert v / 2.0 == Vector(2, 3)
  assert v * 0.1 == Vector(0.4, 0.6)
  assert v.distance_to(v2) == math.sqrt(1+4)

  v3 = Vector(Vector(1, 2) - Vector(2, 0)) # -1.0, 2.0
  v3.magnitude *= 2
  assert v3 == [-2, 4]

  v3.radians = math.pi # 180 degrees
  v3.magnitude = 2
  assert v3 == [-2, 0]
  v3.degrees = -90
  assert v3 == [0, -2]
  
  assert list(Vector(1, 1).steps_to(Vector(3, 3))) == [[1.7071067811865475, 1.7071067811865475], [2.414213562373095, 2.414213562373095], [3, 3]]
  assert list(Vector(1, 1).steps_to(Vector(-1, 1))) == [[0, 1], [-1, 1]]
  assert list(Vector(1, 1).rounded_steps_to(Vector(3, 3))) == [[2, 2], [3, 3]]
github Minimata / IslandGenerator / main.py View on Github external
def warp(p, freq=5.0):
    """
    This is the core warp function, gotten from Inigo Quilez ( <3 )

    It takes a position (as a vector) on entry.
    Returns a float value between -1 and 1.
    The more warping, the closer to 0 the value will get, so scale it up before writing it to an image.
    """
    updated_value = Vector(0, 0)
    for i in range(num_warpings):
        off1 = simplex_offsets[2 * i]
        off2 = simplex_offsets[2 * i + 1]
        updated_value = Vector(fbm(p + updated_value * 4.0 + Vector(off1[0], off1[1]), freq=freq),
                               fbm(p + updated_value * 4.0 + Vector(off2[0], off2[1]), freq=freq))
    return fbm(p + updated_value * 4.0, freq=freq)
github omarrayward / Linear-Algebra-Refresher-Udacity / vector.py View on Github external
def cross_product(self, other):
        [x1, y1, z1] = self.coordinates
        [x2, y2, z2] = other.coordinates
        x = (y1 * z2) - (y2 * z1)
        y = -((x1 * z2) - (x2 * z1))
        z = (x1 * y2) - (x2 * y1)
        return Vector([x, y, z])
github Minimata / IslandGenerator / main.py View on Github external
def create_gradient_from_normals(normals):
    gradients = []
    print("Creating gradients...")
    for normal in normals:
        if normal[2] > 0:
            gradients.append(Vector(normal[0] / normal[2], normal[1] / normal[2], 0.0))
        else:
            gradients.append(Vector(normal[0], normal[1], 0.0).normalize())
    return gradients
github teammcr192 / spherical-k-means / Python / spkmeans.py View on Github external
def compute_concept(self, p):
		"""
		Computes the Concept Vector of given partition p, and returns said
		Concept Vector. Parameter p must be a list containing at least one
		document Vector.
		"""
		# computer sum of all vectors in partition p
		cv = Vector(self.num_words)
		for doc_v in p:
			cv += doc_v
			
		# compute the mean vector for partition using the sum vector
		cv *= (1/len(p))
		
		# computer the norm of the mean vector
		cv.normalize()
		
		return cv
github omarrayward / Linear-Algebra-Refresher-Udacity / vector.py View on Github external
v = Vector([2.118, 4.827])
    w = Vector([0, 0])
    is_parallel = v.is_parallel(w)
    is_orthogonal = v.is_orthogonal(w)

    print('4 parallel: {}, orthogonal: {}'.format(is_parallel, is_orthogonal))

    # *****************

    v = Vector([3.039, 1.879])
    w = Vector([0.825, 2.036])
    projected_vector = v.get_projected_vector(w)

    print('projected vector is: {}'.format(projected_vector))

    v = Vector([-9.88, -3.264, -8.159])
    w = Vector([-2.155, -9.353, -9.473])
    orthogonal_vector = v.get_orthogonal_vector(w)

    print('orthogonal vector is: {}'.format(orthogonal_vector))

    v = Vector([3.009, -6.172, 3.692, -2.51])
    w = Vector([6.404, -9.144, 2.759, 8.718])
    projected_vector = v.get_projected_vector(w)
    orthogonal_vector = v.get_orthogonal_vector(w)

    print('second projected vector is: {}'.format(projected_vector))

    print('second orthogonal vector is: {}'.format(orthogonal_vector))

    # *****************
github kykamath / streaming_lsh / vector.py View on Github external
def getNormalizedVector(self):
        modValue = self.mod()
        if modValue==0: return Vector(self)
        normalizedVector = Vector()
        for k, v in self.iteritems(): normalizedVector[k]=v/modValue
        return normalizedVector
github guaxiao / rasterizer.py / canvas.py View on Github external
def draw_scanline(self, va, vb, y):
        x1 = int(va.position.x)
        x2 = int(vb.position.x)
        sign = 1 if x2 > x1 else -1
        factor = 0
        for x in range(x1, x2 + sign * 1, sign):
            if x1 != x2:
                factor = (x - x1) / (x2 - x1)
            color = interpolate(va.color, vb.color, factor)
            self.draw_point(Vector(x, y), color)