python中的罪和cos的功能不为我工作?

发布于 2025-01-18 11:05:26 字数 1680 浏览 3 评论 0原文

我是这种数学的新手,在学校喷气机中没有它。因此,我的问题是从球形坐标到GRRID的变化(半径和角度到X和Y)。首先,我以偶数的角度驱使圆圈。直到这里一切都起作用。当它们转换时,它们与中心的距离都相同,但接缝是随机旋转的。这可以很好地看出,数量很少,例如4。 该项目的最终目的是为我需要一个带有积分的圆圈画有心脏。所以这里我的代码:

from tkinter import *
from math import *
import round_canvas as round

#settings
speed = 1 # in ms
window_size = 700
cellamount = 20
cell = window_size / cellamount
r = 300
pointSize = 20
animationSize = window_size - cell*4

#colors:
#48979b
#7cbeba
#a7c7b3
#d9beac
#f5e6d9

#window
win = Tk()
win.title("heart")
win.resizable(False, False)
win.geometry(str(window_size) + "x" + str(window_size))
win.configure(background="#f5e6d9")

# variables
point_amount = 4
point_cords = [] # saves point rotations in list (place in list coresponds to number of point)

# canvas
canvas = Canvas(win, width=window_size, height=window_size, bg="#f5e6d9", 
highlightthickness=0)
canvas.place(x=0,y=0)
canvas_overlay = round.round_rectangle(canvas, cell, cell, cell*cellamount-cell, 
cell*cellamount-cell, radius=40, fill="#d9beac")

# functions:
def drawPoints():
    for point in range(0, point_amount):
        #print(point)
        localPoint = 360/(point_amount)*point
        point_cords.append(localPoint)

def showPoints():
    for angle in point_cords:
        print(angle)
        localX = cos(angle)*r+window_size/2 # https://www.youtube.com/watch?v=O5wjXoFrau4&t=635s
        localY = sin(angle)*r+window_size/2
        print(localX, localY)
        visiblePoint = canvas.create_oval(localX-pointSize/2, localY-pointSize/2, localX+pointSize/2, localY+pointSize/2, width=0, fill="#7cbeba")

# main
def main():
    drawPoints()
    showPoints()

win.after(speed,main)
win.mainloop()

I am new to this kind of mathematics and didn't have it in school jet. So my problem is the change from spherical coordinates to a grrid (radius and angle to x and y). First I devide the circle in even angles. Until here everything works. When they get transformed they have all the same distance to the center but seam to be random in rotation. This can be well seen with low numbers like 4.
The final purpose of the project will be to draw a cardioid for that I need a circle with points. So here my code:

from tkinter import *
from math import *
import round_canvas as round

#settings
speed = 1 # in ms
window_size = 700
cellamount = 20
cell = window_size / cellamount
r = 300
pointSize = 20
animationSize = window_size - cell*4

#colors:
#48979b
#7cbeba
#a7c7b3
#d9beac
#f5e6d9

#window
win = Tk()
win.title("heart")
win.resizable(False, False)
win.geometry(str(window_size) + "x" + str(window_size))
win.configure(background="#f5e6d9")

# variables
point_amount = 4
point_cords = [] # saves point rotations in list (place in list coresponds to number of point)

# canvas
canvas = Canvas(win, width=window_size, height=window_size, bg="#f5e6d9", 
highlightthickness=0)
canvas.place(x=0,y=0)
canvas_overlay = round.round_rectangle(canvas, cell, cell, cell*cellamount-cell, 
cell*cellamount-cell, radius=40, fill="#d9beac")

# functions:
def drawPoints():
    for point in range(0, point_amount):
        #print(point)
        localPoint = 360/(point_amount)*point
        point_cords.append(localPoint)

def showPoints():
    for angle in point_cords:
        print(angle)
        localX = cos(angle)*r+window_size/2 # https://www.youtube.com/watch?v=O5wjXoFrau4&t=635s
        localY = sin(angle)*r+window_size/2
        print(localX, localY)
        visiblePoint = canvas.create_oval(localX-pointSize/2, localY-pointSize/2, localX+pointSize/2, localY+pointSize/2, width=0, fill="#7cbeba")

# main
def main():
    drawPoints()
    showPoints()

win.after(speed,main)
win.mainloop()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ζ澈沫 2025-01-25 11:05:26

Python 的三角函数以 弧度 计算角度,而不是度数。

您可以使用 math.radiansmath. Degrees

>>> math.sin(math.radians(45))
0.7071067811865476

Python's trig functions take their angles in radians, not degrees.

You can easily convert between the two units using math.radians and math.degrees.

>>> math.sin(math.radians(45))
0.7071067811865476
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文