Changeset 2454 in lmdz_wrf for trunk/tools/geometry_tools.py


Ignore:
Timestamp:
Apr 21, 2019, 11:08:54 PM (6 years ago)
Author:
lfita
Message:

Adding:

  • `p_reg_polygon': Function to provide a regular polygon of Nv vertices
  • `p_reg_star': Function to provide a regular star of Nv vertices
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/geometry_tools.py

    r2453 r2454  
    4141# ellipse_polar: Function to determine an ellipse from its center and polar coordinates
    4242# p_circle: Function to get a polygon of a circle
     43# p_reg_polygon: Function to provide a regular polygon of Nv vertices
     44# p_reg_star: Function to provide a regular star of Nv vertices
    4345# p_square: Function to get a polygon square
    4446# p_spiral: Function to provide a polygon of an Archimedean spiral
     
    511513
    512514    return spiral
     515
     516def p_reg_polygon(Nv, lf, N=50):
     517    """ Function to provide a regular polygon of Nv vertices
     518      Nv: number of vertices
     519      lf: length of the face
     520      N: number of points
     521    """
     522    fname = 'p_reg_polygon'
     523
     524    reg_polygon = np.zeros((N,2), dtype=np.float)
     525
     526    # Number of points per vertex
     527    Np = N/Nv
     528    # Angle incremental between vertices
     529    da = 2.*np.pi/Nv
     530    # Radii of the circle according to lf
     531    radii = lf*Nv/(2*np.pi)
     532
     533    iip = 0
     534    for iv in range(Nv-1):
     535        # Characteristics between vertices iv and iv+1
     536        av1 = da*iv
     537        v1 = [radii*np.sin(av1), radii*np.cos(av1)]
     538        av2 = da*(iv+1)
     539        v2 = [radii*np.sin(av2), radii*np.cos(av2)]
     540        dx = (v2[1]-v1[1])/Np
     541        dy = (v2[0]-v1[0])/Np
     542        for ip in range(Np):
     543            reg_polygon[ip+iv*Np,:] = [v1[0]+dy*ip,v1[1]+dx*ip]
     544
     545    # Characteristics between vertices Nv and 1
     546
     547    # Number of points per vertex
     548    Np2 = N - Np*(Nv-1)
     549
     550    av1 = da*Nv
     551    v1 = [radii*np.sin(av1), radii*np.cos(av1)]
     552    av2 = 0.
     553    v2 = [radii*np.sin(av2), radii*np.cos(av2)]
     554    dx = (v2[1]-v1[1])/Np2
     555    dy = (v2[0]-v1[0])/Np2
     556    for ip in range(Np2):
     557        reg_polygon[ip+(Nv-1)*Np,:] = [v1[0]+dy*ip,v1[1]+dx*ip]
     558
     559    return reg_polygon
     560
     561def p_reg_star(Nv, lf, freq, vs=0, N=50):
     562    """ Function to provide a regular star of Nv vertices
     563      Nv: number of vertices
     564      lf: length of the face of the regular polygon
     565      freq: frequency of union of vertices ('0', for just centered to zero arms)
     566      vs: vertex from which start (0 being first [0,lf])
     567      N: number of points
     568    """
     569    fname = 'p_reg_star'
     570
     571    reg_star = np.zeros((N,2), dtype=np.float)
     572
     573    # Number of arms of the star
     574    if freq != 0 and np.mod(Nv,freq) == 0:
     575        Na = Nv/freq + 1
     576    else:
     577        Na = Nv
     578
     579    # Number of points per arm
     580    Np = N/Na
     581    # Angle incremental between vertices
     582    da = 2.*np.pi/Nv
     583    # Radii of the circle according to lf
     584    radii = lf*Nv/(2*np.pi)
     585
     586    iip = 0
     587    av1 = vs*da
     588    for iv in range(Na-1):
     589        # Characteristics between vertices iv and iv+1
     590        v1 = [radii*np.sin(av1), radii*np.cos(av1)]
     591        if freq != 0:
     592            av2 = av1 + da*freq
     593            v2 = [radii*np.sin(av2), radii*np.cos(av2)]
     594        else:
     595            v2 = [0., 0.]
     596            av2 = av1 + da
     597        dx = (v2[1]-v1[1])/(Np-1)
     598        dy = (v2[0]-v1[0])/(Np-1)
     599        for ip in range(Np):
     600            reg_star[ip+iv*Np,:] = [v1[0]+dy*ip,v1[1]+dx*ip]
     601        if av2 > 2.*np.pi: av1 = av2 - 2.*np.pi
     602        else: av1 = av2 + 0.
     603
     604    iv = Na-1
     605    # Characteristics between vertices Na and 1
     606    Np2 = N-Np*iv
     607    v1 = [radii*np.sin(av1), radii*np.cos(av1)]
     608    if freq != 0:
     609        av2 = vs*da
     610        v2 = [radii*np.sin(av2), radii*np.cos(av2)]
     611    else:
     612        v2 = [0., 0.]
     613    dx = (v2[1]-v1[1])/(Np2-1)
     614    dy = (v2[0]-v1[0])/(Np2-1)
     615    for ip in range(Np2):
     616        reg_star[ip+iv*Np,:] = [v1[0]+dy*ip,v1[1]+dx*ip]
     617
     618    return reg_star
    513619
    514620# Combined objects
Note: See TracChangeset for help on using the changeset viewer.