user3488947
user3488947

Reputation: 1

All points along a line at a particular angle

I have got a point (x0,y0). I want to find all points along the line starting at (x0,y0) which is at an angle theta with respect to the x axis. I have got only (x0,y0) and theta at my hand. nothing more. How do I go about this?

Upvotes: 0

Views: 414

Answers (1)

TooTone
TooTone

Reputation: 8126

The cosine of the angle gives you the steps to take in the x-direction, and the sine of the angle gives you the steps to take in the y-direction. It's better to take this approach rather than finding the gradient of the line because for a vertical line the gradient is infinite.

You can't find all points on a computer, because there are an infinite number of them, so you have to decide on a step-size and the number of steps. This is illustrated in the Python program below which chooses a step-size of 1 and 100 steps.

import math, matplotlib.pyplot as plt

def pts(x0,y0,theta):
    t = range(101) # t=0,1,2,3,4,5...,100
    x = [x0 + tt*math.cos(theta) for tt in t]
    y = [y0 + tt*math.sin(theta) for tt in t]
    return x,y

def degrees2radians(degrees):
    return degrees * math.pi/180

degrees = 45
x,y=pts(-100,-100, degrees2radians(degrees))
plt.plot(x, y, label='{} degrees'.format(degrees))

degrees = 90
x,y=pts(100,100, degrees2radians(degrees))
plt.plot(x, y, label='{} degrees'.format(degrees))

plt.xlim(-100,300)
plt.ylim(-100,300)
plt.legend()
plt.show()

and outputs

enter image description here

The R program below takes a similar approach.

drawline=function(x0,y0,theta) {
    t=0:100 # t = 0,1,2,3,4,5,...,100
    # x formed by stepping by cos theta each time
    x=x0 + t*cos(theta)
    # y formed by stepping by sin theta each time
    y=y0 + t*sin(theta)
    # plot
    rng=c(min(x,y),max(x,y)) # range
    plot(y~x,xlim=rng,ylim=rng,type="l")
}

Here, theta is in radians. So drawline(-100,-100,pi/4) corresponds to 45 degrees and gives the first plot, whereas drawline(100,100,pi/2) corresponds to 90 degrees and gives the vertical line shown on the left of the second plot.

enter image description here

enter image description here

Upvotes: 1

Related Questions