python画旋转六边形

python画六边形

我已经把值传入了,可以进行修改,旋转角度必须得填,否则执行不了,若不想旋转图形可以填0,然后点击获取,再点击执行就ok了.
可随意输入六个点坐标
python画旋转六边形_第1张图片

python画旋转六边形_第2张图片

旋转后的效果图

旋转30后python画旋转六边形_第3张图片

注意:

代码中,我是绕(1,1)点进行旋转的

代码

from tkinter import *
import tkinter
import math
op = tkinter.Tk()
top.geometry("500x400")
e = tkinter.Entry()
e2 = tkinter.Entry()
e3 = tkinter.Entry()
e4 = tkinter.Entry()
e5 = tkinter.Entry()
e6 = tkinter.Entry()
e7 = tkinter.Entry()
e8 = tkinter.Entry()
e9 = tkinter.Entry()
e10 = tkinter.Entry()
e1 = tkinter.Entry()
e11 = tkinter.Entry()
e12 = tkinter.Entry()
group = [e, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11]
i = 0
data = [400, 300, 500, 300, 600, 400, 500, 450, 400, 450, 300, 350]
# data=[-20,-10,-100,-60,-150,-150,-200,-88,-120,-50,-77,-12]


j = 0
for each in group:
    each.grid(row=i, column=1, pady=5)
    each.insert(0, data[j])
    j += 1
    i += 1
e12.grid(row=12, column=1, pady=5)
Label(top, text="第1个点x", wraplength=500).grid(row=0, column=0)
Label(top, text="第1个点y", wraplength=500).grid(row=1, column=0)
Label(top, text="第2个点x", wraplength=500).grid(row=2, column=0)
Label(top, text="第2个点y", wraplength=500).grid(row=3, column=0)
Label(top, text="第3个点x", wraplength=500).grid(row=4, column=0)
Label(top, text="第3个点y", wraplength=500).grid(row=5, column=0)
Label(top, text="第4个点x", wraplength=500).grid(row=6, column=0)
Label(top, text="第4个点y", wraplength=500).grid(row=7, column=0)
Label(top, text="第5个点x", wraplength=500).grid(row=8, column=0)
Label(top, text="第5个点y", wraplength=500).grid(row=9, column=0)
Label(top, text="第6个点x", wraplength=500).grid(row=10, column=0)
Label(top, text="第6个点y", wraplength=500).grid(row=11, column=0)
Label(top, text="输入旋转角度 -60—60", wraplength=500).grid(row=12, column=0)


def fun():
    global b1, b2, b3, b4, b5, b6, a7, n1, n2, n3, n4, n5, n6, a8, b7
    global a1, a2, a3, a4, a5, a6, m1, m2, m3, m4, m5, m6
    a7 = int(e12.get()) / 180 * math.pi
    b1 = int(e.get())
    a1 = int(e1.get())
    b2 = int(e2.get())
    a2 = int(e3.get())
    b3 = int(e4.get())
    a3 = int(e5.get())
    b4 = int(e6.get())
    a4 = int(e7.get())
    b5 = int(e8.get())
    a5 = int(e9.get())
    b6 = int(e10.get())
    a6 = int(e11.get())

    # 绕点(1,1)
    a8 = 1
    b7 = 1
    n1 = (b1 - a8) * math.cos(a7) - (a1 - b7) * math.sin(a7) + a8
    m1 = (b1 - a8) * math.sin(a7) + (a1 - b7) * math.cos(a7) + b7
    n2 = (b2 - a8) * math.cos(a7) - (a2 - b7) * math.sin(a7) + a8
    m2 = (b2 - a8) * math.sin(a7) + (a2 - b7) * math.cos(a7) + b7
    n3 = (b3 - a8) * math.cos(a7) - (a3 - b7) * math.sin(a7) + a8
    m3 = (b3 - a8) * math.sin(a7) + (a3 - b7) * math.cos(a7) + b7
    n4 = (b4 - a8) * math.cos(a7) - (a4 - b7) * math.sin(a7) + a8
    m4 = (b4 - a8) * math.sin(a7) + (a4 - b7) * math.cos(a7) + b7
    n5 = (b5 - a8) * math.cos(a7) - (a5 - b7) * math.sin(a7) + a8
    m5 = (b5 - a8) * math.sin(a7) + (a5 - b7) * math.cos(a7) + b7
    n6 = (b6 - a8) * math.cos(a7) - (a6 - b7) * math.sin(a7) + a8
    m6 = (b6 - a8) * math.sin(a7) + (a6 - b7) * math.cos(a7) + b7
inter.Button(top, text="获取", command=fun)
btn2.grid(row=0, column=2)
global points


def dd():
    
    points = [n1, m1, n2, m2, n3, m3, n4, m4, n5, m5, n6, m6]
    root = Tk()
    w = Canvas(
        root,
        width=2000,
        height=2000,
        background="white"
    )
    w.pack()//放在窗口中
      w.create_polygon(//创建多边形函数
        points,
        outline="red",  # 线的颜色
        fill='green',  # 填充色

    )
    mainloop()
btn = tkinter.Button(top, text="执行", command=dd)
btn.grid(row=1, column=2)
tkinter.mainloop()//窗口循环

若有什么不足与错误肯请指正,小白一只,谢谢大家。

你可能感兴趣的:(python)