I'm trying to draw possible bonds around a selected atom.
I am using cos and sin to find the positions of the possible new atoms in steps of 45° so I get 8 different positions , which works fine. When it comes to actually drawing the double or triple bonds, the bonds at 135° (top left) and 315° (bottom right) are shown as single bond lines, instead of double or triple.
Does anyone have an idea why this bug might occur or how to solve it?
This is a part of my paintEvent function:
# Calculate possible positions for new atoms in 360° around the selected atom
if self.selected_atom is not None:
potential_positions = self.calc_potential_positions(self.selected_atom)
for pos in potential_positions:
# If atoms at position don't overlap, draw the potential bonds and atoms in different colour
if not self.check_atom_overlap(pos[0], pos[1]):
if self.bond_order == 2:
self.draw_double_bond(self.selected_atom.x_coords, self.selected_atom.y_coords, pos[0],
pos[1], False)
elif self.bond_order == 3:
self.draw_triple_bond(self.selected_atom.x_coords, self.selected_atom.y_coords,
pos[0], pos[1], False)
else:
self.draw_single_bond(self.selected_atom.x_coords, self.selected_atom.y_coords,
pos[0], pos[1], False)
self.draw_atom_circle(pos[0], pos[1], self.selected_atom.x_coords,
self.selected_atom.y_coords)
self.draw_atom(pos[0], pos[1], self.element.SYMBOL, True)
self.draw_center_atom(self.selected_atom.x_coords, self.selected_atom.y_coords,
self.selected_atom.symbol)
@staticmethod
def calc_potential_positions(atom: chem_editor_logic.Atom) -> list[tuple[int, int]]:
"""
:param atom:
:return: list of tuples containing x and y coordinates of potential positions for new atoms
"""
x = atom.x_coords
y = atom.y_coords
distance = 40
# Calculate coordinates for angles in steps of 45 degrees from 0 to 360
coordinates_list = []
for angle_degrees in range(0, 360, 45):
angle_radians = math.radians(angle_degrees)
new_x = x + distance * math.cos(angle_radians)
new_y = y + distance * math.sin(angle_radians)
coordinates_list.append((int(new_x), int(new_y)))
print(x, y)
print(coordinates_list)
return coordinates_list
def draw_single_bond(self, atom1_x: int, atom1_y: int, atom2_x: int, atom2_y: int, actual_bond: bool) -> None:
painter = QPainter(self)
pen = QPen()
# Set colour black
pen.setColor(QColor(0, 0, 0))
pen.setWidth(2)
if not actual_bond:
# Set colour red
pen.setColor(QColor(255, 0, 0))
painter.setPen(pen)
painter.drawLine(QPoint(atom1_x, atom1_y), QPoint(atom2_x, atom2_y))
def draw_double_bond(self, atom1_x: int, atom1_y: int, atom2_x: int, atom2_y: int, actual_bond: bool) -> None:
painter = QPainter(self)
pen = QPen()
# Set colour black
pen.setColor(QColor(0, 0, 0))
pen.setWidth(2)
if not actual_bond:
# Set colour red
pen.setColor(QColor(255, 0, 0))
painter.setPen(pen)
# Draw two lines for a double bond
offset = 3
painter.drawLine(QPoint(atom1_x - offset, atom1_y - offset),
QPoint(atom2_x - offset, atom2_y - offset))
painter.drawLine(QPoint(atom1_x + offset, atom1_y + offset),
QPoint(atom2_x + offset, atom2_y + offset))
def draw_triple_bond(self, atom1_x: int, atom1_y: int, atom2_x: int, atom2_y: int, actual_bond: bool) -> None:
painter = QPainter(self)
pen = QPen()
# Set colour black
pen.setColor(QColor(0, 0, 0))
pen.setWidth(2)
if not actual_bond:
# Set colour red
pen.setColor(QColor(255, 0, 0))
painter.setPen(pen)
# Draw three lines for a triple bond
offset = 4
painter.drawLine(QPoint(atom1_x, atom1_y),
QPoint(atom2_x, atom2_y))
painter.drawLine(QPoint(atom1_x - offset, atom1_y - offset),
QPoint(atom2_x - offset, atom2_y - offset))
painter.drawLine(QPoint(atom1_x + offset, atom1_y + offset),
QPoint(atom2_x + offset, atom2_y + offset))
Those are the functions in use here.
