原理:矢量的向量积 a · b = |a| * |b| * cos(α) => α = acos((a · b) / (|a| * |b|))
源码及测试代码如下:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <float.h>
#include <math.h>
/*****************************************************************************
*
* Macro definition
*
*****************************************************************************/
/*****************************************************************************
*
* Structure/Class definition
*
*****************************************************************************/
//
typedef struct Point {
double x;
double y;
} Point;
//
typedef struct Line {
Point start;
Point end;
} Line;
/*****************************************************************************
*
* Data definition
*
*****************************************************************************/
/*****************************************************************************
*
* Function Entity
*
*****************************************************************************/
//
// 向量 a 点乘 b = |a| * |b| * cos(angle) => angle = acos((a 点乘 b) / (|a| * |b|))
//
void CalcAngleOfTwoLine(Line &line_1, Line &line_2, double &angle) {
double inner_product;
double len_product;
double line_1_x_distance;
double line_1_y_distance;
double line_2_x_distance;
double line_2_y_distance;
//
line_1_x_distance = line_1.end.x - line_1.start.x;
line_1_y_distance = line_1.end.y - line_1.start.y;
line_2_x_distance = line_2.end.x - line_2.start.x;
line_2_y_distance = line_2.end.y - line_2.start.y;
//
inner_product = line_1_x_distance * line_2_x_distance + line_1_y_distance * line_2_y_distance;
//
len_product = sqrt(line_1_x_distance * line_1_x_distance + line_1_y_distance * line_1_y_distance) * \
sqrt(line_2_x_distance * line_2_x_distance + line_2_y_distance * line_2_y_distance);
//
angle = acos(inner_product/len_product);
printf("CalcAngleOfTwoLine - %f\r\n", angle * 180.0 / M_PI);
}
//
int main(int argc, char **argv) {
Line line_1;
Line line_2;
double angle;
//
//
//
line_1.start.x = 1;
line_1.start.y = 1;
line_1.end.x = 2;
line_1.end.y = 1;
//
line_2.start.x = 1;
line_2.start.y = 2;
line_2.end.x = 2;
line_2.end.y = 3;
//
CalcAngleOfTwoLine(line_1, line_2, angle);
//
//
//
line_1.start.x = 1;
line_1.start.y = 1;
line_1.end.x = 2;
line_1.end.y = 1;
//
line_2.start.x = 1;
line_2.start.y = 2;
line_2.end.x = 0;
line_2.end.y = 3;
//
CalcAngleOfTwoLine(line_1, line_2, angle);
//
//
//
line_1.start.x = 2;
line_1.start.y = 1;
line_1.end.x = 1;
line_1.end.y = 1;
//
line_2.start.x = 1;
line_2.start.y = 2;
line_2.end.x = 0;
line_2.end.y = 3;
//
CalcAngleOfTwoLine(line_1, line_2, angle);
//
//
//
line_1.start.x = 1;
line_1.start.y = 1;
line_1.end.x = 2;
line_1.end.y = 1;
//
line_2.start.x = 1;
line_2.start.y = 2;
line_2.end.x = 2;
line_2.end.y = 2;
//
CalcAngleOfTwoLine(line_1, line_2, angle);
//
//
//
line_1.start.x = 2;
line_1.start.y = 1;
line_1.end.x = 1;
line_1.end.y = 1;
//
line_2.start.x = 1;
line_2.start.y = 2;
line_2.end.x = 2;
line_2.end.y = 2;
//
CalcAngleOfTwoLine(line_1, line_2, angle);
return 0;
}