DMU50_GUI_PY/funct_inlay.py
Eduard Gerlitz 635d8e29e9 devl_v3
2025-03-11 14:02:57 +01:00

66 lines
2.8 KiB
Python

import matplotlib.pyplot as plt
import numpy as np
def generate_picture_inlay(all_group_vars, picture_path, DEBUG=False):
try:
vars = all_group_vars['inlay']
x_num, y_num = int(vars['x_num']), int(vars['y_num'])
x_offset_mm, y_offset_mm = float(vars['x_offset']), float(vars['y_offset'])
canvas_width_mm, canvas_height_mm = 800, 1200
x_offset = x_offset_mm / canvas_height_mm # Swapping due to axis swap
y_offset = y_offset_mm / canvas_width_mm # Swapping due to axis swap
if x_num <= 0 or y_num <= 0:
raise ValueError("x_num and y_num must be positive integers.")
fig, ax = plt.subplots(figsize=(12, 8))
ax.set_facecolor('black')
total_x_offset = x_offset * (y_num + 1)
total_y_offset = y_offset * (x_num + 1)
rect_width = (1 - total_y_offset) / x_num
rect_height = (1 - total_x_offset) / y_num
for j in range(x_num):
for i in range(y_num):
# Mirroring horizontally
x_start = 1 - (y_offset + i * (rect_height + y_offset) + rect_height)
y_start = x_offset + j * (rect_width + x_offset)
rect = plt.Rectangle((x_start, y_start), rect_height, rect_width, color='white')
ax.add_patch(rect)
triangle = plt.Polygon([
(x_start + rect_height, y_start), # Bottom right of the rectangle
(x_start + rect_height - 0.05 * rect_height, y_start), # Left along the bottom
(x_start + rect_height, y_start + 0.05 * rect_width) # Up from the bottom right
], color='black')
ax.add_patch(triangle)
# Arrows with adjusted margins
ax.arrow(0.98, 0.02, -0.9, 0, head_width=0.03, head_length=0.02, fc='green', ec='green', linewidth=2, edgecolor='white')
ax.arrow(0.98, 0.02, 0, 0.9, head_width=0.02, head_length=0.03, fc='red', ec='red', linewidth=2, edgecolor='white')
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_xticks([]), ax.set_yticks([])
plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1) # Adjusting the margins
ax.set_xlim(1, 0) # Flipping the x-axis
ax.set_ylim(1, 0) # Flipping the y-axis
if DEBUG:
plt.show()
else:
fig.savefig(picture_path, bbox_inches='tight', pad_inches=0.5, dpi=300, transparent=True) # Adjusted padding
plt.close()
except ValueError as ve:
print(f"Invalid Input: {ve}")
except Exception as e:
print(f"Error generating matrix: {e}")
if __name__ == "__main__":
vars = {'x_num': 5, 'y_num': 3, 'x_offset': 20, 'y_offset': 20}
picture_path = "cfg_picture/TEST_inlay.jpg"
generate_picture_inlay({'inlay': vars}, picture_path, DEBUG=True)