Changeset 1482 in lmdz_wrf for trunk


Ignore:
Timestamp:
Apr 1, 2017, 5:50:24 PM (8 years ago)
Author:
lfita
Message:

Adding:

`crossingpoint_2Dlines': Function to determinant the crossing point between two lines in a plane
`points_2Dline': Function to define a line from a pair of points on a plane

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1473 r1482  
    6060# color_deg: Function to generate a degradation of colors in rgb base
    6161# contflow: Function to bring back the increment in j,i grid points according to a trip: (inflow directions)
     62# crossingpoint_2Dlines: Function to determinant the crossing point between two lines in a plane
    6263# curvelocalize_2D: Function to provide the localization a curve in a 2D field of positions via the equivalent
    6364#   i,j-quads within which the curve lays (-1, no value for the given quad [2x2 points around]) and provide
     
    103104# period_information_360d: Function to provide the information of a given period idate, edate (dates in
    104105#  [YYYY][MM][DD][HH][MI][SS] format) in a 360 years calendar
     106# points_2Dline: Function to define a line from a pair of points on a plane
     107# PolyArea: Function to compute the area of the polygon following 'Shoelace formula'
    105108# pretty_int: Function to plot nice intervals
    106109# prime_decomposition: Function to decompose a given number with its multiple prime numbers
    107110# prime_numbers: Function to find all the prime numbers up to a given value above 17
    108111# printing_dictionary: Function to print the content of a dictionary
    109 # PolyArea: Function to compute the area of the polygon following 'Shoelace formula'
    110112# radius_angle: Function to generate a matrix with the angle at a given point
    111113# radius_dist: Function to generate a matrix with the distance at a given point
     
    1145611458    return gribcode
    1145711459
     11460def crossingpoint_2Dlines(line1, line2):
     11461    """ Function to determinant the crossing point between two lines in a plane
     11462      line1= [a,b,c] terms of the polynomial representation of line1 ax+by+d
     11463      line2= [a,b,c] terms of the polynomial representation of line2 ax+by+d
     11464    >>> crossingpoint_2Dlines(np.array([1,-1,0]), np.array([1,1,-3]))
     11465    [[ 1.5  1.5]]
     11466    """
     11467    fname = 'crossingpoint_2lines'
     11468
     11469    if len(line1.shape) != len(line2.shape):
     11470        print errormsg
     11471        print '  ' + fname + ': rank difference between lines !!'
     11472        print '    shape line1:', line1.shape, 'line2:', line2.shape
     11473        quit(-1)
     11474
     11475    #determinant
     11476    matA = np.array([line1[0:2], line2[0:2]])
     11477    detA = np.linalg.det(matA)
     11478
     11479    # x-det
     11480    matX = np.array([[-line1[2],-line1[1]], [line2[2],line2[1]]])
     11481    detX = np.linalg.det(matX)
     11482
     11483    # y-det
     11484    matY = np.array([[line1[0],line1[2]], [-line2[0],-line2[2]]])
     11485    detY = np.linalg.det(matY)
     11486
     11487    #print 'mat A:'
     11488    #print matA
     11489    #print 'mat X:'
     11490    #print matX
     11491    #print 'mat Y:'
     11492    #print matY
     11493
     11494    #print 'det A:', detA, 'X:', detX, 'Y:', detY
     11495
     11496    x = detX / detA
     11497    y = detY / detA
     11498
     11499    return np.array([[x, y]])
     11500
     11501#print crossingpoint_2Dlines(np.array([1,-1,0]), np.array([1,1,-3]))
     11502
     11503def points_2Dline(pt1, pt2):
     11504    """ Function to define a line from a pair of points on a plane
     11505          it returns the terms of the polynomial description of the line ax+by+c=0 [a,b,c]
     11506      pt1= first point [x,y]
     11507      pt2= second point [x,y]
     11508    >>> points_2Dline(np.array([1,1]), np.array([2,2]))
     11509    [1 -1 0]
     11510    points_2Dline(np.array([1,2]), np.array([2,1]))
     11511    [1 1 -3]
     11512    """
     11513    fname = 'points_2Dline'
     11514
     11515    # minus
     11516    minuseq = pt1 - pt2
     11517    b = -minuseq[0]/minuseq[1]
     11518
     11519    # plus
     11520    pluseq = pt1 + pt2 
     11521    subsb = pluseq[1]*b
     11522
     11523    subsa = pluseq[0] + subsb
     11524
     11525    c = -subsa/2
     11526
     11527    return np.array([1, b, c])
     11528
     11529line1 = points_2Dline(np.array([1,1]), np.array([2,2]))
     11530line2 = points_2Dline(np.array([1,2]), np.array([2,1]))
     11531
     11532print crossingpoint_2Dlines(np.array([1,-1,0]), np.array([1,1,-3]))
     11533
    1145811534#quit()
Note: See TracChangeset for help on using the changeset viewer.