BohYoh.comトップページへ

基本情報技術者試験 平成14年度・春期・午後 問6 ソースプログラム

C言語講座へ  情報処理技術者試験対策講座へ  情報処理技術者試験対策講座(C言語)へ 
/*
    基本情報技術者試験          平成14年度・春期・午後 問6
                                BohYoh Shibata PREPARATION
*/

void  gSetPixel( int, int );
double  GetMax( double, double, double );
double  GetMin( double, double, double );

double  GetMax( double a, double b, double c )
{
    if ( ( a >= b ) && ( a >= c ) ) return a;
    if ( b >=c ) return b;
    return c;
}

double  GetMin( double a, double b, double c )
{
    if ( ( a <= b ) && ( a <= c ) ) return a;
    if ( b <= c ) return b;
    return c;
}

void  DrawTrg( double x1, double y1, double x2, double y2,
               double x3, double y3 )
{
    double Len1, Len2, Len3;
    double dx, dy;

    dx = GetMax( x1, x2, x3 ) - GetMin( x1, x2, x3 );
    dy = GetMax( y1, y2, y3 )  - GetMin( y1, y2, y3 );
    if ( ( dx <= 1.0 ) && ( dy <= 1.0 ) ) {
        gSetPixel( (int)x1, (int)y1 );
        gSetPixel( (int)x2, (int)y2 );
        gSetPixel( (int)x3, (int)y3 );
        return;
        }
    Len1 = ( x2-x1 )*( x2-x1 ) + ( y2-y1 )*( y2-y1 );
    Len2 = ( x3-x2 )*( x3-x2 ) + ( y3-y2 )*( y3-y2 );
    Len3 = ( x1-x3 )*( x1-x3 ) + ( y1-y3 )*( y1-y3 );

    if ( ( Len1 >= Len2 ) && ( Len1 >= Len3 ) ) {
        DrawTrg( x1, y1, (x1+x2)/2, (y1+y2)/2, x3, y3 );
        DrawTrg( x3, y3, (x1+x2)/2, (y1+y2)/2, x2, y2 );
        }
    else if ( Len2 >= Len3 ) {
        DrawTrg( x1, y1, (x2+x3)/2, (y2+y3)/2, x3, y3 );
        DrawTrg( x2, y2, (x2+x3)/2, (y2+y3)/2, x1, y1 );
        }
    else {
        DrawTrg( x1, y1, (x1+x3)/2, (y1+y3)/2, x2, y2 );
        DrawTrg( x2, y2, (x1+x3)/2, (y1+y3)/2, x3, y3 );
        }
}