![](/img/trans.png)
[英]How to draw a line on an image given the slope and the intercept coordinates (x, y) using Python
[英]How can draw a line using the x and y coordinates of two points?
我想知道如何使用兩個二維點的 x 和 y 坐標繪制一條線。 我嘗試了海龜圖形,但它使用度數工作。
根據您的繪圖需要,您可以使用matplotlib
import matplotlib.pyplot as plt
plt.plot([x1,x2],[y1,y2])
plt.show()
我嘗試了海龜圖形,但它使用度數工作。
你的前提不成立——烏龜可以做到,不需要學位:
import turtle
point1 = (50, 100)
point2 = (150, 200)
turtle.penup()
turtle.goto(point1)
turtle.pendown()
turtle.goto(point2)
turtle.hideturtle()
turtle.exitonclick()
如果您已經在使用turtle
您可以使用Tkinter
畫布:
import tkinter
x1, y1, x2, y2 = 10, 20, 30, 40
window = tkinter.Tk()
canva = tkinter.Canvas(window)
line = canva.create_line(x1, y1, x2, y2)
canva.pack()
您可以使用以下公式從 4 個點計算角度
angle = arctan((y2-y1)/(x2-x1))
只是一個警告,根據您使用的數學庫,這可能會以弧度輸出。 但是,您可以使用以下公式將弧度轉換為度數。
deg = rad * (180/pi)
為了完整起見,您還可以使用Pillow的ImageDraw 模塊(Python Image Library / PIL fork)。 這樣您就不需要窗口,而是可以將繪制的圖像保存到文件中。
from PIL import Image, ImageDraw
im = Image.new('RGB', (100, 100))
draw = ImageDraw.Draw(im)
draw.line((0, 0) + im.size, fill=128)
draw.line((0, im.size[1], im.size[0], 0), fill=128)
im.save('test.png')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.