用Python绘制时钟

时间:2018-08-30 19:29:58

标签: python svg coordinates clock angle

我正在用python编写一个程序,以svg文件格式绘制时钟。问题是我没有得到显示正确时间的短线。我的代码有什么问题?

这是Inkscape中的样子:

Image of clock in Inkscape

这是我的代码:

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()

0 个答案:

没有答案