- Added distance and quadrant

- added line distance to drawn lines
This commit is contained in:
bklronin
2024-06-26 17:42:13 +02:00
parent ac9176fbd3
commit 3e85a11787
4 changed files with 251 additions and 197 deletions

View File

@@ -72,6 +72,11 @@ class SketchWidget(QWidget):
def distance(self, p1, p2):
return math.sqrt((p1.x() - p2.x())**2 + (p1.y() - p2.y())**2)
def calculate_midpoint(self, point1, point2):
mx = (point1.x() + point2.x()) // 2
my = (point1.y() + point2.y()) // 2
return QPoint(mx, my)
def is_point_on_line(self, p, p1, p2, tolerance=5):
# Calculate the lengths of the sides of the triangle
a = self.distance(p, p1)
@@ -137,18 +142,19 @@ class SketchWidget(QWidget):
"""print("Initial slv_points_main:", self.slv_points_main)
print("Change list:", point_list)"""
for tbu_points_idx in point_list:
# Each tbu_points_idx is a tuple: (index, old_point, new_point)
index, old_point, new_point = tbu_points_idx
"""print("Updating index:", index)
print("Old point:", old_point)
print("New point:", new_point)"""
if len(point_list) > 0:
for tbu_points_idx in point_list:
# Each tbu_points_idx is a tuple: (index, old_point, new_point)
index, old_point, new_point = tbu_points_idx
"""print("Updating index:", index)
print("Old point:", old_point)
print("New point:", new_point)"""
# Update the point in slv_points_main
self.slv_points_main[index]['ui_point'] = new_point
# Update the point in slv_points_main
self.slv_points_main[index]['ui_point'] = new_point
# Print updated state
#print("Updated slv_points_main:", self.slv_points_main)
# Print updated state
#print("Updated slv_points_main:", self.slv_points_main)
"""# UPDATE UI POINTS with solver points
for i in range(len(self.slv_points_main) + len(self.slv_lines_main)):
@@ -436,6 +442,9 @@ class SketchWidget(QWidget):
elif self.solv.solve() == ResultFlag.INCONSISTENT:
print("Solve_failed - Incons")
if event.button() == Qt.LeftButton and self.mouse_mode == "pb_con_mid":
points_need_update = check_all_points()
print("This", points_need_update)
@@ -514,19 +523,21 @@ class SketchWidget(QWidget):
painter.drawLine(middle_x, 0, middle_x, self.height())
# Draw tick marks
tick_length = 10
tick_spacing = 50
tick_length = int(10 * self.zoom)
tick_spacing = int(50 * self.zoom)
pen = QPen(Qt.gray, 1, Qt.SolidLine)
painter.setPen(pen)
# Draw tick marks on the X axis
for x in range(0, self.width(), tick_spacing):
painter.drawLine(x, middle_y - tick_length // 2, x, middle_y + tick_length // 2)
# Draw tick marks on the X axis to the right and left from the middle point
for x in range(0, self.width() // 2, tick_spacing):
painter.drawLine(middle_x + x, middle_y - tick_length // 2, middle_x + x, middle_y + tick_length // 2)
painter.drawLine(middle_x - x, middle_y - tick_length // 2, middle_x - x, middle_y + tick_length // 2)
# Draw tick marks on the Y axis
for y in range(0, self.height(), tick_spacing):
painter.drawLine(middle_x - tick_length // 2, y, middle_x + tick_length // 2, y)
# Draw tick marks on the Y axis upwards and downwards from the middle point
for y in range(0, self.height() // 2, tick_spacing):
painter.drawLine(middle_x - tick_length // 2, middle_y + y, middle_x + tick_length // 2, middle_y + y)
painter.drawLine(middle_x - tick_length // 2, middle_y - y, middle_x + tick_length // 2, middle_y - y)
# Draw the origin point in red
painter.setPen(QPen(Qt.red, 4))
@@ -553,11 +564,6 @@ class SketchWidget(QWidget):
# Apply the zoom factor
painter.scale(self.zoom, self.zoom)
# Set the background color
#painter.fillRect(0, self.width(), 0, self.height(), QColor('black'))
# Draw axes
pen = QPen(Qt.gray)
pen.setWidth(2 / self.zoom)
painter.setPen(pen)
@@ -567,7 +573,14 @@ class SketchWidget(QWidget):
painter.drawEllipse(point['ui_point'], 3 / self.zoom, 3 / self.zoom)
for dic in self.slv_lines_main:
painter.drawLine(dic['ui_points'][0] , dic['ui_points'][1])
p1 = dic['ui_points'][0]
p2 = dic['ui_points'][1]
painter.drawLine(p1, p2)
dis = self.distance(p1, p2)
mid = self.calculate_midpoint(p1, p2)
painter.drawText(mid, str(round(dis, 2)))
pen = QPen(Qt.green)
pen.setWidth(2)
@@ -587,7 +600,7 @@ class SketchWidget(QWidget):
painter.setPen(highlight_pen)
painter.drawEllipse(self.hovered_point, 5 / self.zoom, 5 / self.zoom)
if self.selected_line:
if self.selected_line and not self.hovered_point:
p1, p2 = self.selected_line
painter.setPen(QPen(Qt.red, 2))
painter.drawLine(p1, p2)