如何在直线上方和平行于直线的任何角度绘制文本?

2024-10-02 06:37:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将文本放在行的中央上方(由变量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


Tags: 方法text文本mathintdistancecanvasangle
2条回答

如果按照以下说明使用tcl > 8.6,则可以在tkinter上绘制旋转文本:canvas_item = tk.create_textcanvas.itemconfig(canvas_item, angle=rotation_angle)

为了达到您想要的效果,您需要一些几何图形,特别是线段的坐标、其中点、垂直于线段的偏移向量以及线段的角度

我将计算适当几何元素所需的算法封装在类point和c类Vector中。这些课程不是防弹的,但它们为您提供了基础几何的起点

我添加了一个由两点定义的线的示例,文本根据需要放置并旋转以匹配线段的方向

enter image description here

import math
import tkinter as tk


class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __add__(self, other: 'Vector'):
        return Vector(other.x + self.x, other.y + self.y)

    def __sub__(self, other):
        return Vector(self.x - other.x, self.y - other.y)

    def __iter__(self):
        yield self.x
        yield self.y

    def __str__(self):
        return f'{self.__class__.__name__}({self.x}, {self.y})'


class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __add__(self, other):
        return Point(other.x + self.x, other.y + self.y)

    def __sub__(self, other):
        return Vector(other.x - self.x, other.y - self.y)

    def scale(self, scalar):
        return Vector(self.x * scalar, self.y * scalar)

    def normal(self):
        norm = self.norm()
        return Vector(self.x / norm, self.y / norm)

    def norm(self):
        return math.hypot(self.x, self.y)

    def perp(self):
        x, y = self.normal()
        return Vector(y, -x)

    def angle(self):
        return math.atan2(-self.y, self.x) * (180 / math.pi)

    def __iter__(self):
        yield self.x
        yield self.y

    def __str__(self):
        return f'{self.__class__.__name__}({self.x}, {self.y})'


if __name__ == '__main__':

    root = tk.Tk()
    canvas = tk.Canvas(root, width=500, height=500)

    p0, p1 = Point(100, 40), Point(200, 300)
    segment = p1 - p0
    mid_point = segment.scale(0.5) + p0
    # canvas.create_oval(*(mid_point - Vector(2, 2)), *(Vector(2, 2) + mid_point))

    line = canvas.create_line(*p0, *p1)
    offset = segment.perp().scale(20)
    # canvas.create_line(*mid_point, *(mid_point+offset))

    txt = canvas.create_text(*(offset + mid_point), text='example')
    canvas.itemconfig(txt, angle=segment.angle())

    canvas.pack()
    root.mainloop()

enter image description here

lengthX = endX - startX
lengthY = endY - startY
fullLength = math.sqrt(lengthX**2 + lengthY**2)
#unit direction vector
ux = lengthX / fullLength
uy = lengthY / fullLength
#unit normal
if ux < 0:
    nx, ny = -uy, ux
else:
    nx, ny = uy, -ux
#text center point (D at the picture)
cx = x + nx * distance
cy = y + ny * distance

#if you need start of text (S at the picture)
sx = x + nx * distance - ux * halfwidth
sy = y + ny * distance - uy * halfwidth

相关问题 更多 >

    热门问题