#! /usr/bin/env python # def moment_normalized ( n, x, y, p, q ): #*****************************************************************************80 # ## MOMENT_NORMALIZED computes a normalized moment of a polygon. # # Discussion: # # Alpha(P,Q) = Integral ( x, y in polygon ) x^p y^q dx dy / Area ( polygon ) # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 23 June 2015 # # Author: # # John Burkardt # # Reference: # # Carsten Steger, # On the calculation of arbitrary moments of polygons, # Technical Report FGBV-96-05, # Forschungsgruppe Bildverstehen, Informatik IX, # Technische Universitaet Muenchen, October 1996. # # Parameters: # # Input, integer N, the number of vertices of the polygon. # # Input, real X(N), Y(N), the vertex coordinates. # # Input, integer P, Q, the indices of the moment. # # Output, real ALPHA_PQ, the normalized moment Alpha(P,Q). # from moment import moment nu_pq = moment ( n, x, y, p, q ) nu_00 = moment ( n, x, y, 0, 0 ) alpha_pq = nu_pq / nu_00 return alpha_pq def moment_normalized_test ( ): #*****************************************************************************80 # ## MOMENT_NORMALIZED_TEST tests MOMENT_NORMALIZED. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 23 June 2015 # # Author: # # John Burkardt # import numpy as np import platform n = 4 alpha_exact = np.array ( [ \ 1.0, 5.0, 4.0, 30.66666666666667, 22.0, 18.66666666666666 ] ) x = np.array ( [ 2.0, 10.0, 8.0, 0.0 ] ) y = np.array ( [ 0.0, 4.0, 8.0, 4.0 ] ) print ( '' ) print ( 'MOMENT_NORMALIZED_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' MOMENT_NORMALIZED computes normalized moments of a polygon.' ) print ( ' Here, we test the code using a rectange with known moments.' ) print ( '' ) print ( ' P Q Alpha(P,Q)' ) print ( ' Computed Exact' ) print ( '' ) k = 0 for s in range ( 0, 3 ): for p in range ( s, -1, -1 ): q = s - p alpha_pq = moment_normalized ( n, x, y, p, q ) print ( ' %2d %2d %14.6g %14.6g' % ( p, q, alpha_pq, alpha_exact[k] ) ) k = k + 1 # # Terminate. # print ( '' ) print ( 'MOMENT_NORMALIZED_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) moment_normalized_test ( ) timestamp ( )