Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I would like to know how to draw a line using the x and y coordinates of two 2-dimensional points. I tried the turtle graphics, but it works using degrees.

like image 568
RezaOptic Avatar asked Oct 20 '15 15:10

RezaOptic


3 Answers

Depending of your needs for plotting you can use matplotlib

import matplotlib.pyplot as plt
plt.plot([x1,x2],[y1,y2])
plt.show()
like image 128
efirvida Avatar answered Oct 29 '22 13:10

efirvida


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

Your premise doesn't 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()
like image 35
cdlane Avatar answered Oct 29 '22 13:10

cdlane


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 line

like image 28
Nick stands with Ukraine Avatar answered Oct 29 '22 12:10

Nick stands with Ukraine