Changeset 2623 in lmdz_wrf for trunk/tools


Ignore:
Timestamp:
Jun 22, 2019, 9:03:58 PM (6 years ago)
Author:
lfita
Message:

Adding:

  • `angle_vectors2D': Angle between two vectors with sign
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/geometry_tools.py

    r2622 r2623  
    2424####### Contents:
    2525# add_secpolygon_list: Function to add a range of points of a polygon into a list
     26# angle_vectors2D: Angle between two vectors with sign
    2627# cut_between_[x/y]polygon: Function to cut a polygon between 2 given value of the [x/y]-axis
    2728# cut_[x/y]polygon: Function to cut a polygon from a given value of the [x/y]-axis
     
    277278    fname = 'mod_vec'
    278279
    279     mod = np.sqrt(vec[0]*vec[0] + vec[1]*vec[1])
     280    v = np.array(vec, dtype=np.float)
     281    mod = np.sqrt(v[0]*v[0] + v[1]*v[1])
    280282
    281283    return mod
    282284
    283 print mod_vec([1., 1.])
    284 quit()
    285 
    286285def angle_vectors2D(veca, vecb):
    287     """ Angle between two vectors
     286    """ Angle between two vectors with sign
     287        FROM: https://stackoverflow.com/questions/5188561/signed-angle-between-two-3d-vectors-with-same-origin-within-the-same-plane
    288288      veca: angle A [ya, xa]
    289289      vecb: angle B [yb, xb]
    290290      NOTE: angle from A to B
    291291    >>> angle_vectors2D([1.,0.], [0.,1.])
    292    
     292    1.57079632679
     293    >>> angle_vectors2D([0.,1.], [1.,0.])
     294    1.57079632679
    293295    """
    294296    fname = 'angle_vectors2D'
    295297
    296     moda = mod_vec(veca)
    297     modb = mod_vec(vecb)
    298 
    299     return alpha
     298    v1 = np.array(veca, dtype=np.float)
     299    v2 = np.array(vecb, dtype=np.float)
     300
     301    moda = mod_vec(v1)
     302    modb = mod_vec(v2)
     303    modab = mod_vec(v1*v2)
     304
     305    vc = np.cross(v1,v2)
     306    theta = np.arcsin(vc/(moda*modb))
     307
     308    # Without sign
     309    #alpha = np.arccos(modab/(moda*modb))
     310
     311    return theta
     312
     313print angle_vectors2D([1.,0.], [0.,1.])
     314print angle_vectors2D([0.,1.], [1.,0.])
     315
     316quit()
    300317
    301318def max_coords_poly(polygon):
Note: See TracChangeset for help on using the changeset viewer.