/* * ***************************************************************** * * * * * Copyright (c) Fast Corporation, 1997 * * * * * * All Rights Reserved. Unpublished rights reserved under * * * the copyright laws of the Japan. * * * * * * The software contained on this media is proprietary to * * * and embodies the confidential technology of Fast * * * Corporation. Possession, use, duplication or * * * dissemination of the software and media is authorized only * * * pursuant to a valid written license from Fast Corporation. * * * * * ***************************************************************** */ /* CSC90X 最小自乗法直線近似             CALCLINE.C */ /*[作成者]H.Yagi */ /* 目的: 関数: 履歴: Ver 1.0 97/08/14 Ver 2.0 99/03/24 CSC903確認 Ver 2.1 99/04/07 GUI変更 S.Masuda 注記:m_menu.hをインクルードして下さい。 m_menu.c及びm_note.cをリンクして下さい。 */ /* * Include compiler runtime library */ #include #include /* * Include CSC90X library */ #include "f_stdlib.h" #include "f_time.h" #include "f_gui.h" #include "f_stdio.h" #include "f_graph.h" #include "f_image.h" #include "f_pinf.h" #include "f_video.h" #include "f_system.h" #include "f_math.h" /* * Include CSC90X common local */ #include "m_menu.h" /* * プロトタイプ宣言 */ void main( void ); void main_menu_disp( void ); void disp_help( void ); void dot_set( void ); void exec( void ); void disp_abc_line( double, double, double ); void draw_calc_line( double, double ); int d4i5_d( double ); extern int message_note( void ); /* * メニュー項目 */ #define LANG_N 2 #define MAIN_MENU_N 4 static const char *str_main_menu[MAIN_MENU_N][LANG_N] = { { " SET DOTS ", "ドット設定" }, { " EXEC ", " 直線近似 " }, { " PROC3 ", " 処理3 " }, { " PROC4 ", " 処理4 " } }; #define INIT_CUR_POS_X 255 #define INIT_CUR_POS_Y 239 #define MIN_DOT_NO 2 #define MAX_DOT_NO 30 POINT point[30]; int cnt; /* * メイン */ void main( void ) { int sts; int xpos; int ypos; int s_xpos; int s_ypos; /* 初期メッセージ表示 */ if( NORMAL_RETURN != message_note() ) return; /* パラメタ初期化 */ xpos = INIT_CUR_POS_X; ypos = INIT_CUR_POS_Y; s_xpos = INIT_CUR_POS_X; s_ypos = INIT_CUR_POS_Y; /* 入力ビデオ制御 */ Lib_input_video_control( GRAY_PLANE ); /* ビデオ出力表示項目制御 */ Lib_display_control( GRAY_PLANE | LINE_PLANE | CHAR_PLANE ); /* ビデオ表示項目クリア */ Lib_memory_clear( LINE_PLANE | CHAR_PLANE ); /* カーソル初期化 */ Lib_init_cursor(); /* メインメニューの表示 */ main_menu_disp(); /* マウスカーソルの表示 */ Lib_draw_cursor( INIT_CUR_POS_X, INIT_CUR_POS_Y ); /* メニュー制御 */ for (;;) { sts = 0; /* マウス位置読みとり */ sts = Lib_see_current_position( &xpos, &ypos ); if ( s_xpos != xpos || s_ypos != ypos ) { /* マウス表示位置移動 */ Lib_move_cursor( xpos, ypos ); s_xpos = xpos; s_ypos = ypos; } /* 処理振り分け */ if ( CURSOR_EXECUTE == sts ) { if ( xpos > MENU_1_XS && xpos < MENU_1_XE && ypos > MENU_1_YS && ypos < MENU_1_YE ) { Lib_memory_clear( LINE_PLANE | CHAR_PLANE ); dot_set( ); /* ドット設定 */ Lib_move_cursor( xpos, ypos ); main_menu_disp(); } else if ( xpos > MENU_2_XS && xpos < MENU_2_XE && ypos > MENU_2_YS && ypos < MENU_2_YE ) { Lib_memory_clear( LINE_PLANE | CHAR_PLANE ); exec( ); /* 直線近似試行 */ Lib_move_cursor( xpos, ypos ); main_menu_disp(); } else if ( xpos > MENU_H_XS && xpos < MENU_H_XE && ypos > MENU_H_YS && ypos < MENU_H_YE ) { Lib_memory_clear( LINE_PLANE | CHAR_PLANE ); disp_help(); /* ヘルプ */ Lib_move_cursor( xpos, ypos ); main_menu_disp(); } else if ( xpos > MENU_E_XS && xpos < MENU_E_XE && ypos > MENU_E_YS && ypos < MENU_E_YE ) { break; /* 終了 */ } } } } /* * メインメニュー表示 */ static void main_menu_disp( void ) { int iLanguage; /* 日本語/英語表示文字列切替情報取得 */ iLanguage = Lib_get_disp_language(); /* 整列キー表示 -> ( m_menu.c ) */ SUB_menu_disp4he( (char *)str_main_menu[0][iLanguage], (char *)str_main_menu[1][iLanguage], (char *)NULL, (char *)NULL ); } /* * ヘルプ表示 */ static void disp_help( void ) { Lib_chrdisp( 10, 8, "【機能\概要】       CALCLINE " ); Lib_chrdisp( 11, 10, "<直線近似 サンプル>" ); Lib_chrdisp( 11, 12, "2〜30個のドット設定の後、最小自乗法直線近似" ); Lib_chrdisp( 11, 14, "を行い求めた直線を表\示します。" ); } /************************************************************************/ /* ドット設定 */ /************************************************************************/ void dot_set( void ) { int sts; int xpos; int ypos; int s_xpos; int s_ypos; xpos = INIT_CUR_POS_X; ypos = INIT_CUR_POS_Y; s_xpos = INIT_CUR_POS_X; s_ypos = INIT_CUR_POS_Y; Lib_chrdisp(5,29,"実行キー :設定"); Lib_chrdisp(5,30,"キャンセルキー:終了"); cnt = 0; for (;;) { sts = 0; sts = Lib_see_current_position( &xpos, &ypos ); /* マウス位置読みとり */ if ( s_xpos != xpos || s_ypos != ypos ) { Lib_move_cursor( xpos, ypos ); s_xpos = xpos; s_ypos = ypos; } if ( CURSOR_EXECUTE == sts ) { point[cnt].x = xpos; point[cnt].y = ypos; Lib_pset( xpos, ypos, GRAPH_DRAW ); cnt++; if ( MAX_DOT_NO <= cnt ) break; } if ( CURSOR_CANCEL == sts ) { if ( cnt < MIN_DOT_NO ) Lib_display_message( 70, 200, "CANCEL err!", "2点以上のドット設定が必要です。" ); else break; } } Lib_cls(CHAR_PLANE, BLACK_COLOR); } /************************************************************************/ /* 直線近似試行 */ /************************************************************************/ void exec( ) { char ss[50]; int status; int i; double da, db, dc; LINE coeff; for ( i = 0; i < cnt; i++ ) Lib_pset( point[i].x, point[i].y, 1 ); if ( NORMAL_RETURN == ( status = Lib_calcline( cnt, point, &coeff ) ) ) { da = (double)coeff.a; db = (double)coeff.b; dc = (double)coeff.c; /* ax + by + c = 0 ・・・・・(1) */ disp_abc_line( da, db, dc ); da = (double)coeff.a / 65536.0; db = (double)coeff.b / 65536.0; dc = (double)coeff.c / 16.0; Lib_sprintf( ss, "係数 a = %lf", da ); Lib_chrdisp( 1, 28, ss ); Lib_sprintf( ss, "係数 b = %lf", db ); Lib_chrdisp( 1, 29, ss ); Lib_sprintf( ss, "係数 c = %lf", dc ); Lib_chrdisp( 1, 30, ss ); } else Lib_chrdisp( 1, 30, "Lib_calcline ERR! " ); Lib_display_keyinput( 430, 0, " 確認 " ); Lib_cls( ( LINE_PLANE | CHAR_PLANE ), BLACK_COLOR ); } /***********************************************************************/ /* 直線係数から直線表示              */ /***********************************************************************/ void disp_abc_line( a, b, c ) double a, b, c; /* 入力:直線係数a,b,c */ /* aは65536倍のままであること*/ /* bは65536倍のままであること*/ /* cは   16倍のままであること*/ { double dw; int iw; double m, n; if ( 0 == b ) { dw = -c * 4096.0 / a; iw = (int)dw; Lib_drawline( iw, 0, iw, Lib_get_dy_size() - 1 ); } else { m = -a / b; n = -c * 4096.0 / b; draw_calc_line( m, n ); } } /***********************************************************************/ /* 直線描画 */ /***********************************************************************/ void draw_calc_line( a, b ) double a; /* 入力:傾き */ double b; /* 入力:y切片 */ /*[関数値] なし */ /*[処理概要] y = a * x + bから直線を描画する。 */ /*[注記] なし */ { double d_x, d_y; int x, y; int fx, fy; /* ビデオ座標系の諸パラメータの取得 */ fx = Lib_get_fx_size(); fy = Lib_get_fy_size(); if( - 1.0 <= a && a <= 1.0 ) { for( x = 0; x <= fx; x++ ) { d_x = ( double )x; d_y = a * d_x + b; y = ( int )d_y; Lib_pset( x, y, GRAPH_DRAW ); } } else { for( y = 0; y <= fy; y++ ) { d_y = ( double )y; d_x = d_y / a - b / a; x = ( int )d_x; Lib_pset( x, y, GRAPH_DRAW ); } } } /***********************************************************************/ /* 入力数値(double)の4捨5入した数値(int)を返す */ /***********************************************************************/ int d4i5_d( double in ) { int out; if ( in >= 0.0 ) out = (int)( in + 0.5 ); else out = (int)( in - 0.5 ); return( out ); }