我想将文本放在行的中央上方(由变量distance
控制)。有一个方法drawLineDescription()
用于此。此方法获取直线的起点和终点,然后计算中心点x, y
。我已经与angle
一起工作,以确保文本放置正确。不幸的是,我不知道如何将文本垂直放置在每一个角度的行上,也就是说,根据旋转,变量x, y
必须能够移动。我如何补充这一点
def drawLineDescription(canvas, startX, startY, endX, endY, distance):
lengthX = endX - startX
lengthY = endY - startY
x = int(startX+((lengthX)/2))
y = int(startY+((lengthY)/2))
angle = math.degrees(math.atan2(lengthY, lengthX)*-1)
angle = round(angle)
if angle < -90 or angle > 90:
angle += 180
canvas.create_text(x, y, angle=angle, font=("Arial", 12), text="exampleText")
最后,它应该是这样的(多行文字示例-这些行从不与文本交叉): Example result
如果按照以下说明使用
tcl > 8.6
,则可以在tkinter上绘制旋转文本:canvas_item = tk.create_text
和canvas.itemconfig(canvas_item, angle=rotation_angle)
为了达到您想要的效果,您需要一些几何图形,特别是线段的坐标、其中点、垂直于线段的偏移向量以及线段的角度
我将计算适当几何元素所需的算法封装在类
point
和c类Vector
中。这些课程不是防弹的,但它们为您提供了基础几何的起点我添加了一个由两点定义的线的示例,文本根据需要放置并旋转以匹配线段的方向
相关问题 更多 >
编程相关推荐