示例#1
0
 def mouseMoveEvent(self, event: QMouseEvent) -> None:
     pos = self.mapToScene(event.localPos().toPoint())
     x = int(pos.x())
     y = int(pos.y())
     if self.status == 'line':
         self.temp_item.p_list[1] = [x, y]
     elif self.status == 'polygon':
         self.temp_item.p_list[len(self.temp_item.p_list) - 1] = [x, y]
     elif self.status == 'ellipse':
         self.temp_item.p_list[1] = [x, y]
     elif self.status == 'curve':
         self.temp_item.p_list[len(self.temp_item.p_list) - 1] = [x, y]
     elif self.status == 'translate':
         self.item_dict[self.selected_id].p_list = alg.translate(self.item_dict[self.selected_id].p_list, x - self.temp_loc[0], y - self.temp_loc[1])
         self.item_dict[self.selected_id].update()
         self.temp_loc = [x, y]
     elif self.status == 'rotate' :
         x0 = x - self.temp_loc[0]
         y0 = y - self.temp_loc[1]
         r = self.temp_r - alg.cal_r(x0, y0)
         self.item_dict[self.selected_id].p_list = alg.rotate(self.temp_list, self.temp_loc[0], self.temp_loc[1], int(r * 180 / math.pi))
         self.item_dict[self.selected_id].update()
     elif self.status == 'scale' :
         x0 = x - self.temp_loc[0]
         y0 = y - self.temp_loc[1]
         r = (x0 * x0 + y0 * y0) ** 0.5
         self.item_dict[self.selected_id].p_list = alg.scale(self.temp_list, self.temp_loc[0], self.temp_loc[1], r / self.temp_dis)
         self.item_dict[self.selected_id].update()
     elif self.status == 'clip':
         self.clip_loc = [x, y]
     self.updateScene([self.sceneRect()])
     super().mouseMoveEvent(event)
示例#2
0
文件: cg_gui.py 项目: Wandaboma/CG
 def mouseMoveEvent(self, event: QMouseEvent) -> None:
     pos = self.mapToScene(event.localPos().toPoint())
     x = int(pos.x())
     y = int(pos.y())
     if self.status == 'line':
         self.temp_item.p_list[1] = [x, y]
     elif self.status == 'ellipse':
         self.temp_item.p_list[1] = [x, y]
     elif self.status == 'translate':
         if self.selected_id != '':
             temp_list = alg.translate(self.origin_list, x - self.x0, y - self.y0)
             self.item_dict[self.selected_id].p_list = temp_list
     elif self.status == 'rotate':
         if self.selected_id != '':
             if y != self.y0:
                 theta = math.atan(1.0 * (x - self.x0) / (y - self.y0)) * 180 / math.pi
                 temp_list = alg.rotate(self.origin_list, self.x0, self.y0, -int(theta))
                 self.item_dict[self.selected_id].p_list = temp_list
     elif self.status == 'scale':
         if self.selected_id != '':
             t = ((x - self.x0) * (x - self.x0) + (y - self.y0) * (y - self.y0)) ** 0.5    
             t = t / 30
             temp_list = alg.scale(self.origin_list, self.x0, self.y0, t)
             self.item_dict[self.selected_id].p_list = temp_list
     elif self.status == 'clip' or self.status == 'pclip':
         if self.selected_id != '':
             self.x1 = x
             self.y1 = y
     elif self.status == 'copy':
          if self.selected_id != '':
             temp_list = alg.translate(self.origin_list, x - self.x0, y - self.y0)
             self.temp_item.p_list = temp_list
     self.updateScene([self.sceneRect()])
     super().mouseMoveEvent(event)
示例#3
0
 def mouseMoveEvent(self, event: QMouseEvent) -> None:
     pos = self.mapToScene(event.localPos().toPoint())
     x = int(pos.x())
     y = int(pos.y())
     if self.status == 'line':
         self.temp_item.p_list[1] = [x, y]
     if self.status == 'ellipse':
         if self.temp_item == None:
             print("Hit Here")
             pass
         self.temp_item.p_list[1] = [x, y]
     if self.status == "polygon":
         # save the last position mouse stay
         # and make Item move as mouse did
         # Here Hit crash at one time
         if self.temp_item == None:
             pass
         else:
             self.temp_item.p_list[-1] = [x, y]
     if self.status == "translate":
         self.temp_item.p_list = alg.translate(self.temp_p_list,
                                               x - self.start_point[0],
                                               y - self.start_point[1])
     if self.status == "scale":
         xp = ((x - self.start_point[0])**2 +
               (y - self.start_point[1])**2) / 10000
         # print("缩放倍数:",xp)
         self.temp_item.p_list = alg.scale(self.temp_p_list,
                                           self.start_point[0],
                                           self.start_point[1], xp)
     if self.status == 'clip_LB' or self.status == 'clip_CS':
         x_1 = min(self.start_point[0], x)
         x_2 = max(self.start_point[0], x)
         y_1 = min(self.start_point[1], y)
         y_2 = max(self.start_point[1], y)
         self.temp_item1.p_list = [[x_1, y_1], [x_1, y_2], [x_2, y_2],
                                   [x_2, y_1], [x_1, y_1]]
         if self.status == 'clip_LB':
             self.temp_item.p_list = alg.clip(self.temp_p_list,
                                              self.start_point[0],
                                              self.start_point[1], x, y,
                                              "Liang-Barsky")
         else:
             self.temp_item.p_list = alg.clip(self.temp_p_list,
                                              self.start_point[0],
                                              self.start_point[1], x, y,
                                              "Cohen-Sutherland")
     if self.status == "curve":
         if self.temp_item == None:
             pass
         else:
             self.temp_item.p_list[-1] = [x, y]
     if self.status == 'free':
         if self.temp_item == None:
             print("Hit Here")
             pass
         self.temp_item.p_list.append([x, y])
     self.updateScene([self.sceneRect()])
     super().mouseMoveEvent(event)
示例#4
0
 def start_scale(self):
     if self.selected_id == '':
         return
     self.status = 'scale'
     self.temp_item = self.item_dict[self.selected_id]
     self.pList = self.temp_item.p_list
     x_coordinate, xCoordinatePressed = QInputDialog.getInt(self, '缩放', 'Please input x_coordinate:', QLineEdit.Normal)
     y_coordinate, yCoordinatePressed = QInputDialog.getInt(self, '缩放', 'Please input y_coordinate:', QLineEdit.Normal)
     multiple, multiplePressed = QInputDialog.getDouble(self, '缩放', 'Please input multiple:', QLineEdit.Normal)
     if multiplePressed and xCoordinatePressed and yCoordinatePressed:
         self.temp_item.p_list = alg.scale(self.pList, x_coordinate, y_coordinate, multiple)
     self.pList = self.temp_item.p_list
     self.updateScene([self.sceneRect()])
示例#5
0
 def mouseMoveEvent(self, event: QMouseEvent) -> None:
     pos = self.mapToScene(event.localPos().toPoint())
     x = int(pos.x())
     y = int(pos.y())
     if self.status == 'line':
         if (self.temp_item == None):
             pass
         else:
             self.temp_item.p_list[1] = [x, y]
     elif self.status == 'ellipse':
         if (self.temp_item == None):
             pass
         else:
             self.temp_item.p_list[1] = [x, y]
     elif self.status == 'polygon':
         if (self.temp_item == None):
             pass
         else:
             self.temp_item.p_list[-1] = [x, y]
     elif self.status == 'fill_polygon':
         if (self.temp_item == None):
             pass
         else:
             self.temp_item.p_list[-1] = [x, y]
     elif self.status == 'curve':
         if (self.temp_item == None):
             pass
         else:
             self.temp_item.p_list[-1] = [x, y]
     elif self.status == 'translate':
         self.temp_item.p_list = alg.translate(self.plist,
                                               x - self.start_xy[0],
                                               y - self.start_xy[1])
     elif self.status == 'scale':
         s = ((x - self.start_xy[0])**2 + (y - self.start_xy[1])**2) / 10000
         self.temp_item.p_list = alg.scale(self.plist, self.start_xy[0],
                                           self.start_xy[1], s)
     elif self.status == 'clip':
         self.temp_item.p_list = alg.clip(self.plist, self.start_xy[0],
                                          self.start_xy[1], x, y,
                                          self.temp_algorithm)
     elif self.status == 'polygon_clip':
         self.temp_item.p_list = alg.polygon_clip(self.plist,
                                                  self.start_xy[0],
                                                  self.start_xy[1], x, y)
     self.updateScene([self.sceneRect()])
     #self.prepareGeometryChange()
     super().mouseMoveEvent(event)
示例#6
0
文件: cg_gui.py 项目: DerekHJH/cgPA
    def mouseMoveEvent(self, event: QMouseEvent) -> None:
        if event.buttons() == QtCore.Qt.LeftButton:
            pos = self.mapToScene(event.localPos().toPoint())
            x = int(pos.x())
            y = self.height - 1 - int(pos.y())
            if self.status == 'line' or self.status == 'ellipse':
                self.temp_item.p_list[1] = [x, y]
            elif self.status == 'translate':
                self.temp_item.p_list = alg.translate(self.p_list_copy,
                                                      x - self.xcenter,
                                                      y - self.ycenter)
            elif self.status == 'rotate':
                if self.xstart == -1:
                    self.xstart = x
                    self.ystart = y
                else:
                    a = self.xstart - self.xcenter
                    b = self.ystart - self.ycenter
                    c = x - self.xcenter
                    d = y - self.ycenter
                    diff = a * d - b * c
                    Cos = (a * c +
                           b * d) / math.sqrt(a * a +
                                              b * b) / math.sqrt(c * c + d * d)
                    angle = math.acos(Cos) / math.pi * 180
                    if diff < 0:
                        angle = 360 - angle
                    angle = 360 - angle
                    self.temp_item.p_list = alg.rotate(self.p_list_copy,
                                                       self.xcenter,
                                                       self.ycenter, angle)
            elif self.status == 'scale':
                s = max(0.1, 1.0 + (x - self.xcenter) / self.w)
                self.temp_item.p_list = alg.scale(self.p_list_copy,
                                                  self.xcenter, self.ycenter,
                                                  s)
            elif self.status == 'clip':
                self.xstart = x
                self.ystart = y
                self.rb.setGeometry(
                    QRect(self.origin, event.pos()).normalized())

        self.updateScene([self.sceneRect()])
        super().mouseMoveEvent(event)
示例#7
0
                y = int(line[3])
                r = int(line[4])
                item_type, p_list, algorithm, color = item_dict[item_id]
                item_dict[item_id] = [
                    item_type,
                    alg.rotate(p_list, x, y, -r), algorithm, color
                ]
            elif line[0] == 'scale':
                item_id = line[1]
                x = int(line[2])
                y = int(line[3])
                s = float(line[4])
                item_type, p_list, algorithm, color = item_dict[item_id]
                item_dict[item_id] = [
                    item_type,
                    alg.scale(p_list, x, y, s), algorithm, color
                ]
            elif line[0] == 'clip':
                item_id = line[1]
                x0 = int(line[2])
                y0 = int(line[3])
                x1 = int(line[4])
                y1 = int(line[5])
                clip_alg = line[6]
                item_type, p_list, algorithm, color = item_dict[item_id]
                item_dict[item_id] = [
                    item_type,
                    alg.clip(p_list, x0, y0, x1, y1, clip_alg), algorithm,
                    color
                ]
示例#8
0
 def item_scale(self, p_list, cx, cy, s):
     self.prepareGeometryChange()
     self.p_list = alg.scale(p_list, cx, cy, s)
示例#9
0
文件: cg_cli.py 项目: txdqtxdq/cg2020
                item_dict[item_id] = item_type, p_list, algorithm, color
            elif line[0] == 'rotate':
                item_id = line[1]
                x = int(line[2])
                y = int(line[3])
                r = int(line[4])
                item_type, p_list, algorithm, color = item_dict[item_id]
                p_list = alg.rotate(p_list, x, y, r)
                item_dict[item_id] = item_type, p_list, algorithm, color
            elif line[0] == 'scale':
                item_id = line[1]
                x = int(line[2])
                y = int(line[3])
                s = float(line[4])
                item_type, p_list, algorithm, color = item_dict[item_id]
                p_list = alg.scale(p_list, x, y, s)
                item_dict[item_id] = item_type, p_list, algorithm, color
            elif line[0] == 'clip':
                item_id = line[1]
                x0 = int(line[2])
                y0 = int(line[3])
                x1 = int(line[4])
                y1 = int(line[5])
                clip_algorithm = line[6]
                item_type, p_list, algorithm, color = item_dict[item_id]
                p_list = alg.clip(p_list, x0, y0, x1, y1, clip_algorithm)
                item_dict[item_id] = item_type, p_list, algorithm, color

            line = fp.readline()

示例#10
0
 def scalea(self, x, y, s):  #重名加了一个a
     temp_list = alg.scale(self.p_list, x, y, s)
     self.p_list = temp_list
     if self.if_polygon_fill == True:
         temp_list = alg.scale(self.filled_list, x, y, s)
         self.filled_list = temp_list
示例#11
0
文件: cg_cli.py 项目: VEGyyf/cg2020a
     y = int(item_type[2])
     r = int(item_type[3])
     newpixels = alg.rotate(
         p_list, x, y, r, algorithm,
         item_type[len(item_type) - 1])
     if newpixels is not None:
         for x, y in newpixels:
             canvas[y, x] = color
 elif item_type[0] == 'scale':
     parl = []
     newpixels = []
     x = int(item_type[1])
     y = int(item_type[2])
     r = float(item_type[3])
     newpixels = alg.scale(
         p_list, x, y, r, algorithm,
         item_type[len(item_type) - 1])
     if newpixels is not None:
         for x, y in newpixels:
             canvas[y, x] = color
 elif item_type[0] == 'clip':
     pixels = []
     newpixels = []
     xm = int(item_type[1])
     ym = int(item_type[2])
     xM = int(item_type[3])
     yM = int(item_type[4])
     clipalg = item_type[5]
     newpixels = alg.clip(p_list, xm, ym, xM, yM,
                          clipalg, algorithm)
     if newpixels is not None:
示例#12
0
                temp_item[1] = alg.translate(temp_item[1], dx, dy)
                item_dict[item_id] = temp_item
            elif line[0] == 'rotate':
                item_id = line[1]
                x = int(line[2])
                y = int(line[3])
                r = int(line[4])
                temp_item = item_dict.pop(item_id)
                temp_item[1] = alg.rotate(temp_item[1], x, y, r)
                item_dict[item_id] = temp_item
            elif line[0] == 'scale':
                item_id = line[1]
                x = int(line[2])
                y = int(line[3])
                s = float(line[4])
                temp_item = item_dict.pop(item_id)
                temp_item[1] = alg.scale(temp_item[1], x, y, s)
                item_dict[item_id] = temp_item
            elif line[0] == 'clip':
                item_id = line[1]
                x0 = int(line[2])
                y0 = int(line[3])
                x1 = int(line[4])
                y1 = int(line[5])
                algorithm = line[6]
                temp_item = item_dict.pop(item_id)
                temp_item[1] = alg.clip(temp_item[1], min(x0, x1), min(y0, y1),
                                        max(x0, x1), max(y0, y1), algorithm)
                item_dict[item_id] = temp_item
            line = fp.readline()
示例#13
0
 def paint(self,
           painter: QPainter,
           option: QStyleOptionGraphicsItem,
           widget: Optional[QWidget] = ...) -> None:
     if self.item_type == 'line':
         p_list = self.p_list
         if self.rotate_angle != 0:
             p_list = alg.rotate(self.p_list, self.rotate_center[0],
                                 self.rotate_center[1], self.rotate_angle)
         if self.scale_ratio != 0:
             p_list = alg.scale(self.p_list, self.scale_center[0],
                                self.scale_center[1], self.scale_ratio)
         item_pixels = alg.draw_line(p_list, self.algorithm)
         for p in item_pixels:
             painter.setPen(self.paintColor)
             painter.drawPoint(*p)
         if self.selected:
             painter.setPen(QColor(255, 0, 0))
             painter.drawRect(self.boundingRect())
     elif self.item_type == 'polygon':
         p_list = self.p_list
         if self.rotate_angle != 0:
             p_list = alg.rotate(self.p_list, self.rotate_center[0],
                                 self.rotate_center[1], self.rotate_angle)
         if self.scale_ratio != 0:
             p_list = alg.scale(self.p_list, self.scale_center[0],
                                self.scale_center[1], self.scale_ratio)
         item_pixels = alg.draw_polygon(p_list, self.algorithm)
         for p in item_pixels:
             painter.setPen(self.paintColor)
             painter.drawPoint(*p)
         if self.selected:
             painter.setPen(QColor(255, 0, 0))
             painter.drawRect(self.boundingRect())
     elif self.item_type == 'ellipse':
         p_list = self.p_list
         if self.scale_ratio != 0:
             p_list = alg.scale(self.p_list, self.scale_center[0],
                                self.scale_center[1], self.scale_ratio)
         item_pixels = alg.draw_ellipse(p_list)
         for p in item_pixels:
             painter.setPen(self.paintColor)
             painter.drawPoint(*p)
         if self.selected:
             painter.setPen(QColor(255, 0, 0))
             painter.drawRect(self.boundingRect())
     elif self.item_type == 'curve':
         p_list = self.p_list
         if self.rotate_angle != 0:
             p_list = alg.rotate(self.p_list, self.rotate_center[0],
                                 self.rotate_center[1], self.rotate_angle)
         if self.scale_ratio != 0:
             p_list = alg.scale(self.p_list, self.scale_center[0],
                                self.scale_center[1], self.scale_ratio)
         item_pixels = alg.draw_curve(p_list, self.algorithm)
         for p in p_list:
             painter.setPen(self.paintColor)
             painter.drawPoint(*p)
         for p in item_pixels:
             painter.drawPoint(*p)
         if self.selected:
             painter.setPen(QColor(255, 0, 0))
             painter.drawRect(self.boundingRect())
     elif self.item_type == 'select':
         x0, y0 = self.p_list[0]
         x1, y1 = self.p_list[1]
         x = min(x0, x1)
         y = min(y0, y1)
         w = max(x0, x1) - x
         h = max(y0, y1) - y
         painter.setPen(QColor(0, 0, 255))
         painter.drawRects(QRectF(x, y, w, h))
     elif self.item_type == 'rect':
         x0, y0 = self.p_list[0]
         x1, y1 = self.p_list[1]
         x = min(x0, x1)
         y = min(y0, y1)
         w = max(x0, x1) - x
         h = max(y0, y1) - y
         painter.setPen(self.paintColor)
         painter.drawRects(QRectF(x, y, w, h))
         if self.selected:
             painter.setPen(QColor(255, 0, 0))
             painter.drawRect(self.boundingRect())
     elif self.item_type == 'rotate':
         painter.drawPoint(*(self.p_list[0]))
     elif self.item_type == 'scale':
         painter.drawPoint(*(self.p_list[0]))
     elif self.item_type == 'clip':
         x0, y0 = self.p_list[0]
         x1, y1 = self.p_list[1]
         x = min(x0, x1)
         y = min(y0, y1)
         w = max(x0, x1) - x
         h = max(y0, y1) - y
         painter.setPen(QColor(0, 0, 255))
         painter.drawRects(QRectF(x, y, w, h))
示例#14
0
    def mouseReleaseEvent(self, event: QMouseEvent) -> None:
        if self.status == 'line':
            self.item_dict[self.temp_id] = self.temp_item
            self.list_widget.addItem(self.temp_id)
            self.finish_draw()
        elif self.status == 'polygon':
            pass
        elif self.status == 'ellipse':
            self.item_dict[self.temp_id] = self.temp_item
            self.list_widget.addItem(self.temp_id)
            self.finish_draw()
        elif self.status == 'curve':
            pass
        elif self.status == 'select':
            x0, y0 = self.temp_item.p_list[0]
            x1, y1 = self.temp_item.p_list[1]
            x = min(x0, x1)
            y = min(y0, y1)
            w = max(x0, x1) - x
            h = max(y0, y1) - y
            self.selection_changed(x, y, w, h)
            self.scene().removeItem(self.temp_item)
            self.finish_select()
        elif self.status == 'rect':
            self.item_dict[self.temp_id] = self.temp_item
            self.list_widget.addItem(self.temp_id)
            self.finish_draw()
        elif self.status == 'move':
            self.finish_move()
        elif self.status == 'rotate':
            if len(self.temp_item.p_list) == 2:
                self.scene().removeItem(self.temp_item)
                x0, y0 = self.temp_item.p_list[0]
                for item in self.selected_items:
                    item.p_list = alg.rotate(item.p_list, x0, y0,
                                             item.rotate_angle)
                self.finish_rotate()
        elif self.status == 'scale':
            if len(self.temp_item.p_list) == 2:
                self.scene().removeItem(self.temp_item)
                x0, y0 = self.temp_item.p_list[0]
                for item in self.selected_items:
                    item.p_list = alg.scale(item.p_list, x0, y0,
                                            item.scale_ratio)
                self.finish_scale()
        elif self.status == 'clip':
            x0, y0 = self.temp_item.p_list[0]
            x1, y1 = self.temp_item.p_list[1]
            x = min(x0, x1)
            y = min(y0, y1)
            w = max(x0, x1) - x
            h = max(y0, y1) - y
            for item in self.selected_items:
                if item.item_type == 'line':
                    item.p_list = alg.clip(item.p_list, x, y, x + w, y + h,
                                           self.temp_algorithm)
                    if len(item.p_list) < 2:
                        self.scene().removeItem(item)
                        # need to remove from Item List
            self.scene().removeItem(self.temp_item)
            self.finish_clip()

        self.updateScene([self.sceneRect()])
        super().mouseReleaseEvent(event)
示例#15
0
     item_type = item_dict[item_id][0]
     pointSet = item_dict[item_id][1]
     algorithm = item_dict[item_id][2]
     color = item_dict[item_id][3]
     pointSet = alg.rotate(pointSet, x, y, r)
     item_dict[item_id] = [item_type, pointSet, algorithm, color]
 elif line[0] == 'scale':
     item_id = line[1]
     x = int(line[2])
     y = int(line[3])
     s = float(line[4])
     item_type = item_dict[item_id][0]
     pointSet = item_dict[item_id][1]
     algorithm = item_dict[item_id][2]
     color = item_dict[item_id][3]
     pointSet = alg.scale(pointSet, x, y, s)
     item_dict[item_id] = [item_type, pointSet, algorithm, color]
 elif line[0] == 'clip':
     item_id = line[1]
     x_min = int(line[2])
     y_min = int(line[3])
     x_max = int(line[4])
     y_max = int(line[5])
     clip_algorithm = line[-1]
     item_type = item_dict[item_id][0]
     pointSet = item_dict[item_id][1]
     algorithm = item_dict[item_id][2]
     color = item_dict[item_id][3]
     pointSet = alg.clip(pointSet, x_min, y_min, x_max, y_max, clip_algorithm)
     item_dict[item_id] = [item_type, pointSet, algorithm, color]
 line = fp.readline()
示例#16
0
文件: cg_cli.py 项目: Wandaboma/CG
                item_type, p_list, algorithm, color = item_dict[id]
                pixels = alg.translate(p_list, x0, y0)
                item_dict[id] = [item_type, pixels, algorithm, color]
            elif line[0] == 'rotate':
                id = line[1]
                x0 = int(line[2])
                y0 = int(line[3])
                r = int(line[4])
                item_type, p_list, algorithm, color = item_dict[id]
                pixels = alg.rotate(p_list, x0, y0, r)
                item_dict[id] = [item_type, pixels, algorithm, color]
            elif line[0] == 'scale':
                id = line[1]
                x0 = int(line[2])
                y0 = int(line[3])
                s = float(line[4])
                item_type, p_list, algorithm, color = item_dict[id]
                pixels = alg.scale(p_list, x0, y0, s)
                item_dict[id] = [item_type, pixels, algorithm, color]
            elif line[0] == 'clip':
                id = line[1]
                x0 = int(line[2])
                y0 = int(line[3])
                x1 = int(line[4])
                y1 = int(line[5])
                clip_algo = line[6]
                item_type, p_list, algorithm, color = item_dict[id]
                pixels = alg.clip(p_list, x0, y0, x1, y1, clip_algo)
                item_dict[id] = [item_type, pixels, algorithm, color]
            line = fp.readline()
示例#17
0
                p_list = item_dict[item_id][1]
                p_list = alg.translate(p_list, dx, dy)
                item_dict[item_id][1] = p_list
            elif line[0] == 'rotate':
                item_id = line[1]
                x = int(line[2])
                y = int(line[3])
                r = int(line[4])
                p_list = item_dict[item_id][1]
                p_list = alg.rotate(p_list, x, y, r)
                item_dict[item_id][1] = p_list
            elif line[0] == 'scale':
                item_id = line[1]
                x = int(line[2])
                y = int(line[3])
                s = float(line[4])
                p_list = item_dict[item_id][1]
                item_dict[item_id][1] = alg.scale(p_list, x, y, s)
            elif line[0] == 'clip':
                item_id = line[1]
                x0 = int(line[2])
                y0 = int(line[3])
                x1 = int(line[4])
                y1 = int(line[5])
                algorithm = line[6]
                p_list = item_dict[item_id][1]
                item_dict[item_id][1] = alg.clip(p_list, x0, y0, x1, y1,
                                                 algorithm)

            line = fp.readline()
示例#18
0
 def mouseMoveEvent(self, event: QMouseEvent) -> None:
     pos = self.mapToScene(event.localPos().toPoint())
     x = int(pos.x())
     y = int(pos.y())
     if self.status == 'line':
         if self.temp_item:
             self.temp_item.p_list[1] = [x, y]
     elif self.status == 'rectangle':
         if self.temp_item:
             self.temp_item.p_list[1] = [x, y]
     elif self.status == 'polygon':
         if self.temp_item:
             x0, y0 = self.temp_item.p_list[0]
             if math.sqrt((x - x0)**2 + (y - y0)**2) <= 10 and len(
                     self.temp_item.p_list) >= 4:
                 self.temp_item.p_list.pop(len(self.temp_item.p_list) - 1)
                 self.temp_item.finished = True
                 self.item_dict[self.temp_id] = self.temp_item
                 self.finish_draw()
             else:
                 self.temp_item.p_list[-1] = [x, y]
     elif self.status == 'ellipse':
         if self.temp_item:
             self.temp_item.p_list[1] = [x, y]
     elif self.status == 'curve':
         if self.temp_item:
             self.temp_item.p_list[-1] = [x, y]
     elif self.status == 'translate_p1':
         pass
     elif self.status == 'translate_p2':
         if self.temp_coordinate:
             sx, sy = self.temp_coordinate
             self.temp_item.p_list = alg.translate(self.temp_plist, x - sx,
                                                   y - sy)
     elif self.status == 'rotate':
         self.rotate_end = [x, y]
         # 计算向量夹角
         sx, sy = self.rotate_start
         ex, ey = self.rotate_end
         cx, cy = self.rotate_center
         vec1 = np.array([sx - cx, sy - cy])
         vec2 = np.array([ex - cx, ey - cy])
         len1 = np.sqrt(vec1.dot(vec1))
         len2 = np.sqrt(vec2.dot(vec2))
         r = np.arccos(vec1.dot(vec2) / (len1 * len2))
         angle = int(math.degrees(r) + 0.5)
         if sx == ex:
             if sy < ey and cx > sx:
                 angle = 360 - angle
             elif sy > ey and cx < sx:
                 angle = 360 - angle
         else:
             k = (ey - sy) / (ex - sx)
             b = sy - k * sx
             if sx > ex and cy > k * cx + b:
                 angle = 360 - angle
             elif sx < ex and cy < k * cx + b:
                 angle = 360 - angle
         if self.temp_item:
             self.temp_item.p_list = alg.rotate(self.temp_plist,
                                                self.rotate_center[0],
                                                self.rotate_center[1],
                                                -angle)
     elif self.status == 'scale':
         self.scale_end = [x, y]
         # 计算向量夹角
         sx, sy = self.scale_start
         ex, ey = self.scale_end
         cx, cy = self.scale_center
         s = math.sqrt((ex - sx)**2 + (ey - sy)**2) / 80
         if self.temp_item:
             self.temp_item.p_list = alg.scale(self.temp_plist, cx, cy, s)
     elif self.status == 'clip':
         sx, sy = self.temp_coordinate
         if self.temp_item:
             self.temp_item.p_list = alg.clip(self.temp_plist, sx, sy, x, y,
                                              self.temp_algorithm)
     elif self.status == 'polygon_clip':
         sx, sy = self.temp_coordinate
         if self.temp_algorithm == 'Sutherland-Hodgman':
             if self.temp_item:
                 self.temp_item.p_list = alg.polygon_clip(
                     self.temp_plist, sx, sy, x, y, self.temp_algorithm)
     elif self.status == '':
         pass
     self.updateScene([self.sceneRect()])
     super().mouseMoveEvent(event)
示例#19
0
                    np.array(pen_color)
                ]
            elif line[0] == 'translate':
                plist = alg.translate(item_dict[line[1]][1], int(line[2]),
                                      int(line[3]))
                item_dict[line[1]][1] = plist
            elif line[0] == 'rotate':
                # 椭圆不需要旋转
                if item_dict[line[1]][0] == 'ellipse':
                    line = fp.readline()
                    continue
                plist = alg.rotate(item_dict[line[1]][1], int(line[2]),
                                   int(line[3]), -1 * int(line[4]))
                item_dict[line[1]][1] = plist
            elif line[0] == 'scale':
                plist = alg.scale(item_dict[line[1]][1], int(line[2]),
                                  int(line[3]), float(line[4]))
                item_dict[line[1]][1] = plist
            elif line[0] == 'clip':
                x_min = min(int(line[2]), int(line[4]))
                x_max = max(int(line[2]), int(line[4]))
                y_min = min(int(line[3]), int(line[5]))
                y_max = max(int(line[3]), int(line[5]))
                plist = alg.clip(item_dict[line[1]][1], x_min, y_min, x_max,
                                 y_max, line[6])
                if plist == []:
                    del item_dict[line[1]]
                else:
                    item_dict[line[1]][1] = plist
            ...

            line = fp.readline()
示例#20
0
     p_list = item_dict[item_id][1].copy()
     item_dict[item_id][1] = alg.translate(p_list, dx, dy)
 elif line[0] == 'rotate':
     item_id = line[1]
     xc = int(line[2])
     yc = height - 1 - int(line[3])
     angle = int(line[4])
     p_list = item_dict[item_id][1].copy()
     item_dict[item_id][1] = alg.rotate(p_list, xc, yc, angle)
 elif line[0] == 'scale':
     item_id = line[1]
     xc = int(line[2])
     yc = height - 1 - int(line[3])
     s = float(line[4])
     p_list = item_dict[item_id][1].copy()
     item_dict[item_id][1] = alg.scale(p_list, xc, yc, s)
 elif line[0] == 'clip':
     item_id = line[1]
     x0 = int(line[2])
     y0 = height - 1 - int(line[3])
     x1 = int(line[4])
     y1 = height - 1 - int(line[5])
     algorithm = line[6]
     p_list = item_dict[item_id][1].copy()
     item_dict[item_id][1] = alg.clip(p_list, x0, y0, x1, y1,
                                      algorithm)
     if len(item_dict[item_id][1]) == 0:
         item_dict.pop(item_id)
 elif line[0] == 'drawCurve':
     item_id = line[1]
     p_list = []
示例#21
0
 def scale(self, s):
     cx, cy = self.get_center()
     self.p_list = alg.scale(self.p_list, cx, cy, s)
示例#22
0
     algorithm = item_dict[item_id][2]
     color = item_dict[item_id][3]
     new_control_point = alg.rotate(control_point, x0, y0, r)
     item_dict[item_id] = [
         graph_type, new_control_point, algorithm, color
     ]
 elif line[0] == 'scale':
     item_id = line[1]
     x0 = int(line[2])
     y0 = int(line[3])
     s = line[4]
     graph_type = item_dict[item_id][0]
     control_point = item_dict[item_id][1]
     algorithm = item_dict[item_id][2]
     color = item_dict[item_id][3]
     new_control_point = alg.scale(control_point, x0, y0, s)
     item_dict[item_id] = [
         graph_type, new_control_point, algorithm, color
     ]
 elif line[0] == 'clip':
     item_id = line[1]
     x0 = int(line[2])
     y0 = int(line[3])
     x1 = int(line[4])
     y1 = int(line[5])
     algorithm = line[6]
     graph_type = item_dict[item_id][0]
     control_point = item_dict[item_id][1]
     color = item_dict[item_id][3]
     new_control_point = alg.clip(control_point, x0, y0, x1, y1,
                                  algorithm)
示例#23
0
文件: cg_cli.py 项目: matessd/cg
     xr = int(line[2])
     yr = int(line[3])
     theta = int(line[4])
     if item_id in item_dict:
         # can't rotate ellipse
         if item_dict[item_id][0] != 'ellipse':
             new_p_list = alg.rotate(item_dict[item_id][1], xr, yr,
                                     theta)
             item_dict[item_id][1] = new_p_list
 elif line[0] == 'scale':
     item_id = line[1]
     xs = int(line[2])
     ys = int(line[3])
     s = float(line[4])
     if item_id in item_dict:
         new_p_list = alg.scale(item_dict[item_id][1], xs, ys, s)
         item_dict[item_id][1] = new_p_list
 elif line[0] == 'clip':
     item_id = line[1]
     x0 = int(line[2])
     y0 = int(line[3])
     x1 = int(line[4])
     y1 = int(line[5])
     algorithm = line[-1]
     if item_id in item_dict:
         new_p_list = alg.clip(item_dict[item_id][1], x0, y0, x1,
                               y1, algorithm)
         # print(new_p_list)
         if new_p_list == []:
             del item_dict[item_id]
         else:
示例#24
0
 def mouseMoveEvent(self, event: QMouseEvent) -> None:
     pos = self.mapToScene(event.localPos().toPoint())
     x = int(pos.x())
     y = int(pos.y())
     if self.status == 'line':
         self.temp_item.p_list[1] = [x, y]
     elif self.status == 'ellipse':
         self.temp_item.p_list[1] = [x, y]
     elif self.status == 'translate':
         QApplication.setOverrideCursor(Qt.ClosedHandCursor)
         self.temp_item.p_list = alg.translate(self.temp_plist,
                                               x - self.translateOrigin[0],
                                               y - self.translateOrigin[1])
     elif self.status == 'scale':
         if self.scalePoint != [-1, -1]:
             QApplication.setOverrideCursor(Qt.ClosedHandCursor)
             a1 = self.corePoint[0] - self.scalePoint[0]
             b1 = self.corePoint[1] - self.scalePoint[1]
             a2 = self.corePoint[0] - self.projectPointToLine(
                 self.corePoint, self.scalePoint, [x, y])[0]
             b2 = self.corePoint[1] - self.projectPointToLine(
                 self.corePoint, self.scalePoint, [x, y])[1]
             if a1 == 0:
                 pivotLength = b1
                 nowLength = b2
             elif b1 == 0:
                 pivotLength = a1
                 nowLength = a2
             else:
                 if abs(a1) > abs(b1):
                     pivotLength = a1
                     nowLength = a2
                 else:
                     pivotLength = b1
                     nowLength = b2
             s = nowLength / pivotLength
             self.temp_item.p_list = alg.scale(self.temp_plist,
                                               self.scalePoint[0],
                                               self.scalePoint[1], 1 - s)
             self.helperLines_item.p_list = []
             for point in self.temp_item.p_list:
                 self.helperLines_item.p_list.append(
                     [point, self.scalePoint])
     elif self.status == 'rotate':
         if self.rotatePoint != [-1, -1]:
             QApplication.setOverrideCursor(Qt.ClosedHandCursor)
             x1, y1 = self.corePoint[0], self.corePoint[1]
             x2, y2 = self.rotatePoint[0] - x1, self.rotatePoint[1] - y1
             x3, y3 = x - x1, y - y1
             flip = False
             if x2 == 0:
                 if x3 < 0:
                     flip = True
             elif x2 < 0:
                 k = -y2 / x2
                 if -y3 < k * x3:
                     flip = True
             elif x2 > 0:
                 k = -y2 / x2
                 if -y3 > k * x3:
                     flip = True
             a = math.sqrt(pow(x3 - x2, 2) + pow(y3 - y2, 2))
             b = math.sqrt(x2 * x2 + y2 * y2)
             c = math.sqrt(x3 * x3 + y3 * y3)
             cosA = (b * b + c * c - a * a) / (2 * b * c)
             r = int(math.acos(cosA) * 180 / math.pi)
             if flip:
                 r = 360 - r
             self.temp_item.p_list = alg.rotate(self.temp_plist,
                                                self.corePoint[0],
                                                self.corePoint[1], r)
             # if self.temp_item.item_type == 'polygon' or self.temp_item.item_type == 'curve':
             #
             self.helperLines_item.p_list = []
             for point in self.temp_item.p_list:
                 self.helperLines_item.p_list.append(
                     [point, self.rotatePoint])
     elif self.status == 'clip':
         QApplication.setOverrideCursor(Qt.ClosedHandCursor)
         self.clipPoint2 = [x, y]
     if self.temp_item:
         self.helperPoints_item.p_list = self.temp_item.p_list[:]
     self.checkHelper()
     self.updateScene([self.sceneRect()])
     super().mouseMoveEvent(event)
示例#25
0
 def mouseMoveEvent(self, event: QMouseEvent) -> None:
     pos = self.mapToScene(event.localPos().toPoint())
     x = int(pos.x())
     y = int(pos.y())
     if self.status == 'line':
         self.temp_item.p_list[1] = [x, y]
     elif self.status == 'ellipse':
         self.temp_item.p_list[1] = [x, y]
     elif self.status == 'polygon':
         self.temp_item.p_list[-1] = [x, y]
     elif self.status == 'curve':
         self.temp_item.p_list[-1] = [x, y]
     elif self.status == 'translate':
         if self.selected_id != '':
             dx = x - int(self.origin_pos.x())
             dy = y - int(self.origin_pos.y())
             self.temp_item.p_list = alg.translate(self.origin_p_list, dx,
                                                   dy)
     elif self.status == 'rotate':
         if self.selected_id != '' and self.trans_center is not None and self.origin_pos is not None:
             x_origin, y_origin = int(self.origin_pos.x() -
                                      self.trans_center.x()), int(
                                          self.origin_pos.y() -
                                          self.trans_center.y())
             len_origin = math.sqrt(x_origin**2 + y_origin**2)
             x_now, y_now = x - int(self.trans_center.x()), y - int(
                 self.trans_center.y())
             len_now = math.sqrt(x_now**2 + y_now**2)
             if len_origin != 0 and len_now != 0:
                 sin_origin = y_origin / len_origin
                 cos_origin = x_origin / len_origin
                 sin_now = y_now / len_now
                 cos_now = x_now / len_now
                 delta_sin = sin_now * cos_origin - cos_now * sin_origin
                 delta_cos = cos_now * cos_origin + sin_now * sin_origin
                 if delta_cos >= 0:
                     r = math.asin(delta_sin)
                 else:
                     r = math.pi - math.asin(delta_sin)
                 self.temp_item.p_list = alg.rotate(
                     self.origin_p_list, int(self.trans_center.x()),
                     int(self.trans_center.y()), r, False)
     elif self.status == 'scale':
         if self.selected_id != '' and self.trans_center is not None and self.origin_pos is not None:
             x_last, y_last = int(self.origin_pos.x() -
                                  self.trans_center.x()), int(
                                      self.origin_pos.y() -
                                      self.trans_center.y())
             len_last = math.sqrt(x_last**2 + y_last**2)
             if len_last != 0:
                 x_now, y_now = x - int(self.trans_center.x()), y - int(
                     self.trans_center.y())
                 len_now = math.sqrt(x_now**2 + y_now**2)
                 self.temp_item.p_list = alg.scale(
                     self.origin_p_list, int(self.trans_center.x()),
                     int(self.trans_center.y()), len_now / len_last)
     elif self.status == 'clip':
         if self.selected_id != '' and self.origin_pos is not None and self.temp_item.item_type == 'line':
             x_min = min(int(self.origin_pos.x()), x)
             x_max = max(int(self.origin_pos.x()), x)
             y_min = min(int(self.origin_pos.y()), y)
             y_max = max(int(self.origin_pos.y()), y)
             '''
             self.temp_item.p_list = alg.clip(self.origin_p_list, x_min, y_min, x_max, y_max, self.temp_algorithm)
             print(self.temp_item.p_list)
             '''
             if self.border is None:
                 self.border = QGraphicsRectItem(x_min - 1, y_min - 1,
                                                 x_max - x_min + 2,
                                                 y_max - y_min + 2)
                 self.scene().addItem(self.border)
                 self.border.setPen(QColor(0, 255, 255))
             else:
                 self.border.setRect(x_min - 1, y_min - 1,
                                     x_max - x_min + 2, y_max - y_min + 2)
     elif self.status == 'freedom':
         self.temp_item.p_list.append([x, y])
     self.updateScene([self.sceneRect()])
     super().mouseMoveEvent(event)
示例#26
0
 def real(self):
     sx = self.sx
     sy = self.sy
     scaled = scale(self, *self.sc, sx, sy) if self.sc else self
     rotated = rotate(scaled, *self.rc, self.r) if self.rc else scaled
     return rotated
示例#27
0
                item_id = line[1]
                dx = int(line[2])
                dy = int(line[3])
                item_dict[item_id][1] = alg.translate(item_dict[item_id][1],
                                                      dx, dy)
            elif line[0] == 'rotate':
                item_id = line[1]
                x = int(line[2])
                y = int(line[3])
                r = int(line[4])
                item_dict[item_id][1] = alg.rotate(item_dict[item_id][1], x, y,
                                                   r)
            elif line[0] == 'scale':
                item_id = line[1]
                x = int(line[2])
                y = int(line[3])
                s = float(line[4])
                item_dict[item_id][1] = alg.scale(item_dict[item_id][1], x, y,
                                                  s)
            elif line[0] == 'clip':
                item_id = line[1]
                x0 = int(line[2])
                y0 = int(line[3])
                x1 = int(line[4])
                y1 = int(line[5])
                algorithm = line[6]
                item_dict[item_id][1] = alg.clip(item_dict[item_id][1], x0, y0,
                                                 x1, y1, algorithm)

            line = fp.readline()
示例#28
0
 def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, \
           widget: Optional[QWidget] = ...) -> None:
     def angle(v1, v2):
         """计算v2相对于v1的顺时针角度
         v1 = [[x0,y0],[x1,y1]], v2同理
         """
         dx1 = v1[1][0] - v1[0][0]
         dy1 = v1[1][1] - v1[0][1]
         dx2 = v2[1][0] - v2[0][0]
         dy2 = v2[1][1] - v2[0][1]
         angle1 = math.atan2(dy1, dx1)
         angle1 = int(angle1 * 180/math.pi)
         angle2 = math.atan2(dy2, dx2)
         angle2 = int(angle2 * 180/math.pi)
         ret = angle1 - angle2
         return ret
     
     def thick_draw_point(painter, poi):
         """加粗绘制一个点,用于裁剪线段时高亮选中部分"""
         painter.drawPoint(*[poi[0]+1,poi[1]+1])
         painter.drawPoint(*[poi[0]+1,poi[1]-1])
         painter.drawPoint(*[poi[0]-1,poi[1]+1])
         painter.drawPoint(*[poi[0]-1,poi[1]-1])
         return
     
     def paint_small_cycle(painter, p_list):
         for poi in p_list:
             pixels = alg.draw_ellipse([[poi[0]-2,poi[1]-2],[poi[0]+2,poi[1]+2]])
             for p in pixels:
                 painter.drawPoint(*p)
         return
     
     def paint_dotted_line(painter, p_list):
         pixels = alg.draw_dotted_line(p_list)
         for p in pixels:
             painter.drawPoint(*p)
     
     if self.p_list == [] or self.item_type == 'delete':
         # be deleted
         return
     
     # change p_list accoring to edit_type
     new_p_list = self.p_list
     if self.edit_type == 'translate':
         # 控制点
         painter.setPen(QColor(255,0,255))
         paint_small_cycle(painter, [self.poi, self.poi1])
         paint_dotted_line(painter, [self.poi, self.poi1])
         
         new_p_list = alg.translate(self.p_list, self.poi1[0]-self.poi[0], \
                                     self.poi1[1]-self.poi[1])
         if self.edit_over == 1:
             # finish
             self.edit_finish(new_p_list)
     elif self.edit_type == 'rotate':
         if self.item_type == 'ellipse':
             # g_window.statusBar().clearMessage()
             print("Can't rotate ellipse.")
             self.edit_finish(self.p_list)
         else:
             painter.setPen(QColor(255,0,255))
             if self.param_cnt==1:
                 paint_small_cycle(painter, [self.center])
             elif self.param_cnt == 2:
                 paint_small_cycle(painter, [self.center, self.poi, self.poi1])
                 paint_dotted_line(painter, [self.center, self.poi])
                 paint_dotted_line(painter, [self.center, self.poi1])
                 # center and poi, poi1 all gotten
                 theta = angle([self.center, self.poi], [self.center, self.poi1])
                 new_p_list = alg.rotate(self.p_list, \
                                 self.center[0], self.center[1], theta)
             if self.edit_over == 1:
                 # clear
                 self.edit_finish(new_p_list)
     elif self.edit_type == 'scale':
         painter.setPen(QColor(255,0,255))
         if self.param_cnt == 1:
             paint_small_cycle(painter, [self.center])
         if self.param_cnt == 2:
             paint_small_cycle(painter, [self.center, self.poi, self.poi1])
             paint_dotted_line(painter, [self.center, self.poi])
             paint_dotted_line(painter, [self.center, self.poi1])
             # 缩放倍数, 根据dx的比值确定
             if self.poi[0]-self.center[0] == 0:
                 s = 1
             else :
                 s = (self.poi1[0]-self.center[0])/(self.poi[0]-self.center[0])
             new_p_list = alg.scale(self.p_list, \
                             self.center[0], self.center[1], s)
         if self.edit_over == 1:
             self.edit_finish(new_p_list)
     elif self.edit_type == 'clip':
         if self.edit_over == 0:
             # draw the clip window
             painter.setPen(QColor(0,255,0))
             painter.drawRect( self.regionRect([self.poi,self.poi1]) )                 
             tmp_p_list = alg.clip(self.p_list, self.poi[0], self.poi[1],\
                             self.poi1[0], self.poi1[1], self.edit_algorithm)
             if tmp_p_list != []:
                 # highlight the line in clip window
                 tmp_pixels = self.get_draw_pixels(tmp_p_list,self.algorithm)
                 painter.setPen(QColor(0, 255, 0))
                 for p in tmp_pixels:
                     thick_draw_point(painter, p)
         elif self.edit_over == 1:
             # 得到裁剪后的端点
             new_p_list = alg.clip(self.p_list, self.poi[0], self.poi[1],\
                             self.poi1[0], self.poi1[1], self.edit_algorithm)
             self.edit_finish(new_p_list)
             if self.p_list == []:
                 # 线段被裁剪没了
                 self.item_type = 'delete'
                 self.pixels = []
                 g_canvas.clear_selection()
                 g_list_widget.takeItem(g_list_widget.currentRow())
                 del g_canvas.item_dict[self.id]
                 #下面这句加了后,画布大小改变后再删除图元会崩溃
                 # g_canvas.scene().removeItem(self)
                 return
     # 填充
     if self.isPadding:
         painter.setPen(self.paddingColor)
         polygon_padding(painter, self)
     
     item_pixels = []
     if new_p_list != []:
         if self.id == g_canvas.cur_id:
             item_pixels = self.get_draw_pixels(new_p_list, self.algorithm)
             self.pixels = item_pixels
         else:
             item_pixels = self.pixels
     else :
         print("Undefined Behavior: new_p_list shouldn't be []")
         # 线段被裁剪没了的话不该到这一步
         return
     # draw
     painter.setPen(self.penColor)
     for p in item_pixels:
         painter.drawPoint(*p)
     # draw bound
     if self.selected:
         painter.setPen(QColor(255, 0, 0))
         painter.drawRect(self.regionRect(new_p_list))
         pass