Linux下完成: 已知两三角形顶点坐标,判断其是否有公共边

来源:互联网 发布:java下载压缩文件 编辑:程序博客网 时间:2024/05/16 04:56

文档创建日期:2010-02-20[以下代码为本人原创,如需转载,请注明出处]


 

问题来源:http://topic.csdn.net/u/20100211/21/553ff3df-a342-455c-bbbb-d03c9a758ef8.html

 


   001    // 以下程序用于判断两个三角形是否有公共边
   002   
   003    // The beginning of eg07-05.c.
   004   
   005    // Header files included.
   006    #include <stdio.h>
   007    #include <math.h>
   008   
   009    // No.1: Subroutine: float rounding ( float f_num );
   010    float  rounding ( float f_num )
   011    {
   012        long int    temp=0;
   013        temp = (long int) (f_num * 100);
   014        temp = (long int) ((f_num*100 - temp)*2 + temp);
   015        f_num = temp / 100.0;
   016       
   017        return  (f_num);
   018    }
   019   
   020    // No.2: Subroutine:
   021    /* void vector_coordinates ( const float *x1, const float *y1,
   022     *                                       const float *x2, const float *y2,
   023     *                                       float *target_x, float *target_y );
   024    */
   025    void  vector_coordinates ( const float *x1, const float *y1,
   026                                           const float *x2, const float *y2,
   027                                           float *target_x, float *target_y )
   028    {
   029        *target_x = *x2 - *x1;
   030        *target_y = *y2 - *y1;
   031    }
   032   
   033    // No.3: Subroutine:
   034    /* int collinear_vector ( const float *v1_x, const float *v1_y,
   035     *                                const float *v2_x, const float *v2_y );
   036    */
   037    int  collinear_vector ( const float *v1_x, const float *v1_y,
   038                                    const float *v2_x, const float *v2_y )
   039    {
   040        // 返回0表示两向量不共线,返回1表示两向量共线
   041        if ( (*v1_x)*(*v2_y) - (*v1_y)*(*v2_x) == 0.0 )
   042            return  (1);
   043        else
   044            if ( (*v1_x)*(*v2_y) - (*v1_y)*(*v2_x) != 0.0 )
   045                return  (0);
   046    }
   047   
   048    // No.4: Subroutine: 判断点是否在线上
   049    /* int point_online ( const float *sour1_x, const float *sour1_y,
   050     *                           const float *sour2_x, const float *sour2_y,
   051     *                           const float *target_x, const float *target_y );
   052    */
   053    int  point_online ( const float *sour1_x, const float *sour1_y,
   054                               const float *sour2_x, const float *sour2_y,
   055                               const float *target_x, const float *target_y )
   056    {
   057        // 返回1表示点(target_x,target_y)在给定的直线上,返回0表示它不在给定直线上
   058        float    temp=0.0;
   059        temp = (*target_y - *sour1_y)*(*sour2_x - *sour1_x) -
   060                    (*sour2_y - *sour1_y)*(*target_x - *sour1_x);
   061        if ( temp == 0 )
   062            return  (1);
   063        return  (0);
   064    }
   065   
   066    // No.5: Subroutine:
   067    /* float distance ( const float *point1_x, const float *point1_y,
   068     *                       const float *point2_x, const float *point2_y );
   069    */
   070    float  distance ( const float *point1_x, const float *point1_y,
   071                           const float *point2_x, const float *point2_y )
   072    {
   073        float    dist=0.0;
   074        dist = sqrt ( pow((*point1_x - *point2_x), 2.0) +
   075                          pow((*point1_y - *point2_y), 2.0) );
   076        return  (dist);
   077    }
   078   
   079    // No.6: Subroutine:
   080    /* int judge_shape ( const float *p1_x, const float *p1_y,
   081     *                            const float *p2_x, const float *p2_y,
   082     *                            const float *p3_x, const float *p3_y );
   083    */
   084    int  judge_shape ( const float *p1_x, const float *p1_y,
   085                               const float *p2_x, const float *p2_y,
   086                               const float *p3_x, const float *p3_y )
   087    {
   088        // 返回1表示给定的3个点可以构成三角形,返回0表示不能构成三角形
   089        float    edge_1=0.0,  edge_2=0.0,  edge_3=0.0;
   090        edge_1 = distance ( p1_x, p1_y,  p2_x, p2_y );
   091        edge_2 = distance ( p1_x, p1_y,  p3_x, p3_y );
   092        edge_3 = distance ( p2_x, p2_y,  p3_x, p3_y );
   093        if ( (edge_1==0.0) || (edge_2==0.0) || (edge_3==0.0) )
   094        {
   095            printf ( "The three points can't form a triangle!/n" );
   096            return  (0);
   097        }
   098        if ( ((fabs(edge_2-edge_3) < edge_1) && (edge_1 < (edge_2+edge_3))) ||
   099             ((fabs(edge_1-edge_3) < edge_2) && (edge_2 < (edge_1+edge_3))) ||
   100             ((fabs(edge_1-edge_2) < edge_3) && (edge_3 < (edge_1+edge_2))) )
   101            return  (1);
   102        else
   103        {
   104            printf ( "The three points can't form a triangle!/n" );
   105            return  (0);
   106        }
   107    }
   108   
   109    // Main function's declaration.
   110    int  main ( void )
   111    {
   112        int    i=0,  j=0,  mark_1=0,  mark_2=0,  tag=0;
   113        char   ch[6] = {'A', 'B', 'C', 'D', 'E', 'F'};
   114        float  point[6][2],  vector[2][2]={{0.0, 0.0}, {0.0, 0.0}};
   115        printf ( "请依次输入三角形ABC的3个点的横纵坐标(每个点的横坐标在前,纵坐标在后):/n" );
   116        for ( i=0; i<3; i++ )
   117            for ( j=0; j<2; j++ )
   118                scanf  ( "%f", &point[i][j] );
   119        if ( (judge_shape( &point[0][0], &point[0][1],
   120                                  &point[1][0], &point[1][1],
   121                                  &point[2][0], &point[2][1] )) == 0 )
   122        {
   123            printf ( "请重新运行本程序./n" );
   124            return  (1);
   125        }
   126        printf ( "请依次输入三角形DEF的3个点的横纵坐标(每个点的横坐标在前,纵坐标在后):/n" );
   127        for ( i=3; i<6; i++ )
   128            for ( j=0; j<2; j++ )
   129                scanf  ( "%f", &point[i][j] );
   130        if ( (judge_shape( &point[3][0], &point[3][1],
   131                                  &point[4][0], &point[4][1],
   132                                  &point[5][0], &point[5][1] )) == 0 )
   133        {
   134            printf ( "请重新运行本程序./n" );
   135            return  (2);
   136        }
   137        for ( i=0; i<3; i++ )
   138        {
   139            vector_coordinates ( &point[i][0], &point[i][1],
   140                                          &point[(i+1)%3][0], &point[(i+1)%3][1],
   141                                          &vector[0][0], &vector[0][1] );
   142            for ( j=3; j<6; j++ )
   143            {
   144                vector_coordinates ( &point[j][0], &point[j][1],
   145                                               &point[(j+1)%3+3][0], &point[(j+1)%3+3][1],
   146                                               &vector[1][0], &vector[1][1] );
   147                mark_1 = collinear_vector ( &vector[0][0], &vector[0][1],
   148                                                          &vector[1][0], &vector[1][1] );
   149                mark_2 = point_online ( &point[i][0], &point[i][1],
   150                                                     &point[(i+1)%3][0], &point[(i+1)%3][1],
   151                                                     &point[j][0], &point[j][1] );
   152                tag = mark_1 && mark_2;
   153                if ( tag == 1 )
   154                {
   155                    printf ( "The two edges are on the same linear: " );
   156                    printf ( "<%c%c> and <%c%c>./n",
   157                               ch[i], ch[(i+1)%3], ch[j], ch[(j+1)%3+3] );
   158                }
   159            }
   160        }
   161        if ( tag == 0 )
   162            printf ( "The two triangles have no public linear./n" );
   163        return  (0);
   164    }
   165   
   166    // The end of C program: eg07-05.c.
   167


本程序可以随意输入需要的数据。