Python(パイソン) ,ソースプログラムリスト あり ,プログラム作ってみた ,[指定された曲線を、指定された値で、近似的に等分割するような、点の位置を求める] を 行う

Python(パイソン) プログラム作ってみた インデックス へ
-----

2024.8.16 presented in [note] ( //note.com/runningWater/ )
2024.8.24 rewritten

----------
これ以降に記述されている内容は、このようなコンピューター・プログラムを制作した、というような事を、ただ、述べているに過ぎない。

以下の記述を読んだ人が、それを単に参考にする、というのであれば、問題は無いと、思われる。

しかし、記述されている内容に沿って、その人が、そこに記されているのと同様の制作や作業を行った際に、その制作、作業、コンピューターの作動の結果、使用されたコンピューター等、様々な方面において、何らかの問題が発生しない、という保証は、全くない。

その制作、作業、コンピューターの作動の結果、その人や、その人が所属している組織、その人が使用した様々な機器、インフラストラクチャー等の、身の上にどのような事が起ころうとも、私は一切、責任を負わない。

このプログラムは、Python(パイソン) 言語を使って、記述されている。
----------
 何らかの曲線(数式で表現されている)を、
 指定された値で、等分割して、
 その分割点の位置(XY座標)を、求めたい
 
というような時に、呼び出して使えるようなものを、制作した。

まず、その使用例を述べる。

下図は、
 ここに記述されているモジュール(以降、[モジュール・A] とする)とは異なる、別のモジュール群(以降、[モジュール群・B] とする)が、表示した画面の一部を、示したものである。

Fig 1

[モジュール群・B] は、
 [モジュール・A]を使用した([モジュール・A]の関数を、呼び出して)のだが、
 その際に、
  曲線を表現するモジュール(以降、[モジュール・C] とする)
  と
  分割個数 ( = 11 )
  を、指定した。

図中に、青色の四角形で表示されている位置が、その曲線を、11等分する点の位置を、示している。

ただし、この位置の算出は、近似計算的なやり方を用いて、行っている。曲線を表す数式から、方程式を作り、さらに、そこから・・・、と、いうような、数学的手法を、用いてはいない。

この処理で対象としてよい曲線は、以下のような条件の全てを、満たすものでなくては、ならない。

 条件(1) 1個の[パラメーター・s] を用いて、パラメトリック表現できるようなものであること
  (例えば、楕円のように、 x = A × cos ( 2π × s ) , y = B × sin ( 2π × s ) )
 条件(2) [0 から 1] の範囲の、[パラメーター・s]値を用いて、表現できるようなものであること
   (上記の楕円を表す数式においては、s が、0 から 1 へ変わるにつれて、楕円上を一周する、というような形となる)

この例においては、 
 [モジュール群・B] は、
 下記のように記述されている[モジュール・C] を用いて、
 [モジュール・A]を使用したのであった。
 
[モジュール・C]の記述内容を変えて、曲線の形を変えた後に、[モジュール・A]が使用されるならば、変更後の曲線の形に沿って、等分割点の位置が、算出されるであろう。

ファイル名 [Curve.py]

import math

#===========================================
class Curve :

    CV_CLASS_NAME = "Curve"

    #------------------------------------------------------------
        # definition of constructer
    def __init__( self
                     , arg_Curve_ID
                ):

        methode_name = "constructer"

        self.iv_Curve_ID = arg_Curve_ID

    #------------------------------------------------------------
        # definition of figure of this Curve
        #    x and y is decided by parameter s_value
        #      s_value must be in range , from 0.0  to  1.0

    def decide_x_and_y_by_s_value ( self
                                  , arg_s_value
                                  ):

        methode_name = "decide_x_and_y_by_s_value"

                   # ellipse figure
        horizontal_direction_radius = 80
        vertical_direction_radius = 50
                #-------------------------------
        if ( arg_s_value < 0.5 ) :
                #-------------- vertical line ------
            x_value = horizontal_direction_radius  \
                         * ( arg_s_value * 2 )
            y_value = 0
        else :
                #------------- ellipse figure curve ------
            t_value = ( arg_s_value - 0.5 ) * ( 2 * math.pi )

            x_value = horizontal_direction_radius * math.cos ( t_value )
            y_value = vertical_direction_radius * math.sin ( t_value )

            #--------- return decided value
        return_value = [ x_value , y_value ]
        return return_value

----------

[モジュール・A] の記述内容は、下記のとおりである。

[arg_ins_Curve] に、曲線を定義するモジュールを設定し、
[arg_number_of_section] に、分割数を設定した後に、
[モジュール群・B] から、
[decide_values_of_EquallySpacedPointsOnCurve]
への、呼び出しが、かけられる、というような形にした。

ファイル名 [DecideAproximateEquallySpacedPointsOnCurve.py]

----------

# ===============================================
#  DecideAproximateEquallySpacedPointsOnCurve
# ===============================================

import Curve
import math

MODULE_NAME = "DecideAproximateEquallySpacedPointsOnCurve"

function_name = "func_start_process"

global_number_of_section = 0

global_list_s_value_at_section_end = " "
global_list_x_value_at_section_end = " "
global_list_y_value_at_section_end = " "
global_list_length_from_begin_at_section_end = " "

     #----------------------------------
def  get_length_from_begin_at_section_end ( arg_i ) :

        return global_list_length_from_begin_at_section_end [ arg_i - 1 ]

     #----------------------------------
def  get_s_value_at_section_end ( arg_i ) :

        return global_list_s_value_at_section_end [ arg_i - 1 ]

     #----------------------------------
def   get_x_and_y_value_at_section_end ( arg_i ) :

        return_value = [ global_list_x_value_at_section_end [ arg_i - 1 ]
                       , global_list_y_value_at_section_end [ arg_i - 1 ]
                       ]

        return return_value

      #----------------------------------
def  get_number_of_section ( ) :

        return global_number_of_section


    #--------------------------------------------------------------
def decide_values_of_EquallySpacedPointsOnCurve (
                       arg_ins_Curve
                     , arg_number_of_section
                     , arg_incliment_value_of_s
                                                    ) :

    function_name = "decide_values_of_EquallySpacedPointsOnCurve"

    global global_number_of_section
    global_number_of_section = arg_number_of_section

    s_value_of_BeginPoint = 0.0
    s_value_of_EndPoint = 1.0

        #culculate length of Curve
    length_along_Curve_between_0_and_1   \
         =  calculate_length_along_Curve_between_two_points_on_Curve (
                       arg_ins_Curve
                     , s_value_of_BeginPoint
                     , s_value_of_EndPoint
                     , arg_incliment_value_of_s
                                                                  )
    length_of_oneSection_in_Curve   \
             = length_along_Curve_between_0_and_1      \
                       / global_number_of_section

            #initialize list
    global global_list_length_from_begin_at_section_end
    global global_list_s_value_at_section_end
    global global_list_x_value_at_section_end
    global global_list_y_value_at_section_end

    global_list_length_from_begin_at_section_end  \
         = [0] * global_number_of_section
    global_list_s_value_at_section_end  \
         = [0] * global_number_of_section
    global_list_x_value_at_section_end  \
         = [0] * global_number_of_section
    global_list_y_value_at_section_end  \
         = [0] * global_number_of_section

    for i in range ( 1 , ( global_number_of_section + 1 ) , 1 ) :

        global_list_length_from_begin_at_section_end [ i - 1 ]  \
               = length_of_oneSection_in_Curve * i
        global_list_s_value_at_section_end [ i - 1 ]   \
               = calculate_s_value_by_FromPoint_and_length_along_Curve (
                       arg_ins_Curve
                    ,  s_value_of_BeginPoint
                    ,  global_list_length_from_begin_at_section_end [ i - 1 ]
                    ,  arg_incliment_value_of_s
                                                                    )
             #-------------------------------
        s_value_1 = global_list_s_value_at_section_end [ i - 1 ]
        xy_of_Point             \
              = arg_ins_Curve   \
                    .decide_x_and_y_by_s_value (
                        s_value_1
                                              )

        global_list_x_value_at_section_end [ i - 1 ] \
                    = xy_of_Point [ 0 ]
        global_list_y_value_at_section_end [ i - 1 ] \
                    = xy_of_Point [ 1 ]

    #-------------------------------------------------
def calculate_length_along_Curve_between_two_points_on_Curve (
                        arg_ins_Curve
                     ,  arg_s_value_of_Point_1
                     ,  arg_s_value_of_Point_2
                     ,  arg_incliment_value_of_s
                                             ) :

        function_name = "calculate_length_along_Curve_between_two_points_on_Curve"

        current_length_along_Curve = 0.0 ;
        current_s_value = arg_s_value_of_Point_1
        next_s_value = 0.0
        distance_between_Current_Point_and_Next_Point = 0.0

        process_continue = "Y"
        while ( process_continue == "Y" ) :

            next_s_value   \
                   = current_s_value  \
                       + arg_incliment_value_of_s
            if ( next_s_value > arg_s_value_of_Point_2 ) :
                process_continue = "N"
                break

            distance_between_Current_Point_and_Next_Point  \
                  = calculate_distance_between_two_points_on_Curve (
                           arg_ins_Curve
                         , current_s_value
                         ,  next_s_value
                                                                   )
            current_length_along_Curve   \
                      += distance_between_Current_Point_and_Next_Point
            current_s_value = next_s_value


        return current_length_along_Curve ;

     #-------------------------------------------------
def calculate_s_value_by_FromPoint_and_length_along_Curve (
                        arg_ins_Curve
                     ,  arg_s_value_of_FromPoint
                     ,  arg_length_along_Curve_FromPoint
                     ,  arg_incliment_value_of_s
                                             ) :

        function_name = "calculate_s_value_by_FromPoint_and_length_along_Curve"

        current_length_along_Curve = 0.0
        current_s_value =  arg_s_value_of_FromPoint
        next_parameter_value = 0.0
        distance_between_Current_Point_and_Next_Point = 0.0

        w1 = 0

        process_continue = "Y"
        while ( process_continue == "Y" ) :

            next_s_value    \
                = current_s_value  \
                     + arg_incliment_value_of_s
            distance_between_Current_Point_and_Next_Point   \
                = calculate_distance_between_two_points_on_Curve (
                              arg_ins_Curve
                           ,  current_s_value
                           ,  next_s_value
                                                                 )
            w1 = current_length_along_Curve    \
                     + distance_between_Current_Point_and_Next_Point

            if ( w1 > arg_length_along_Curve_FromPoint ) :
                process_continue = "N"
                break
                #-------------------------------
            current_length_along_Curve    \
                    +=  distance_between_Current_Point_and_Next_Point
            current_s_value = next_s_value

        return current_s_value

        #------------------------------------------------------------
def calculate_distance_between_two_points_on_Curve (
                            arg_ins_Curve
                          , arg_s_value_of_Point_1
                          , arg_s_value_of_Point_2
                                                   ) :

        function_name = "calculate_distance_between_two_points_on_Curve"

        x_y_of_Point_1    \
                = arg_ins_Curve  \
                    .decide_x_and_y_by_s_value ( arg_s_value_of_Point_1 )
        x_y_of_Point_2    \
                = arg_ins_Curve  \
                    .decide_x_and_y_by_s_value ( arg_s_value_of_Point_2 )

        return_value    \
              = calculate_distance_between_two_points (
                         x_y_of_Point_1
                      ,  x_y_of_Point_2
                                                      )

        return return_value ;

        #------------------------------------------------------------
def calculate_distance_between_two_points (
                       arg_list_x_y_of_Point_1
                     , arg_list_x_y_of_Point_2
                                          ) :

        methode_name = "calculate_distance_between_two_points"

        difference_x \
            = arg_list_x_y_of_Point_2 [ 0 ] \
                - arg_list_x_y_of_Point_1 [ 0 ]

        difference_y \
            = arg_list_x_y_of_Point_2 [ 1 ] \
                - arg_list_x_y_of_Point_1 [ 1 ]

        difference_x_2th_power = difference_x * difference_x
        difference_y_2th_power = difference_y * difference_y

        return_value   \
               = math.sqrt ( difference_x_2th_power + difference_y_2th_power )

        return return_value ;

     #----------------------------------
def display (  ) :

        function_name = "display"

        print ( "==================================" )
        print ( "Enter into , Module = " + MODULE_NAME
                      + " , function = " + function_name )
        print ( "==================================" )

        print ( "=========================" )
        print ( "global_number_of_section = " \
            +  str ( global_number_of_section ) )
        print ( "=========================" )

        for i in range ( 1 , ( global_number_of_section + 1 ) , 1 ) :

            print ( "=========================" )
            print ( "order number of Point = " + str ( i ) )

            print ( "  length_from_begin_at_section_end = "  \
                       + str (                               \
                   global_list_length_from_begin_at_section_end [ i - 1 ] \
                             )                               \
                  )
            print ( "  s_value_at_section_end = "  \
                       + str (                               \
                   global_list_s_value_at_section_end [ i - 1 ] \
                             )                               \
                  )
            print ( "  x_value_at_section_end = "  \
                       + str (                               \
                   global_list_x_value_at_section_end [ i - 1 ] \
                             )                               \
                  )
            print ( "  y_value_at_section_end = "  \
                       + str (                               \
                   global_list_y_value_at_section_end [ i - 1 ] \
                             )                               \
                  )
            print ( "***********************************" ) ;

        print ( "==================================" )
        print ( "Exit from , Module = " + MODULE_NAME
                      + " , function = " + function_name )
        print ( "==================================" )

----------

Python(パイソン) プログラム作ってみた インデックス へ

この記事が気に入ったらサポートをしてみませんか?