def to_qpoint(self, point: Point): """ Преобразования точки в координаты холста(пиксели) :param: point точка в декартовых координатах """ x = point.x() * DrawConst.scaleX + self.o_x y = -point.y() * DrawConst.scaleY + self.o_y return QPoint(x, y)
def main(): x1, y1, x2, y2 = map(int, input().split()) line = Segment(Point(x1, y1), Point(x2, y2)) q = int(input()) for _ in range(q): x0, y0 = map(int, input().split()) p = line.project(Point(x0, y0)) print(p.x, p.y)
def main(): from geometric import Segment, Point x1, y1, x2, y2 = map(int, input().split()) line = Segment(Point(x1, y1), Point(x2, y2)) q = int(input()) for _ in range(q): x0, y0 = map(int, input().split()) p = line.reflect(Point(x0, y0)) print(p.x, p.y)
def main(): q = int(input()) for _ in range(q): x0, y0, x1, y1, x2, y2, x3, y3 = map(int, input().split()) s1 = Segment(Point(x0, y0), Point(x1, y1)) s2 = Segment(Point(x2, y2), Point(x3, y3)) if s1.isOrthogonal(s2): print(2) elif s1.isParallel(s2): print(1) else: print(0)
def to_decart(self, qpoint: QPoint): """ Преобразования точки в декартовы координаты :param: qpoint точка в координатах холста(пиксели) """ x = (qpoint.x() - self.o_x) / DrawConst.scaleX y = (-qpoint.y() + self.o_y) / DrawConst.scaleY return Point(x, y)
def drawBresenhamCircle(self): self.clearCanvas() x = int(self.ui.le_circle_x.text()) y = int(self.ui.le_circle_y.text()) r = int(self.ui.le_circle_r.text()) painter = QPainter() painter.begin(self.pixmap) self.drawCanvas(painter) self.drawEllipse(painter, Point(x, y), r) self.BresenhamCircle(painter, Point(x, y), r) painter.end() self.setPixmap()
def drawBresenhamLine(self): self.clearCanvas() x1 = int(self.ui.le_line_x1.text()) y1 = int(self.ui.le_line_y1.text()) x2 = int(self.ui.le_line_x2.text()) y2 = int(self.ui.le_line_y2.text()) painter = QPainter() painter.begin(self.pixmap) self.drawCanvas(painter) self.drawLine(painter, Point(x1, y1), Point(x2, y2)) self.BresenhamLine(painter, Point(x1, y1), Point(x2, y2)) painter.end() self.setPixmap()
def _init_canvas(self, w, h): """ Начальная инициализиция холста :param: w ширина холста :param: h высота холста """ self.pixmap = QPixmap(w, h) self.pixmap.fill(Qt.white) painter = QPainter() painter.begin(self.pixmap) self.drawCanvas(painter) a: Point = Point(0, 0) b: Point = Point(-3, -6) # self.drawLine(painter, a, b) # self.BresenhamLine(painter, a, b) # self.drawEllipse(painter, a, 4) # self.BresenhamCircle(painter, a, 4) painter.end() self.setPixmap()
def rotateFromPoint(self): """ Метод для поворота фигуры относительно (x, y) """ try: x = float(self.ui.le_rotate_from_point_x.text()) y = float(self.ui.le_rotate_from_point_y.text()) angle = float(self.ui.le_rotate_angle.text()) p_rotate_from = partial(rotate_from, angle=angle, x=Point(x, y)) figure = list(map(p_rotate_from, self._get_figure())) self.drawFigure(figure) except ValueError as ex: self.ui.le_rotate_from_point_x.setText('') self.ui.le_rotate_from_point_y.setText('') self.ui.le_rotate_angle.setText('') print(str(ex)) except Exception as ex: print('Unknown error: ' + str(ex))
def __init__(self, parent=None): QWidget.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) # размеры холста (ширрина и высота) в пикселях self.width = self.ui.canvas.width() self.height = self.ui.canvas.height() self.o_x = int(self.width * DrawConst.centerX) self.o_y = int(self.height * DrawConst.centerY) self.c.connect(lambda i: print('pyqtSignal', i)) self.c.emit(Point(0, 0)) self._init_canvas(self.width, self.height) self.points = [] self.ui.pb_draw_line.clicked.connect(self.drawBresenhamLine) self.ui.pb_draw_circle.clicked.connect(self.drawBresenhamCircle)
def BresenhamLine(self, painter, a: Point, b: Point): x1, y1 = a.x(), a.y() x2, y2 = b.x(), b.y() dx = x2 - x1 dy = y2 - y1 sign_x = 1 if dx > 0 else -1 if dx < 0 else 0 sign_y = 1 if dy > 0 else -1 if dy < 0 else 0 if dx < 0: dx = -dx if dy < 0: dy = -dy if dx > dy: pdx, pdy = sign_x, 0 es, el = dy, dx else: pdx, pdy = 0, sign_y es, el = dx, dy x, y = x1, y1 error, t = el, 0 self.drawPoint(painter, Point(x, y), Qt.red) while t < el: error -= 2 * es if error < 0: error += 2 * el x += sign_x y += sign_y else: x += pdx y += pdy t += 1 self.drawPoint(painter, Point(x, y), Qt.red)
from geometric import Segment, Point q = int(input()) for _ in range(q): x0, y0, x1, y1, x2, y2, x3, y3 = map(int, input().split()) s1 = Segment(Point(x0, y0), Point(x1, y1)) s2 = Segment(Point(x2, y2), Point(x3, y3)) print(s1.getDistance(s2))
def BresenhamCircle(self, painter, a: Point, r: int): x, y = r, 0 error = 1 - x while x >= y: self.drawPoint(painter, Point(x + a.x(), y + a.y())) self.drawPoint(painter, Point(y + a.x(), x + a.y())) self.drawPoint(painter, Point(-x + a.x(), y + a.y())) self.drawPoint(painter, Point(-y + a.x(), x + a.y())) self.drawPoint(painter, Point(-x + a.x(), -y + a.y())) self.drawPoint(painter, Point(-y + a.x(), -x + a.y())) self.drawPoint(painter, Point(x + a.x(), -y + a.y())) self.drawPoint(painter, Point(y + a.x(), -x + a.y())) y += 1 if error < 0: error += 2 * y + 1 else: x -= 1 error += 2 * (y - x + 1)