Boredom got a new best friend

Added rotation to the robots
This commit is contained in:
Daniel Carta
2024-07-02 16:53:07 -06:00
parent fbeef10b97
commit 24de5bc949
+67 -1
View File
@@ -43,12 +43,16 @@ class MainWindow(QMainWindow):
# Variables # Variables
self.coordinates = np.empty((0, 2), dtype=int) self.coordinates = np.empty((0, 2), dtype=int)
self.robot_size = 35 self.robot_size = 35
self.handle_size = 10 self.handle_size = 15
self.rotation_handle_distance = 35
self.handles = [] self.handles = []
self.rotation_handles = []
self.dragging_handle = False self.dragging_handle = False
self.dragging_handle_index = -1 self.dragging_handle_index = -1
self.dragging_robot = False self.dragging_robot = False
self.dragging_robot_index = -1 self.dragging_robot_index = -1
self.dragging_rotation_handle = False
self.dragging_rotation_handle_index = -1
def mousePressEvent(self, event: QMouseEvent): def mousePressEvent(self, event: QMouseEvent):
pos = self.image_label.mapFrom(self, event.position().toPoint()) pos = self.image_label.mapFrom(self, event.position().toPoint())
@@ -59,6 +63,7 @@ class MainWindow(QMainWindow):
self.coordinates = np.vstack((self.coordinates, [x, y])) self.coordinates = np.vstack((self.coordinates, [x, y]))
if len(self.coordinates) >= 2: if len(self.coordinates) >= 2:
self.calculate_handle_pos() self.calculate_handle_pos()
self.calculate_rotation_handle_pos()
self.draw_scene() self.draw_scene()
elif event.button() == Qt.LeftButton: elif event.button() == Qt.LeftButton:
handle_index = self.is_point_in_handle(x, y) handle_index = self.is_point_in_handle(x, y)
@@ -70,6 +75,11 @@ class MainWindow(QMainWindow):
if robot_index != -1: if robot_index != -1:
self.dragging_robot = True self.dragging_robot = True
self.dragging_robot_index = robot_index self.dragging_robot_index = robot_index
else:
rotation_handle_index = self.is_point_in_rotation_handle(x, y)
if rotation_handle_index != -1:
self.dragging_rotation_handle = True
self.dragging_rotation_handle_index = rotation_handle_index
def mouseMoveEvent(self, event: QMouseEvent): def mouseMoveEvent(self, event: QMouseEvent):
if self.dragging_handle: if self.dragging_handle:
@@ -82,6 +92,22 @@ class MainWindow(QMainWindow):
x, y = pos.x(), pos.y() x, y = pos.x(), pos.y()
self.coordinates[self.dragging_robot_index] = [x, y] self.coordinates[self.dragging_robot_index] = [x, y]
self.calculate_handle_pos() self.calculate_handle_pos()
self.calculate_rotation_handle_pos()
self.draw_scene()
elif self.dragging_rotation_handle:
pos = self.image_label.mapFrom(self, event.position().toPoint())
x, y = pos.x(), pos.y()
robot_x, robot_y = self.coordinates[self.dragging_rotation_handle_index]
# Calculate the angle between the robot and the rotation handle
dx = x - robot_x
dy = y - robot_y
angle = np.arctan2(dy, dx)
# Update the position of the rotation handle based on the angle and distance
rotation_handle_x = int(robot_x + self.rotation_handle_distance * np.cos(angle))
rotation_handle_y = int(robot_y + self.rotation_handle_distance * np.sin(angle))
self.rotation_handles[self.dragging_rotation_handle_index] = QPoint(rotation_handle_x, rotation_handle_y)
self.draw_scene() self.draw_scene()
def mouseReleaseEvent(self, event: QMouseEvent): def mouseReleaseEvent(self, event: QMouseEvent):
@@ -90,6 +116,15 @@ class MainWindow(QMainWindow):
self.dragging_handle_index = -1 self.dragging_handle_index = -1
self.dragging_robot = False self.dragging_robot = False
self.dragging_robot_index = -1 self.dragging_robot_index = -1
self.dragging_rotation_handle = False
self.dragging_rotation_handle_index = -1
def is_point_in_rotation_handle(self, x, y):
for i, handle in enumerate(self.rotation_handles):
if (abs(x - handle.x()) <= self.handle_size // 2 and
abs(y - handle.y()) <= self.handle_size // 2):
return i
return -1
def is_point_in_handle(self, x, y): def is_point_in_handle(self, x, y):
for i, handle in enumerate(self.handles): for i, handle in enumerate(self.handles):
@@ -111,6 +146,20 @@ class MainWindow(QMainWindow):
x2, y2 = self.coordinates[-1] x2, y2 = self.coordinates[-1]
self.handles.append(QPoint((x1 + x2) // 2, (y1 + y2) // 2)) self.handles.append(QPoint((x1 + x2) // 2, (y1 + y2) // 2))
def calculate_rotation_handle_pos(self):
for i, (x, y) in enumerate(self.coordinates):
if i >= len(self.rotation_handles):
angle = np.pi / 4
rotation_handle_x = int(x + self.rotation_handle_distance * np.cos(angle))
rotation_handle_y = int(y - self.rotation_handle_distance * np.sin(angle))
self.rotation_handles.append(QPoint(rotation_handle_x, rotation_handle_y))
else:
# Update the position of existing rotation handles
angle = np.pi / 4
rotation_handle_x = int(x + self.rotation_handle_distance * np.cos(angle))
rotation_handle_y = int(y - self.rotation_handle_distance * np.sin(angle))
self.rotation_handles[i] = QPoint(rotation_handle_x, rotation_handle_y)
def draw_scene(self): def draw_scene(self):
self.pixmap = QPixmap(os.path.join(os.path.dirname(os.path.abspath(__file__)), "images", "Field.png")) self.pixmap = QPixmap(os.path.join(os.path.dirname(os.path.abspath(__file__)), "images", "Field.png"))
painter = QPainter(self.pixmap) painter = QPainter(self.pixmap)
@@ -159,11 +208,28 @@ class MainWindow(QMainWindow):
painter.drawText(QRect(top_left_x, top_left_y, self.robot_size, self.robot_size), painter.drawText(QRect(top_left_x, top_left_y, self.robot_size, self.robot_size),
Qt.AlignCenter, str(i + 1)) Qt.AlignCenter, str(i + 1))
for i, handle in enumerate(self.rotation_handles):
painter.setBrush(QColor(255, 0, 0)) # Red color for rotation handle
painter.drawEllipse(
handle,
self.handle_size // 2,
self.handle_size // 2
)
painter.setBrush(Qt.NoBrush)
# Draw line connecting robot and rotation handle
robot_x, robot_y = self.coordinates[i]
painter.drawLine(
QPoint(robot_x, robot_y),
handle
)
self.image_label.setPixmap(self.pixmap) self.image_label.setPixmap(self.pixmap)
def clear_points(self): def clear_points(self):
self.coordinates = np.empty((0, 2), dtype=int) self.coordinates = np.empty((0, 2), dtype=int)
self.handles = [] self.handles = []
self.rotation_handles = []
self.pixmap = QPixmap(os.path.join(os.path.dirname(os.path.abspath(__file__)), "images", "Field.png")) self.pixmap = QPixmap(os.path.join(os.path.dirname(os.path.abspath(__file__)), "images", "Field.png"))
self.image_label.setPixmap(self.pixmap) self.image_label.setPixmap(self.pixmap)