Example #1
0
def split_bezier_line(start_point, end_point, point):
	if len(start_point) > 2: start_point = start_point[2]
	dist1 = distance(start_point, end_point)
	dist2 = distance(start_point, point)
	coef = dist2 / dist1
	x = coef * (end_point[0] - start_point[0]) + start_point[0]
	y = coef * (end_point[1] - start_point[1]) + start_point[1]
	return [x, y]
Example #2
0
def unpack_seg(seg, startpoint=None):
	"""
	Converts path pont into expected sequence.
	Also fixes null size control points.
	"""
	if len(seg) > 2:
		ctrl0 = seg[0]
		ctrl1 = seg[1]
		if startpoint:
			if not distance(startpoint, ctrl0):
				ctrl0 = midpoint(startpoint, seg[2], 0.0001)
			if not distance(seg[2], ctrl1):
				ctrl1 = midpoint(startpoint, seg[2], 0.9999)
		return [True, [ctrl0, ctrl1] , seg[2], seg[3]]
	return [False, None, seg, None]
Example #3
0
def normalize(point):
	"""
	Returns a unit vector pointing in the same direction.
	"""
	k = distance(point)
	if not k: return [0.0, 0.0]
	return [point[0] / k, point[1] / k]
Example #4
0
    def clustering(self):
        """
        Step1: calculate distance between points and the mean of each cluster
        Step2: compare theses k's distances, pick the minimal one
        Step3: if the point did not exist in the cluster whose mean is closest to it, add this point into
        Step4: recalculate means
        Step5: if all points are not reassgined, return
        """
        stop = False
        while not stop:
            stop = True

            for point in self.points:

                ds = []
                for cluster in self.clusters:
                    d = distance(point, cluster["mean"])
                    ds.append(d)

                minId = ds.index(min(ds))

                if point.clusterId != minId:
                    self.clusters[point.clusterId]["cluster"].remove(point)
                    self.clusters[minId]["cluster"].add(point)
                    point.clusterId = minId
                    stop = False

            self._reCalculateMean()
Example #5
0
def _get_point_on_path(flat_path, pos):
	start = flat_path[0]
	end = flat_path[0]
	point = None
	l = 0
	for item in flat_path[1]:
		start, end = end, item
		l += distance(start, end)
		if l >= pos:
			coef = 1.0 - (l - pos) / distance(start, end)
			point = midpoint(start, end, coef)
			break
	if not point:
		last = distance(start, end)
		coef = (pos - l + last) / last
		point = midpoint(start, end, coef)
	angle = get_point_angle(end, start)
	return point, angle
Example #6
0
def get_path_length(path, tolerance=0.5):
	fpath = flat_path(path, tolerance)
	points = [fpath[0], ] + fpath[1]
	if fpath[2] == sk2_const.CURVE_CLOSED:
		points += [fpath[0], ]
	ret = 0
	start = []
	for item in points:
		if not start:
			start = item
			continue
		ret += distance(start, item)
		start = item
	return ret
Example #7
0
	def convert_to_dashes(self, dash_size, dash_list):
		self.cp_indexes = []
		self.cp_dict = {}
		approximations = get_approx_paths([self, ])[0]
		cross_point_id = 0
		local_length = 0.0
		dash_index = 0
		local_length += float(dash_list[dash_index]) * dash_size
		for approximation in approximations:
			approx_path = approximation[1]
			p = 1
			break_flag = False
			while not break_flag:
				(p0, t0), (p1, t1) = approx_path[p - 1:p + 1]
				size = distance(p0, p1)
				if local_length > size:
					local_length -= size
					p += 1
					if p >= len(approx_path): break_flag = True
				elif local_length == size:
					at = t1
					self.cp_indexes.append(at)
					self.cp_dict[at] = cross_point_id
					cross_point_id += 1
					dash_index += 1
					if dash_index >= len(dash_list):dash_index = 0
					local_length = float(dash_list[dash_index]) * dash_size
					p += 1
					if p >= len(approx_path): break_flag = True
				else:
					cp = midpoint(p0, p1, local_length / size)
					at = index(cp, p0, t0, p1, t1)
					self.cp_indexes.append(at)
					self.cp_dict[at] = cross_point_id
					cross_point_id += 1
					dash_index += 1
					if dash_index >= len(dash_list):dash_index = 0
					local_length += float(dash_list[dash_index]) * dash_size
		return self.split(False)
Example #8
0
 def _neighborhood(self, p1, p2):
     return distance(p1, p2) < self.eps
Example #9
0
def length(p):
	"""
	Convenience function to make the code more readable. Returns
	the length of a Vector from the origin to the given Point
	"""
	return distance(p)