본문 바로가기
카테고리 없음

[파이썬 프로젝트] 베지에 곡선 그려주는 GUI 프로그램_2차시

by 라코 Laco 2024. 3. 10.

베지에 곡선까지 그려주는 완전한 프로그램이다. 아래는 소스코드다. 이전에 점 연결하는데 까지의 소스코드가 궁금하다면 "[파이썬 프로제그] 베지에 곡선 그려주는 GUI 프로그램_1차시"를 참고하기 바란다. 간단한 개발일지 같은 것이니 주석 같은걸 따로 첨부하지는 않겠다. beziercal은 본인이 만든 프로그램인데 무시해도 된다. 아래는 실행화면이다.

 

 

2021.10.30 - [프로그래밍] - [파이썬 프로젝트] 베지에 곡선 그려주는 GUI 프로그램_1차시

 

[파이썬 프로젝트] 베지에 곡선 그려주는 GUI 프로그램_1차시

아직 베지에 곡선을 그려주지는 못하고 점 찍으면 그 점들을 선으로만 연결해 준다. 베지에 곡선 구현은 2차시 때 하겠다. 아래는 1차시 소스코드 from tkinter import * point_x = [] point_y = [] i = 0 def point

study-log123.tistory.com

 

 

from tkinter import *
from beziercal import bezier

if __name__=="__main__":
    point_x = []
    point_y = []
    
    def delete(event):
        global point_x, point_y
        canvas.delete('all')
        point_x = []
        point_y = []

    def point(event):
        canvas.delete("all")
        global point_x, point_y
        point_x.append(event.x)
        point_y.append(event.y)

        canvas.create_oval(event.x-5, event.y-5, event.x+5, event.y+5, fill="red")

        if len(point_y) > 1:
            for i in range(1,len(point_x)):
                canvas.create_oval(point_x[i-1]-5, point_y[i-1]-5, point_x[i-1]+5, point_y[i-1]+5, fill="red")
                canvas.create_line(point_x[i-1], point_y[i-1], point_x[i], point_y[i], fill="red")
        t=0
        xlis=[]
        ylis=[]
        while t<=1:
            xl=[]
            yl=[]
            for id in point_x:
                xl.append(id)
            for id in point_y:
                yl.append(id)
            xlis.append(bezier(xl, yl, t)[0])
            ylis.append(bezier(xl, yl, t)[1])
            t+=0.001
        
        for ind in range(1, len(xlis)):
            canvas.create_line(xlis[ind-1],ylis[ind-1], xlis[ind], ylis[ind], fill="yellow")
        

    cur_x = 250
    cur_y = 250

    window = Tk()
    window.title("Bezier-curve")

    canvas = Canvas(window, width=500, height=500, bg="black", relief="sunken", bd=5)
    canvas.pack()
    canvas.bind("<Button>", point)
    window.bind("<s>", delete)

    window.mainloop()

 

아래는 exe 파일이다. 파이썬 파일을 exe파일로 바꾸어 배포하는 방법은

1. widow+r, cmd치고 enter로 명령 프롬프트 들어가기

2. pip install pyinstaller 치고 enter

3. pyinstaller -F -w --icon=icon_name.확장자 project_name.py(ico이외에 png확장자를 쓰려면 따로 설치해야하는 라이브러리가 있는데 이는 그냥 enter치면 끝줄에서 설명해 준다)

 

아래는 베지어 커브 생성기 exe파일이다. 사용법은 클릭으로 곡선을 생성하고 s를 눌러 모두 지울 수 있다. 그럼 잘 사용해보길. 다음 프로젝트는 이 베지에 곡선을 이용해 손떨림 보정기를 만들어 볼 생각이다.

 

bezier-curve.exe
7.95MB

댓글