Exemple #1
0
    def minDistance(self, point):
        d0 = distance(self.points[0] - point)
        d1 = distance((self.points[1] - point))
        d2 = distance((self.points[2] - point))
        d3 = distance((self.points[3] - point))

        return min(d0, d1, d2, d3)
Exemple #2
0
    def getClosestVertex(self, point, epsilon):
        """
        Returns the index and distance of shape's nearest vertex to a specified :point:
        in case the distance is smaller or equal to the threshold :epsilon:.
        Otherwise return None.
        :param point:       the position to which the vertices are to be comapred
        :param epsilon:     a threshold that indicates when to accept a point as
                            close enough to be considered to be returned.
        :return:            index, distance to the closest element if distance < epsilon, else none.
        """
        default_index = -2
        sel_distance, sel_index = default_index, default_index
        for i, p in enumerate(self.points):
            d = distance(p-point)
            if d <= epsilon:
                if sel_distance == default_index or sel_distance > d:
                    sel_index, sel_distance, = i, d

        # Only in case the current element is selected,
        # check the position of the rotate element.
        if self.selected:
            rot_info = self.getShapeRotationVertex(True)
            if rot_info is not None:
                d = distance(point-rot_info[0])
                d = distance(point-rot_info[0])
                if d <= epsilon*2:
                    if sel_distance == default_index or sel_distance > d:
                        sel_index, sel_distance, = Shape.INDEX_ROTATION_ENTITY, d


        return (sel_index, sel_distance) if sel_index != default_index else None
Exemple #3
0
    def __find_transport_buttons(self, objects):
        if not objects['transportcontrol']:
            print('no transport buttons found')
            return

        all_buttons = sorted(objects['transportcontrol'], key=lambda x: x[1])

        # Here we calculate average distance between buttons in group based on 3 first buttons
        a = all_buttons[:3]
        avg_distance = 0
        for i in range(0, len(a) - 1):
            avg_distance += utils.distance(a[i + 1], a[i])

        avg_distance = int(avg_distance / 2)

        # Add other buttons in group
        # Buttons are sorted from top to bottom, so, top buttons are in our group
        group_btns = [all_buttons[0]]
        for i in range(1, len(all_buttons)):
            btn = all_buttons[i]
            dist = utils.distance(btn, all_buttons[0])
            if (avg_distance * i * 1.1) > dist > (avg_distance * i * 0.9):
                group_btns.append(btn)

        self.transport_buttons = group_btns
Exemple #4
0
 def intersectingEdges(self, x1y1, x2y2, points):
     """For each edge formed by `points', yield the intersection
     with the line segment `(x1,y1) - (x2,y2)`, if it exists.
     Also return the distance of `(x2,y2)' to the middle of the
     edge along with its index, so that the one closest can be chosen."""
     x1, y1 = x1y1
     x2, y2 = x2y2
     for i in range(4):
         x3, y3 = points[i]
         x4, y4 = points[(i + 1) % 4]
         denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1)
         nua = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)
         nub = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)
         if denom == 0:
             # This covers two cases:
             #   nua == nub == 0: Coincident
             #   otherwise: Parallel
             continue
         ua, ub = nua / denom, nub / denom
         if 0 <= ua <= 1 and 0 <= ub <= 1:
             x = x1 + ua * (x2 - x1)
             y = y1 + ua * (y2 - y1)
             m = QPointF((x3 + x4) / 2, (y3 + y4) / 2)
             d = distance(m - QPointF(x2, y2))
             yield d, i, (x, y)
Exemple #5
0
 def nearest_vertex(self, point, epsilon):
     index = None
     for i, p in enumerate(self.points):
         dist = distance(p - point)
         if dist <= epsilon:
             index = i
             epsilon = dist
     return index
Exemple #6
0
 def nearestVertex(self, point, epsilon):
     min_idx = None
     min_dis = 1e10
     for i, p in enumerate(self.points):
         dis = distance(p - point)
         if dis <= epsilon:
             if dis < min_dis:
                 min_dis = dis
                 min_idx = i
     return min_idx
Exemple #7
0
 def nearestVertex(self, point, epsilon):
     min_index = None
     min_d = None
     for i, p in enumerate(self.points):
         d = distance(p - point)
         if d <= epsilon:
             if min_d is None or d < min_d:
                 min_d = d
                 min_index = i
     return min_index
Exemple #8
0
 def __find_planet_for_unload(self, transport):
     min_dist = 2000
     pl = None
     for planet in self.planets:
         if planet.name in ARTIFACT_STORAGE:
             continue
         #TODO - find transport current position
         distance_to_planet = utils.distance(transport.destination.coords, planet.coords)
         if planet.name in WARP_PLANETS and planet.status != 3 and distance_to_planet < min_dist:
             pl = planet
             min_dist = distance_to_planet
     return pl
Exemple #9
0
    def __find_unvisited_planet(self, transport):
        min_dist = 2000
        pl = None
        for planet in self.planets:
            if planet.name in transport.visited or planet.status in [1, 2] or planet.name in ARTIFACT_STORAGE:
                continue

            if not transport.destination:
                pl = planet
                break

            distance_to_planet = utils.distance(transport.destination.coords, planet.coords)
            if distance_to_planet > min_dist:
                continue
            min_dist = distance_to_planet
            pl = planet

        if pl:
            print('planet found', pl.name)
        else:
            print('no planets left')
        return pl
Exemple #10
0
 def nearestVertex(self, point, epsilon):
     for i, p in enumerate(self.points):
         if distance(p - point) <= epsilon:
             return i
     return None
Exemple #11
0
 def close_enough(self, p1, p2):
     return distance(p1 - p2) < self.epsilon
Exemple #12
0
 def closeEnough(self, p1, p2):
     #d = distance(p1 - p2)
     #m = (p1-p2).manhattanLength()
     return distance(p1 - p2) < self.epsilon
Exemple #13
0
 def reachMaxPoints(self,epsilon):
     if len(self.points) >= 2 and distance(self.points[-1]-self.points[0]) < epsilon:
         return True
     return False
Exemple #14
0
 def nearest_vertex(self, point, epsilon):
     for i, p in enumerate(self.points):
         if distance(p - point) <= epsilon and i not in [1, 3]:
             return i
     return None
Exemple #15
0
    def paintOnlylabel(self, painter):
        if self.points:
            color = self.select_line_color if self.selected else self.line_color
            if self.warning:
                if self.label in Shape.warning_set or (self.label and
                                                       (' ' in self.label
                                                        or 'ブ' in self.label)):
                    color = EDIT_SELECT_LINE_COLOR
            pen = QPen(color)

            # Try using integer sizes for smoother drawing(?)
            pen.setWidth(max(1, int(round(2.0 / self.scale))))
            painter.setPen(pen)

            line_path = QPainterPath()
            vrtx_path = QPainterPath()

            line_path.moveTo(self.points[0])
            # Uncommenting the following line will draw 2 paths
            # for the 1st vertex, and make it non-filled, which
            # may be desirable.
            #self.drawVertex(vrtx_path, 0)

            painter.drawPath(line_path)
            painter.drawPath(vrtx_path)
            painter.fillPath(vrtx_path, self.vertex_fill_color)

            # Draw text at the top-left
            if self.paintLabel or self.fill:
                if self.fill:
                    color = EDIT_SELECT_LINE_COLOR
                    pen = QPen(color)
                    # Try using integer sizes for smoother drawing(?)
                    pen.setWidth(max(1, int(round(2.0 / self.scale))))
                    painter.setPen(pen)
                    painter.setBackground(QColor(0, 0, 0))
                    painter.setBackgroundMode(Qt.OpaqueMode)

                h_box = distance(self.points[3] - self.points[0])
                size = h_box * 0.5
                size = max(20, min(size, 50))

                # w_box = distance(self.points[1] - self.points[0])
                min_x = sys.maxsize
                min_y = sys.maxsize

                for point in self.points:
                    min_x = min(min_x, point.x())
                    min_y = min(min_y, point.y() - size * 0.5)

                # min_x = min(min_x, self.points[0].x())  # - w_box
                # min_y = min(min_y, self.points[0].y() - size * 0.5)
                min_x = max(min_x, 0)
                min_y = max(min_y, 0)
                if min_x != sys.maxsize and min_y != sys.maxsize:
                    font = QFont()
                    font.setPointSize(size)
                    font.setBold(True)
                    painter.setFont(font)
                    if (self.label == None):
                        self.label = ""
                    if (min_y < MIN_Y_LABEL):
                        min_y += MIN_Y_LABEL
                    # painter.drawText(min_x, min_y, self.label)
                    painter.drawText(min_x, min_y,
                                     '|'.join([self.label] + self.subLabels))
Exemple #16
0
 def closeEnough(self, p1, p2):
     #d = distance(p1 - p2)
     #m = (p1-p2).manhattanLength()
     # print "d %.2f, m %d, %.2f" % (d, m, d - m)
     return distance(p1 - p2) < self.epsilon
Exemple #17
0
 def nearestCursor(self, point, epsilon):
     for i, p in enumerate(self.cursor_points):
         if distance(p - point) <= epsilon:
             return i
     return None