Search code examples
pythongeometrypointshapely

Draw a quarter circle at the end of a straight line


I have coordinates of some points. My task is to get the direction of those points and find where future possible points will be located in the calculated direction. To do so, I have planned the following-

  1. Fit a line to the points
  2. Draw a quarter circle at the end of the fitted line. In common sense, the quarter circle might not be the right option to go for. However, it is a part of another problem and has to be solved this way.

I am using the following codes to fit a line

from matplotlib import pyplot as plt
from scipy import stats

x = [1,2,3,2,5,6,7,8,9,10] 
y = [2,4,11,8,8,18,14,11,18,20]
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
line = [slope*i+intercept for i in x]
plt.plot(x, line)

Suppose, the two points on the fitted line is (9,17) and (10,19). How can I draw a quarter circle at (10,19) with a radius of 5 in the direction of the line?

enter image description here

Ultimately, I will have a point location and I have to check whether the point falls inside the quarter circle or not, I assume which could be done with shapely.


Solution

  • To check whether point P falls inside the quarter circle, you can find distance from line end B (length of BP) and cosine of angle between unit line direction vector d and vector BP

    distance = sqrt(BP.x * BP.x + BP.y * BP.y)
    
    cosine = (d.x * BP.x + d.y * BP.y) / (distance)
    
    if (distance < radius) and (cosine >= sqrt(2)/2) 
      P in sector
    

    Unit vector d might be calculated from data you already have:

    d.x = sign(slope) * sqrt(1/(1+slope**2))
    d.y = sqrt(slope**2/1+slope**2)
    

    Note that sign of components is not defined clearly (because two opposite vectors have the same slope)


    To address the main question - end points of arc might be calculated using rotated (by Pi/4) direction vector

    cf = sqrt(2)/2
    arcbegin.x = b.x + radius * d.x * cf - radius * d.y * cf
    arcbegin.y = b.y + radius * d.x * cf + radius * d.y * cf
    arcend.x = b.x + radius * d.x * cf + radius * d.y * cf
    arcend.y = b.y - radius * d.x * cf + radius * d.y * cf