Search code examples
pythonmathcoordinate-systemsalgebra

Find the largest angle made by different points at the center


Below given is an example image where 'center-point' is (x0,y0) (the center of the wheel). Other points are the other ends of the spoke. The distance between 'center-point" and the other end of spoke may be different (spokes of different length). These all points are in cartesian coordinate system.

I need to find here the largest angle made by any two consecutive spoke. In this fig all the angles are same but assume that any one of the spoke is missing, then we will have that angle as the the largest angle at origin.

My take: I am calculating the angle created by each edge with respect to x axis one at a time subtracting with the previous one (that gives angle between two spoke). I am keeping track of the largest angle, everytime updating it if I encounter an angle larger than the previous. My method works but just wondering if any efficient method is available to find the same.

Wheel with spokes


Solution

  • Assuming you want the angle between two spokes, I suggest you convert the data points to polar/complex co-ordinates, this is made easy in the cmath module, and allows you to do something like this (phase takes out just the angle about centre):

    import cmath
    
    def largest_spoke_angle(centre, peripheral):
         per_from_centre = [complex(z[0]-centre[0], z[1]-centre[1]) for z in peripheral]
         per_angles = [cmath.phase(z) for z in per_from_centre]
         per_angles.sort()
    
         differences = [ per_angles[n+1]-per_angles[n] for n in range(len(per_angles)-1)] \
                        + [per_angles[0] +2*cmath.pi - per_angles[-1]]
    
         return max(differences)#in radians
    
    centre = (0.,0.)
    peripheral = [(1.,2.),(3.,4.),(3.,5.)]
    print largest_spoke_angle(centre, peripheral)