python – How can draw a line using the x and y coordinates of two points?

python – How can draw a line using the x and y coordinates of two points?

Depending of your needs for plotting you can use matplotlib

import matplotlib.pyplot as plt
plt.plot([x1,x2],[y1,y2])
plt.show()

I tried the turtle graphics, but it works using degrees.

Your premise doesnt hold — turtle can do it, no degrees needed:

import turtle

point1 = (50, 100)
point2 = (150, 200)

turtle.penup()
turtle.goto(point1)
turtle.pendown()
turtle.goto(point2)

turtle.hideturtle()
turtle.exitonclick()

python – How can draw a line using the x and y coordinates of two points?

You could make use of pygame depending on what you are doing it for as it allows a similar:

line(Surface, color, (x1,y1), (x2,y2), width)

For Example, when the environment has been set up:

pygame.draw.line(screen, (255,0,255), (20,20), (70,80), 2)

can draw:

Test

Leave a Reply

Your email address will not be published. Required fields are marked *