def pnt2line(pnt, start, end): line_vec = vec.vector(start, end) pnt_vec = vec.vector(start, pnt) line_len = vec.length(line_vec) line_unitvec = vec.unit(line_vec) pnt_vec_scaled = vec.scale(pnt_vec, 1.0 / line_len) t = vec.dot(line_unitvec, pnt_vec_scaled) if t < 0.0: t = 0.0 elif t > 1.0: t = 1.0 nearest = vec.scale(line_vec, t) dist = vec.distance(nearest, pnt_vec) nearest = vec.add(nearest, start) return (dist, nearest)
def PolyLine(img, polys, is_closed, color, text=None, scale_f=1): if not DEBUG: return if scale_f <> 1: polys[0] = map(lambda p:scale(p, scale_f), polys[0]) polys[0] = [(int(p[0]), int(p[1])) for p in polys[0]] cv.PolyLine(img, polys, is_closed, color) if text is not None: polys = polys[0] x, y = reduce(lambda (x, y), (w, z):(x + w, y + z), polys, (0, 0)) cv.PutText(img, text, (x / len(polys), y / len(polys)), font, (0, 255, 255))
def PolyLine(img, polys, is_closed, color, text=None, scale_f=1): if not DEBUG: return if scale_f != 1: polys[0] = map(lambda p: scale(p, scale_f), polys[0]) polys[0] = [(int(p[0]), int(p[1])) for p in polys[0]] cv.PolyLine(img, polys, is_closed, color) if text is not None: polys = polys[0] x, y = reduce(lambda (x, y), (w, z): (x + w, y + z), polys, (0, 0)) cv.PutText(img, text, (x / len(polys), y / len(polys)), font, (0, 255, 255))
def scale2(v): return scale(2.0, v)
def new_function(v): return scale(scalar, v)
def linear_combination(scalars, *vectors): scaled = [scale(s, v) for s,v in zip(scalars, vectors)] return add(*scaled)
def apply_A(v): return add( scale(v[0], Ae1), scale(v[1], Ae2), scale(v[2], Ae3) )
def gravitational_field(sources, x, y): fields = [ vectors.scale(-source.gravity, (x - source.x, y - source.y)) for source in sources ] return vectors.add(*fields)
def normalize(v): l = length(v) return scale(1/l, v)