一直線上にない3点 A,B,C が作る角度の大きさ
(鋭角、直角、鈍角)


目次
0、はじめに
1、C言語(C99)での実装
2、テスト結果
3、ライセンス及び免責事項
4、終わりに


0、はじめに

データ処理をしていると、内接円や外接円(外接円を求める方法)を求めたくなる事が多々ある。
超越関数(三角関数)を使用しないで、簡単な四則演算で算出する方法を示してあるが、同時に
各頂点か成す角度(鋭角、直角、鈍角)を知りたいことも同様にあるので追加処理を用意した。
ベクトルの内積が0の時、2つのベクトルは直交している事はよく知られている。内積が正の値を
取る時、2つのベクトルのなす角は鋭角であり、負の値を取る時は鈍角となるので簡単に判断が
出来る。まず3点から知りたい角度の座標点と、他の2点に対する2つのベクトルを生成して、
その内積 (A.x-B.x)*(C.x-B.x) + (A.y-B.y)*(C.y-B.y) の値を確認するだけの簡単な処理。


1、C言語(C99)での実装

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

typedef struct POINT { float x, y; } POINT;

const char *GetTheStringOfIndicatesTheAngleAttribute( POINT A, POINT B, POINT C ){
    static const char *S[] = {"an acute angle","a right angle","an obtuse angle"};
    int    attribute = (A.x-B.x)*(C.x-B.x) + (A.y-B.y)*(C.y-B.y);
    return 0<attribute ? S[0] : ( attribute<0 ? S[2] : S[1] );
}

double GetTheAngleAsDegree( POINT A, POINT B, POINT C ){
    double d = (A.x-B.x)*(C.x-B.x) + (A.y-B.y)*(C.y-B.y);
    double m = sqrt( (A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y) )
             * sqrt( (C.x-B.x)*(C.x-B.x) + (C.y-B.y)*(C.y-B.y) );
    return (180./3.14159265358979323846)*acos(d/m); //cos: transcendental functions
}


int main(){ srand((unsigned int)time(NULL));
    for(int e=0; e<64; e++ ) { // 0 <= x < 1000, 0 <= y < 1000
        POINT A = (POINT){ rand() % 1000, rand() % 1000 };
        POINT B = (POINT){ rand() % 1000, rand() % 1000 };
        POINT C = (POINT){ rand() % 1000, rand() % 1000 };
        if( (A.x-C.x)*(B.y-C.y) == (B.x-C.x)*(A.y-C.y) ) continue;

        printf( "A:(% 5.0f,% 5.0f), ", A.x, A.y );
        printf( "B:(% 5.0f,% 5.0f), ", B.x, B.y );
        printf( "C:(% 5.0f,% 5.0f)\n", C.x, C.y );
        double AngleABC = GetTheAngleAsDegree( A, B, C );
        double AngleBCA = GetTheAngleAsDegree( B, C, A );
        double AngleCAB = GetTheAngleAsDegree( C, A, B );
        const char *abc = GetTheStringOfIndicatesTheAngleAttribute( A, B, C );
        const char *bca = GetTheStringOfIndicatesTheAngleAttribute( B, C, A );
        const char *cab = GetTheStringOfIndicatesTheAngleAttribute( C, A, B );

        printf("\t Angle ABC, %6.2f deg., is %s.\n", AngleABC, abc );
        printf("\t Angle BCA, %6.2f deg., is %s.\n", AngleBCA, bca );
        printf("\t Angle CAB, %6.2f deg., is %s.\n", AngleCAB, cab );
        printf( "\n");
    }
    return 0;
}


・インライン展開でも支障ない程の、簡単な四則演算のみの短くシンプルな実装
rand() 関数を使って、点 A,B,C の座標を動的に作成した検証用データ
・3点が一直線上にあるかの判定処理(一直線上にある時、角度は常に 0°になってしまう)
      if( (A.x-C.x)*(B.y-C.y)==(B.x-C.x)*(A.y-C.y) )continue;

2、テスト結果

実行結果 『 ∠ABC、∠BCA 、∠CAB を調べた結果を表示 』

A:(   27,  565), B:(    7,  807), C:(   75,  349)
         Angle ABC,   3.72 deg., is an acute angle.
         Angle BCA,   4.08 deg., is an acute angle.
         Angle CAB, 172.20 deg., is an obtuse angle.

A:(  889,   21), B:(  709,  122), C:(  795,  624)
         Angle ABC, 109.58 deg., is an obtuse angle.
         Angle BCA,  18.58 deg., is an acute angle.
         Angle CAB,  51.84 deg., is an acute angle.

A:(   66,  317), B:(  902,  839), C:(  299,   82)
         Angle ABC,  19.48 deg., is an acute angle.
         Angle BCA,  83.29 deg., is an acute angle.
         Angle CAB,  77.23 deg., is an acute angle.

A:(  948,   48), B:(  846,   21), C:(  960,  252)
         Angle ABC,  48.91 deg., is an acute angle.
         Angle BCA,  22.90 deg., is an acute angle.
         Angle CAB, 108.19 deg., is an obtuse angle.

A:(  938,  783), B:(  216,   40), C:(  742,   25)
         Angle ABC,  47.45 deg., is an acute angle.
         Angle BCA, 102.86 deg., is an obtuse angle.
         Angle CAB,  29.68 deg., is an acute angle.

A:(  194,  934), B:(  703,   16), C:(  413,  163)
         Angle ABC,  34.11 deg., is an acute angle.
         Angle BCA, 132.74 deg., is an obtuse angle.
         Angle CAB,  13.15 deg., is an acute angle.

A:(  817,  626), B:(  705,   50), C:(  753,  728)
         Angle ABC,   6.95 deg., is an acute angle.
         Angle BCA,  36.16 deg., is an acute angle.
         Angle CAB, 136.89 deg., is an obtuse angle.

A:(  658,    1), B:(  184,  924), C:(  905,  411)
         Angle ABC,  27.39 deg., is an acute angle.
         Angle BCA,  94.37 deg., is an obtuse angle.
         Angle CAB,  58.25 deg., is an acute angle.

A:(  338,  836), B:(   35,  118), C:(  559,  368)
         Angle ABC,  41.61 deg., is an acute angle.
         Angle BCA,  90.23 deg., is an obtuse angle.
         Angle CAB,  48.16 deg., is an acute angle.

A:(  166,  861), B:(  682,  593), C:(  642,  896)
         Angle ABC,  55.03 deg., is an acute angle.
         Angle BCA,  93.31 deg., is an obtuse angle.
         Angle CAB,  31.65 deg., is an acute angle.

A:(  617,  482), B:(   52,   18), C:(  876,  660)
         Angle ABC,   1.47 deg., is an acute angle.
         Angle BCA,   3.42 deg., is an acute angle.
         Angle CAB, 175.10 deg., is an obtuse angle.

A:(  129,  228), B:(   49,  372), C:(  624,  785)
         Angle ABC,  96.63 deg., is an obtuse angle.
         Angle BCA,  12.68 deg., is an acute angle.
         Angle CAB,  70.68 deg., is an acute angle.

A:(  646,  591), B:(  104,  242), C:(  721,  410)
         Angle ABC,  17.55 deg., is an acute angle.
         Angle BCA,  82.72 deg., is an acute angle.
         Angle CAB,  79.73 deg., is an acute angle.

A:(  861,  722), B:(   53,  803), C:(  781,  180)
         Angle ABC,  34.83 deg., is an acute angle.
         Angle BCA,  57.84 deg., is an acute angle.
         Angle CAB,  87.33 deg., is an acute angle.

A:(  926,  628), B:(  696,  148), C:(  679,  385)
         Angle ABC,  29.70 deg., is an acute angle.
         Angle BCA, 130.43 deg., is an obtuse angle.
         Angle CAB,  19.87 deg., is an acute angle.

A:(  191,  627), B:(  658,  903), C:(  556,   95)
         Angle ABC,  52.22 deg., is an acute angle.
         Angle BCA,  41.65 deg., is an acute angle.
         Angle CAB,  86.13 deg., is an acute angle.

A:(  376,  424), B:(   27,  970), C:(  129,    4)
         Angle ABC,  26.56 deg., is an acute angle.
         Angle BCA,  36.49 deg., is an acute angle.
         Angle CAB, 116.95 deg., is an obtuse angle.

A:(  127,  514), B:(  326,  938), C:(  119,  425)
         Angle ABC,   3.17 deg., is an acute angle.
         Angle BCA,  16.84 deg., is an acute angle.
         Angle CAB, 159.99 deg., is an obtuse angle.

A:(  933,  431), B:(  154,  118), C:(  372,  766)
         Angle ABC,  49.52 deg., is an acute angle.
         Angle BCA,  77.75 deg., is an acute angle.
         Angle CAB,  52.73 deg., is an acute angle.

A:(  866,  620), B:(  522,  962), C:(  493,  705)
         Angle ABC,  51.61 deg., is an acute angle.
         Angle BCA,  96.40 deg., is an obtuse angle.
         Angle CAB,  32.00 deg., is an acute angle.

A:(  507,  856), B:(  737,  809), C:(  258,  376)
         Angle ABC,  53.66 deg., is an acute angle.
         Angle BCA,  20.47 deg., is an acute angle.
         Angle CAB, 105.87 deg., is an obtuse angle.

A:(  169,  268), B:(  692,  644), C:(  363,  422)
         Angle ABC,   1.70 deg., is an acute angle.
         Angle BCA, 175.57 deg., is an obtuse angle.
         Angle CAB,   2.73 deg., is an acute angle.

A:(  386,  962), B:(  389,  205), C:(  543,  935)
         Angle ABC,  12.14 deg., is an acute angle.
         Angle BCA,  87.85 deg., is an acute angle.
         Angle CAB,  80.01 deg., is an acute angle.

A:(  695,  928), B:(  579,  741), C:(  561,  158)
         Angle ABC, 149.96 deg., is an obtuse angle.
         Angle BCA,   8.10 deg., is an acute angle.
         Angle CAB,  21.94 deg., is an acute angle.

A:(  271,  614), B:(   48,  117), C:(  426,  785)
         Angle ABC,   5.34 deg., is an acute angle.
         Angle BCA,  12.69 deg., is an acute angle.
         Angle CAB, 161.98 deg., is an obtuse angle.

A:(  430,  644), B:(   92,  109), C:(  696,  387)
         Angle ABC,  33.00 deg., is an acute angle.
         Angle BCA,  68.73 deg., is an acute angle.
         Angle CAB,  78.27 deg., is an acute angle.

A:(    9,  708), B:(  748,  321), C:(  128,  762)
         Angle ABC,   7.78 deg., is an acute angle.
         Angle BCA, 120.17 deg., is an obtuse angle.
         Angle CAB,  52.05 deg., is an acute angle.

A:(  402,  446), B:(  420,  173), C:(  978,   42)
         Angle ABC, 106.98 deg., is an obtuse angle.
         Angle BCA,  21.83 deg., is an acute angle.
         Angle CAB,  51.18 deg., is an acute angle.

A:(  170,  623), B:(  902,  494), C:(  620,   41)
         Angle ABC,  68.09 deg., is an acute angle.
         Angle BCA,  69.61 deg., is an acute angle.
         Angle CAB,  42.29 deg., is an acute angle.

A:(  322,  850), B:(  918,  875), C:(   97,  486)
         Angle ABC,  22.95 deg., is an acute angle.
         Angle BCA,  32.93 deg., is an acute angle.
         Angle CAB, 124.12 deg., is an obtuse angle.

A:(  223,   74), B:(  305,  989), C:(  787,  353)
         Angle ABC,  42.28 deg., is an acute angle.
         Angle BCA,  79.16 deg., is an acute angle.
         Angle CAB,  58.56 deg., is an acute angle.

A:(   27,  671), B:(  542,  803), C:(  148,  486)
         Angle ABC,  24.44 deg., is an acute angle.
         Angle BCA,  84.37 deg., is an acute angle.
         Angle CAB,  71.19 deg., is an acute angle.

A:(  107,  159), B:(  719,  739), C:(  262,  125)
         Angle ABC,   9.88 deg., is an acute angle.
         Angle BCA, 114.29 deg., is an obtuse angle.
         Angle CAB,  55.83 deg., is an acute angle.

A:(  158,  658), B:(  735,  681), C:(  126,  699)
         Angle ABC,   3.98 deg., is an acute angle.
         Angle BCA,  50.34 deg., is an acute angle.
         Angle CAB, 125.69 deg., is an obtuse angle.

A:(  288,  366), B:(  299,  954), C:(  528,  252)
         Angle ABC,  19.14 deg., is an acute angle.
         Angle BCA,  46.53 deg., is an acute angle.
         Angle CAB, 114.34 deg., is an obtuse angle.

A:(  396,   73), B:(  465,  828), C:(  849,  749)
         Angle ABC,  83.60 deg., is an acute angle.
         Angle BCA,  67.80 deg., is an acute angle.
         Angle CAB,  28.60 deg., is an acute angle.

A:(  991,  739), B:(    2,  734), C:(  923,   54)
         Angle ABC,  36.73 deg., is an acute angle.
         Angle BCA,  59.23 deg., is an acute angle.
         Angle CAB,  84.04 deg., is an acute angle.

A:(   81,  747), B:(  710,  630), C:(  434,  365)
         Angle ABC,  54.37 deg., is an acute angle.
         Angle BCA,  88.91 deg., is an acute angle.
         Angle CAB,  36.72 deg., is an acute angle.

A:(  522,  564), B:(  122,  435), C:(  581,  430)
         Angle ABC,  18.50 deg., is an acute angle.
         Angle BCA,  65.61 deg., is an acute angle.
         Angle CAB,  95.89 deg., is an obtuse angle.

A:(  526,  951), B:(  551,  234), C:(  358,  374)
         Angle ABC,  52.05 deg., is an acute angle.
         Angle BCA, 109.72 deg., is an obtuse angle.
         Angle CAB,  18.23 deg., is an acute angle.

A:(  743,  858), B:(  106,  589), C:(  744,  536)
         Angle ABC,  27.64 deg., is an acute angle.
         Angle BCA,  85.07 deg., is an acute angle.
         Angle CAB,  67.28 deg., is an acute angle.

A:(  483,  378), B:(  298,   60), C:(  205,  553)
         Angle ABC,  40.87 deg., is an acute angle.
         Angle BCA,  47.13 deg., is an acute angle.
         Angle CAB,  92.00 deg., is an obtuse angle.

A:(   22,  645), B:(  723,    0), C:(  602,  301)
         Angle ABC,  25.48 deg., is an acute angle.
         Angle BCA, 142.57 deg., is an obtuse angle.
         Angle CAB,  11.95 deg., is an acute angle.

A:(  748,  453), B:(  470,  256), C:(  757,  670)
         Angle ABC,  19.95 deg., is an acute angle.
         Angle BCA,  32.36 deg., is an acute angle.
         Angle CAB, 127.70 deg., is an obtuse angle.

A:(  942,  133), B:(  343,  722), C:(  881,  957)
         Angle ABC,  68.11 deg., is an acute angle.
         Angle BCA,  70.64 deg., is an acute angle.
         Angle CAB,  41.25 deg., is an acute angle.

A:(  522,  766), B:(   17,  908), C:(  162,  346)
         Angle ABC,  59.83 deg., is an acute angle.
         Angle BCA,  55.07 deg., is an acute angle.
         Angle CAB,  65.10 deg., is an acute angle.

A:(   84,  246), B:(  257,  549), C:(  578,  748)
         Angle ABC, 151.52 deg., is an obtuse angle.
         Angle BCA,  13.66 deg., is an acute angle.
         Angle CAB,  14.82 deg., is an acute angle.

A:(  533,  398), B:(  376,  247), C:(  947,  661)
         Angle ABC,   7.94 deg., is an acute angle.
         Angle BCA,   3.52 deg., is an acute angle.
         Angle CAB, 168.54 deg., is an obtuse angle.

A:(  565,  780), B:(  404,  193), C:(  651,  761)
         Angle ABC,   8.16 deg., is an acute angle.
         Angle BCA,  78.96 deg., is an acute angle.
         Angle CAB,  92.88 deg., is an obtuse angle.

A:(  343,  644), B:(  168,  159), C:(  360,  620)
         Angle ABC,   2.77 deg., is an acute angle.
         Angle BCA, 122.08 deg., is an obtuse angle.
         Angle CAB,  55.15 deg., is an acute angle.

A:(  717,  640), B:(  439,  770), C:(  231,  795)
         Angle ABC, 161.79 deg., is an obtuse angle.
         Angle BCA,  10.84 deg., is an acute angle.
         Angle CAB,   7.37 deg., is an acute angle.

A:(  116,  720), B:(  780,  261), C:(  986,  919)
         Angle ABC,  72.73 deg., is an acute angle.
         Angle BCA,  59.73 deg., is an acute angle.
         Angle CAB,  47.54 deg., is an acute angle.

A:(  896,  642), B:(  657,  143), C:(  865,   93)
         Angle ABC,  77.92 deg., is an acute angle.
         Angle BCA,  79.72 deg., is an acute angle.
         Angle CAB,  22.36 deg., is an acute angle.

A:(  248,  290), B:(  271,  244), C:(  690,  379)
         Angle ABC,  98.71 deg., is an obtuse angle.
         Angle BCA,   6.47 deg., is an acute angle.
         Angle CAB,  74.82 deg., is an acute angle.

A:(  881,  128), B:(  625,  619), C:(  500,  283)
         Angle ABC,  47.94 deg., is an acute angle.
         Angle BCA,  91.73 deg., is an obtuse angle.
         Angle CAB,  40.33 deg., is an acute angle.

A:(  673,  781), B:(  237,  875), C:(  758,  939)
         Angle ABC,  19.17 deg., is an acute angle.
         Angle BCA,  54.72 deg., is an acute angle.
         Angle CAB, 106.11 deg., is an obtuse angle.

A:(  312,  318), B:(  528,  687), C:(  866,  910)
         Angle ABC, 153.76 deg., is an obtuse angle.
         Angle BCA,  13.48 deg., is an acute angle.
         Angle CAB,  12.76 deg., is an acute angle.

A:(  574,   54), B:(  132,  455), C:(  329,  490)
         Angle ABC,  52.29 deg., is an acute angle.
         Angle BCA, 109.26 deg., is an obtuse angle.
         Angle CAB,  18.45 deg., is an acute angle.

A:(  265,  438), B:(  123,  968), C:(  978,  813)
         Angle ABC,  64.73 deg., is an acute angle.
         Angle BCA,  38.02 deg., is an acute angle.
         Angle CAB,  77.26 deg., is an acute angle.

A:(  207,  938), B:(  175,  964), C:(  274,    5)
         Angle ABC,  45.01 deg., is an acute angle.
         Angle BCA,   1.79 deg., is an acute angle.
         Angle CAB, 133.20 deg., is an obtuse angle.

A:(  994,  458), B:(  922,  245), C:(  655,  277)
         Angle ABC, 101.84 deg., is an obtuse angle.
         Angle BCA,  34.93 deg., is an acute angle.
         Angle CAB,  43.22 deg., is an acute angle.

A:(  286,  575), B:(  382,  150), C:(  271,  519)
         Angle ABC,   4.01 deg., is an acute angle.
         Angle BCA, 148.26 deg., is an obtuse angle.
         Angle CAB,  27.72 deg., is an acute angle.

A:(  644,  544), B:(  926,  508), C:(  590,   74)
         Angle ABC,  59.53 deg., is an acute angle.
         Angle BCA,  31.19 deg., is an acute angle.
         Angle CAB,  89.28 deg., is an acute angle.

A:(  614,  513), B:(  623,   27), C:(  690,  685)
         Angle ABC,   6.87 deg., is an acute angle.
         Angle BCA,  18.02 deg., is an acute angle.
         Angle CAB, 155.10 deg., is an obtuse angle.

3、ライセンスと例外規定及び免責事項

本ソフトウェアは、著作権者およびコントリビューターによって「現状のまま」提供
されており、明示黙示を問わず、商業的な使用可能性、および 特定の目的に対する
適合性に関する暗黙の保証も含め、またそれに限定されない、いかなる保証もない。
著作権者もコントリビューターも、事由のいかんを問わず、損害発生の原因いかんを
問わず、かつ責任の根拠が契約であるか厳格責任であるか(過失その他の)不法行為
であるかを問わず、仮にそのような損害が発生する可能性を知らされていたとしても、
本ソフトウェアの使用によって発生した(代替品または代用サービスの調達、使用の
喪失、データの喪失、利益の喪失、業務の中断も含め、またそれに限定されない)
直接損害、間接損害、偶発的な損害、特別損害、懲罰的損害、又は結果損害について、
一切責任を負わないものとする。
但し下記の制限が課される。
1、人命に危害を与える機器及びソフトウエアでの使用を禁じる。
2、前記1に記載物に連携する、機器、ソフトウエア及びシステムでの使用を禁じる。


4、終わりに

中学や高校の数学、ベクトルや、代数が如何に重要な学習だったかを痛感させられる。
やはり、数学(算数)は面白い。 対称性は美しい