计算2条线的夹角

原理:矢量的向量积 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;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值