def find_triangles(points, min_size=30, threshold=0.1): triangles = [] for ind1, p1 in enumerate(points[:-2]): for ind2, p2 in enumerate(points[ind1 + 1:-1], ind1 + 1): dist1 = distance(p1, p2) if dist1 + dist1 * threshold < min_size: continue for p3 in points[ind2 + 1:]: dist2, dist3 = distance(p1, p3), distance(p2, p3) avg_dist = sum([dist1, dist2, dist3]) / 3 e1, e2, e3 = abs(dist1 - dist2), abs(dist1 - dist3), abs(dist2 - dist3) acceptable_error = avg_dist * threshold if e1 <= acceptable_error and e2 <= acceptable_error and e3 <= acceptable_error: cx = sum([p[0] for p in [p1, p2, p3]]) / 3 cy = sum([p[1] for p in [p1, p2, p3]]) / 3 triangles.append((cx, cy, avg_dist)) return triangles
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]
def test_distance(self): #Distance point-origine a = points.origine b = points.Point(1, 1, 1) self.assertEqual(points.distance(a, b), 3**0.5) #Distance point positif - point positif a = points.Point(2, 3, 4) b = points.Point(1, 1, 1) self.assertEqual(points.distance(a, b), ((2 - 1)**2 + (3 - 1)**2 + (4 - 1)**2)**0.5)
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]
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]
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]
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()
def get_path_length(path, tolerance=0.5): fpath = flat_path(path, tolerance) ret = 0 start = fpath[0] for item in fpath[1]: ret += distance(start, item) start = item return ret
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
def _get_point_on_path(flatpath, pos): start = flatpath[0] end = flatpath[0] point = None lenght = 0 for item in flatpath[1]: start, end = end, item lenght += distance(start, end) if lenght >= pos: coef = 1.0 - (lenght - pos) / distance(start, end) point = midpoint(start, end, coef) break if not point: last = distance(start, end) coef = (pos - lenght + last) / last point = midpoint(start, end, coef) angle = get_point_angle(end, start) return point, angle
def predict(clusters: list[Cluster]) -> None: y = int(input("Enter Student's Attendance: ")) x = int(input("Enter Student's Marks: ")) distances = [points.distance(cluster, Point(x, y)) for cluster in clusters] for cluster, distance in zip(clusters, distances): print( f"Probability for {cluster.title}: {1 - distance / sum(distances):.2%}" )
def get_path_length(path, tolerance=0.5): fpath = flat_path(path, tolerance) points = [fpath[0], ] + fpath[1] if fpath[2] == sk2const.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
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
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)
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)
def normalize(point): """ Returns an unit vector pointing in the same direction. """ k = distance(point) return [point[0] / k, point[1] / k] if k else [0.0, 0.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)
def _neighborhood(self, p1, p2): return distance(p1, p2) < self.eps