三角関係

戻る


::::::::::::::
test1.sh
::::::::::::::
#!/usr/bin/perl
# $Id: TriangularRelationship.html,v 1.1 2009/06/22 16:12:02 kishi Exp kishi $

$rate = $ARGV[0];

# $x = 0.32;
# $y = 0.48;
# $z = 0.20;

$x = 0.7;
$y = 0.2;
$z = 0.1;


for ( $i = 0;$i < 800;$i++ ) {
    $sum = $x + $y + $z;

# 増分を求める

    $dx = $rate *$x * ( $y - $z ) / $sum;
    $dy = $rate *$y * ( $z - $x ) / $sum;
    $dz = $rate *$z * ( $x - $y ) / $sum;

    $x += $dx;
    $y += $dy;
    $z += $dz;

    printf( "%3d: ", $i );
    printf( "%12.5f", $x );
    printf( "%12.5f", $y );
    printf( "%12.5f", $z );
    printf( " (%12.5f)", $x + $y + $z );
    printf( "\n" );
}
::::::::::::::
CommonView.java
::::::::::::::
import java.awt.*;
import java.util.*;

/**
* $Id: TriangularRelationship.html,v 1.1 2009/06/22 16:12:02 kishi Exp kishi $
* @author KISHI Yasuhiro
* 2次元座標をリスト化したものを線分で繋いで描画する汎用クラス 
*/

public class CommonView implements Paintable {

    private java.util.List list;
    private Color color;

    public void setModel( java.util.List list, Color color ) {
        this.list = list;
        this.color = color;
    }

    public void paint( Graphics g ) {
        for ( int i = 1;i < list.size();i++ ) {

            g.setColor( color );
            double[] from = ( double[] ) list.get( i - 1 );
            double[] to = ( double[] ) list.get( i );
            g.drawLine( ( int ) from[ 0 ], ( int ) from[ 1 ], ( int ) to[ 0 ], ( int ) to[ 1 ] );

        }
    }
}
::::::::::::::
DrawingPanel.java
::::::::::::::
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

import java.io.*;

/**
$Id: TriangularRelationship.html,v 1.1 2009/06/22 16:12:02 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class DrawingPanel extends JPanel implements Runnable {

    private TriangularRelationshipModel model;
    private CommonView view1, view2, view3;

    public DrawingPanel() {

        model = new TriangularRelationshipModel( 160, 50, 10.0 );

        // VIEWとMODELの関連付け
        view1 = new CommonView();
        view1.setModel( model.getRedList(), Color.red );

        view2 = new CommonView();
        view2.setModel( model.getGreenList(), Color.green );

        view3 = new CommonView();
        view3.setModel( model.getBlueList(), Color.blue );

        // スレッド開始
        Thread thread = new Thread( this );
        thread.start();
    }

    public void run() {
        while ( true ) {

            SwingUtilities.invokeLater(
                new Runnable() {
                    public void run() {
                        repaint();
                    }
                }
            );

            try {
                Thread.sleep( 100 );
            } catch ( Exception e ) {
                e.printStackTrace();
            }
        }
    }

    public void paintComponent( Graphics g ) {

        int width = getWidth();
        int height = getHeight();

        // 画面クリア
        g.setColor( Color.black );
        // このコンポーネントのサイズを取得する
        g.fillRect( 0 , 0 , width , height );

        // 現在のパラメータを表示
        g.setColor( Color.yellow );
        StringBuilder message = new StringBuilder( "■三角関係モデル -- " );
        try {
            String currentRate = sprintf( "%8.2f", model.getRate() );
            message.append( currentRate );
        } catch ( Exception e ) {
            e.printStackTrace();
        }
        g.drawString( message.toString(), 10, 550 );

        // データを生成
        model.generate( 0.7, 0.2, 0.1 );

        // 描画
        view1.paint( g );
        view2.paint( g );
        view3.paint( g );

        // 成長パラメータを変化させる
        model.augmentRate( 0.01 );

    }

    static String sprintf( String format, Object obj ) throws Exception {

        String value = null;
        try {
            // C言語のsprinfもどき
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            PrintWriter pw = new PrintWriter( baos );
            pw.printf( format, obj );
            pw.flush();
            pw.close();

            value = baos.toString( "UTF8" );

            baos.close();

        } catch ( Exception e ) {
            throw e;
        } finally {
            return value;
        }

    }
}

::::::::::::::
Logarithm.java
::::::::::::::
/**
$Id: TriangularRelationship.html,v 1.1 2009/06/22 16:12:02 kishi Exp kishi $
*/

public class Logarithm {

    static double getValueByBaseOf( double number, double base ) {

        return Math.log( number ) / Math.log( base );
    }


    static public void main( String[] args ) {

        double a = 100.0;
        System.out.printf( "%10.3f %10.3f %10.3f\n", a, Math.log( a ), Logarithm.getValueByBaseOf( a, 100.0 ) );

        a = 200.0;
        System.out.printf( "%10.3f %10.3f %10.3f\n", a, Math.log( a ), Logarithm.getValueByBaseOf( a, 100.0 ) );

        a = 10000.0;
        System.out.printf( "%10.3f %10.3f %10.3f\n", a, Math.log( a ), Logarithm.getValueByBaseOf( a, 100.0 ) );

        a = 10.0;
        System.out.printf( "%10.3f %10.3f %10.3f\n", a, Math.log( a ), Logarithm.getValueByBaseOf( a, 100.0 ) );

        a = 1.0;
        System.out.printf( "%10.3f %10.3f %10.3f\n", a, Math.log( a ), Logarithm.getValueByBaseOf( a, 100.0 ) );

    }
}

::::::::::::::
Paintable.java
::::::::::::::
import java.awt.*;

/**
$Id: TriangularRelationship.html,v 1.1 2009/06/22 16:12:02 kishi Exp kishi $
@author KISHI Yasuhiro
*/
public interface Paintable {

    public void paint( Graphics g );
}
::::::::::::::
TriangularRelationship.java
::::::::::::::
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

/**
* $Id: TriangularRelationship.html,v 1.1 2009/06/22 16:12:02 kishi Exp kishi $
* @author KISHI Yasuhiro
*/

public class TriangularRelationship extends JApplet {

    public void init() {

        Container contentPane = getContentPane();
        contentPane.setLayout( new FlowLayout() );

        DrawingPanel dp = new DrawingPanel();
        dp.setPreferredSize( new Dimension( 800 , 600 ) );

        contentPane.add( dp );

    }

}

::::::::::::::
TriangularRelationshipModel.java
::::::::::::::
import java.util.*;

/**
* $Id: TriangularRelationship.html,v 1.1 2009/06/22 16:12:02 kishi Exp kishi $
* @author KISHI Yasuhiro
* 全体の総和は変化しないが、各々の増分が他の現在の状態に依存する系を表現したもの
*/

public class TriangularRelationshipModel {

    private final int SIZE = 3;

    private List[] theLists;
    /** プロットする点の数 */
    private int xSize;
    /** プロットする点のX軸の間隔 */
    private double xPitch;

    private double yMax;

    /** 成長パラメータ */
    private double rate = 0.01;
    private final double INITIAL_RATE = 0.01;

    public TriangularRelationshipModel( int xSize, double yMax, double xPitch ) {

        this.xSize = xSize;
        this.yMax = yMax;
        this.xPitch = xPitch;

        theLists = new List[ 3 ];
        for ( int i = 0;i < SIZE;i++ ) {
            theLists[ i ] = new ArrayList();
        }
    }

    public double getRate() {
        return rate;
    }

    public void augmentRate( double plusValue ) {

        rate += plusValue;
        if ( rate > 6.0 ) {
            rate = INITIAL_RATE;
        }
    }

    public void generate( double r, double g, double b ) {

        // リストをクリア
        for ( int i = 0;i < SIZE; i++ ) {
            theLists[ i ].clear();
        }

        for ( int i = 0;i < xSize; i++ ) {
            double sum = r + g + b;

            // 増分を求める
            double dr = rate * r * ( g - b ) / sum;
            double dg = rate * g * ( b - r ) / sum;
            double db = rate * b * ( r - g ) / sum;

            r += dr;
            g += dg;
            b += db;

            double[] rPoint = new double[ 2 ];
            rPoint[ 0 ] = i * xPitch;
            rPoint[ 1 ] = r * yMax;
            theLists[ 0 ].add( rPoint );

            double[] gPoint = new double[ 2 ];
            gPoint[ 0 ] = i * xPitch;
            gPoint[ 1 ] = g * yMax;
            theLists[ 1 ].add( gPoint );

            double[] bPoint = new double[ 2 ];
            bPoint[ 0 ] = i * xPitch;
            bPoint[ 1 ] = b * yMax;
            theLists[ 2 ].add( bPoint );
        }
    }

    public List getRedList() {
        return theLists[ 0 ];
    }
    public List getGreenList() {
        return theLists[ 1 ];
    }
    public List getBlueList() {
        return theLists[ 2 ];
    }


    static public void main( String[] args ) {
        int xSize = 800;
        double yMax = 600.0;
        TriangularRelationshipModel model = new TriangularRelationshipModel( xSize, yMax, 1.0 );

        model.generate( 0.7, 0.2, 0.1 );
        List redList = model.getRedList();
        List greenList = model.getGreenList();
        List blueList = model.getBlueList();

        for ( int i = 0;i < xSize;i++ ) {
            double[] redPoint = ( double[] ) redList.get( i );
            double[] greenPoint = ( double[] ) greenList.get( i );
            double[] bluePoint = ( double[] ) blueList.get( i );

            System.out.printf( "%3d:" , i );
            System.out.printf( "%10.3f", redPoint[ 1 ] );
            System.out.printf( "%10.3f", greenPoint[ 1 ] );
            System.out.printf( "%10.3f", bluePoint[ 1 ] );
            System.out.println();
        }

    }
}

戻る inserted by FC2 system