Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting a line from a coordinate with and angle

I basically want to plot a line from a coordinate (x, y) with a given angle (calculating the tangent value).

With a simple line of code like this pl.plot([x1, x2], [y1, y2], 'k-', lw=1) I can plot a line between two points but for this I need to calculate (x2, y2) coordinate. My (x1, y1) coordinate is fixed and the angle is known. Calculating (x2, y2) causes a problem at some point so I just want to plot the line from (x1, y1) with an angle (and preferably with a length).

The simplest solution I came up with that was to use point-slope function which is y - y1 = m(x - X1). Interpreting thiss and searching a little I used this piece of code:

x1 = 10
y1 = -50
angle = 30

sl = tan(radians(angle))
x = np.array(range(-10,10))
y = sl*(x-x1) + y1

pl.plot(x,y)
pl.show

sl is here slope and x1 and y1 are the coordinates. I needed to explain myself since this found to be a poor question.

So now, any ideas on how I can do/solve that?

like image 224
H.Aziz Kayıhan Avatar asked Dec 24 '22 23:12

H.Aziz Kayıhan


1 Answers

I'm not really sure what exactly you want from the explanation, but I think this will do something close to what you asked for.

You should use trigonometry to get the new point if you know the angle and length of a line you want to use.

import numpy as np
import math
import matplotlib.pyplot as plt


def plot_point(point, angle, length):
     '''
     point - Tuple (x, y)
     angle - Angle you want your end point at in degrees.
     length - Length of the line you want to plot.

     Will plot the line on a 10 x 10 plot.
     '''

     # unpack the first point
     x, y = point

     # find the end point
     endy = y + length * math.sin(math.radians(angle))
     endx = length * math.cos(math.radians(angle))

     # plot the points
     fig = plt.figure()
     ax = plt.subplot(111)
     ax.set_ylim([0, 10])   # set the bounds to be 10, 10
     ax.set_xlim([0, 10])
     ax.plot([x, endx], [y, endy])

     fig.show()
like image 114
veda905 Avatar answered Jan 07 '23 10:01

veda905