I am attempting to build a Python GUI that creates a canvas where I can draw using a mouse or touch pen. The canvas will mainly consist of a cursive font, such as "Happy Birthday," for my DIY chocolate 3D printer.
Thus far, I have successfully created a canvas where I can draw, which yields coordinates. However, I am having a hard time figuring out an algorithm to detect an intersection. After encountering an intersection, the curve overlaps the existing drawn curve by changing print height so that I can have continuous extrusion. Later, I will add a G-code converter to move my printer.
Although I have tried all existing slicers that I'm aware of, the consistency is not what a continuous printing would yield.
Below, I have included the code for the canvas creation and display of coordinates:
import tkinter as tk
class TrajectoryApp:
def __init__(self, master):
self.master = master
self.canvas_width = 320
self.canvas_height = 280
self.canvas = tk.Canvas(self.master, width=self.canvas_width, height=self.canvas_height, bg='white')
self.canvas.pack(side=tk.LEFT)
self.canvas.bind('<Button-1>', self.start_trajectory)
self.canvas.bind('<B1-Motion>', self.draw_trajectory)
self.canvas.bind('<ButtonRelease-1>', self.end_trajectory)
self.coordinates = []
self.text_area = tk.Text(self.master, height=self.canvas_height // 10, width=30)
self.text_area.pack(side=tk.RIGHT)
def start_trajectory(self, event):
self.coordinates.append([])
self.coordinates[-1].append((event.x, event.y))
def draw_trajectory(self, event):
self.canvas.create_line(self.coordinates[-1][-1][0], self.coordinates[-1][-1][1], event.x, event.y)
self.coordinates[-1].append((event.x, event.y))
def end_trajectory(self, event):
self.display_coordinates()
def display_coordinates(self):
self.text_area.delete(1.0, tk.END)
for trajectory in self.coordinates:
self.text_area.insert(tk.END, "Trajectory:\n")
for point in trajectory:
x = point[0] * (self.canvas_width / self.canvas.winfo_width())
y = point[1] * (self.canvas_height / self.canvas.winfo_height())
self.text_area.insert(tk.END, f"({x:.2f}, {y:.2f})\n")
self.text_area.insert(tk.END, "\n")
if __name__ == '__main__':
root = tk.Tk()
app = TrajectoryApp(root)
root.mainloop()