简体   繁体   中英

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.

带辐条的车轮

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)

I think I would do something like this:

angles = [get_angle_from_xaxis(origin,point) for point in points]
#make sure the angles are in order
angles.sort()  
#need to compare last one with first one
angles.insert(0,angles[-1]-360.0)  #360 if degrees, otherwise 2*math.pi.
#Now calculate the difference between adjacent angles and take the maximum
maxangle = max( angles[i] - angle for i,angle in enumerate(angles[:-1],1) )

This is basically the solution you describe. The only thing I've added is a check between the last and first and a sort to make sure we have the angles in the right order.

The answer of @user1597034 is correct. But it's not possible to get which spokes resulted in the largest angle.

The code below finds the indices of the two vectors of largest angle:

import cmath
import numpy as np

center = (0.,0.)
peripheral = np.array([(-1.,-1.),(0.,1.),(1.,-0.55), (0,-1), (-1,1)])


per_from_centre = [complex(z[0]-center[0], z[1]-center[1]) for z in peripheral]
per_angles = [cmath.phase(z) for z in per_from_centre]
id_ord = np.argsort(per_angles,axis=-1) # order index 
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]]

# ----- so far, same code in relation to @user1597034  -----

# find index of adjacent angles of greater angle
max_value = max(differences) # maximum value
for i in range(len(differences)):
    if max_value == differences[i]:
        if i == (len(differences)-1):
            pairs = [id_ord[0], id_ord[-1]]
        else:
            pairs = [id_ord[i]] + [id_ord[i+1]]
        print('pair index of largest angle:',pairs)

在此处输入图片说明

pair index of largest angle: [2, 1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM