用python绘制时钟
我正在用python编写一个程序,以svg文件格式绘制时钟。问题是我没有收到显示时间到正确地点的短线。我的代码有什么问题 以下是Inkscape中的外观: 这是我的代码:用python绘制时钟,python,svg,coordinates,clock,angle,Python,Svg,Coordinates,Clock,Angle,我正在用python编写一个程序,以svg文件格式绘制时钟。问题是我没有收到显示时间到正确地点的短线。我的代码有什么问题 以下是Inkscape中的外观: 这是我的代码: import svgwrite import math #Definitions filename = 'inkscape_007.svg' WIDTH = 1000 HEIGHT = 1000 # Open the drawing dwg = svgwrite.Drawing(filename) dwg.viewbo
import svgwrite
import math
#Definitions
filename = 'inkscape_007.svg'
WIDTH = 1000
HEIGHT = 1000
# Open the drawing
dwg = svgwrite.Drawing(filename)
dwg.viewbox(width=WIDTH, height=HEIGHT)
#Drawing code
circle = dwg.circle(center=(500, 500), r='250', fill='none',stroke='darkred',
stroke_width=10)
dwg.add(circle)
dwg.save()
def x_coord(angle,radius,center_x):
x = radius * math.cos(angle) + center_x
x = round(x)
return x
def y_coord(angle,radius,center_y):
y = radius * math.sin(angle) + center_y
y = round(y)
return y
ang = 0
line = dwg.line(start=(x_coord(ang,250,500),y_coord(ang,250,500)), end=
(x_coord(ang,220,500),y_coord(ang,220,500)), stroke='black', stroke_width=10)
dwg.add(line)
dwg.save()
ang = 90
line = dwg.line(start=(x_coord(ang,250,500),y_coord(ang,250,500)), end=
(x_coord(ang,220,500),y_coord(ang,220,500)), stroke='black', stroke_width=10)
dwg.add(line)
dwg.save()
ang = 180
line = dwg.line(start=(x_coord(ang,250,500),y_coord(ang,250,500)), end=
(x_coord(ang,220,500),y_coord(ang,220,500)), stroke='black', stroke_width=10)
dwg.add(line)
dwg.save()
ang = 360
line = dwg.line(start=(x_coord(ang,250,500),y_coord(ang,250,500)), end=
(x_coord(ang,220,500),y_coord(ang,220,500)), stroke='black', stroke_width=10)
dwg.add(line)
dwg.save()
#Save the file
dwg.save()
Python的所有三角函数都是以弧度而不是度来操作的。乍一看,这里缺少的似乎是从小时到角度坐标(弧度或度)的时间转换。我最近用JavaScript制作了类似的时钟。。我不知道这是否有用,但你可以在这里看到我的代码:这渲染了这里看到的时钟:正如Jason Harper所建议的,你需要以弧度而不是度数传递那些三角函数的角度。数学模块具有执行转换的
度
和弧度
函数。谢谢!这解决了我的问题。我在两个函数中都添加了一行:angle=math.radians(angle),这似乎解决了问题。