线性函数,C语言方式实现

                                   线性函数,C语言方式实现

GitHub仓库:https://github.com/XinLiGH/LinearFunction

PS:博文不再更新,后续更新会在GitHub仓库进行。

 

线性函数,C语言方式实现。程序中涉及到线性函数的概念,详细介绍见维基百科[Linear function](https://en.wikipedia.org/wiki/Linear_function)。

 

1,开发环境

1,操作系统:Windows 10 专业版

2,IDE:Visual Studio 2017 专业版

 

2,程序源码

LinearFunction.h文件

/**
  ******************************************************************************
  * @file    LinearFunction.h
  * @author  XinLi
  * @version v1.0
  * @date    13-September-2018
  * @brief   Header file for LinearFunction.c module.
  ******************************************************************************
  * @attention
  *
  * 

Copyright © 2018 XinLi

* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ****************************************************************************** */ #ifndef __LINEARFUNCTION_H #define __LINEARFUNCTION_H #ifdef __cplusplus extern "C" { #endif /* Header includes -----------------------------------------------------------*/ /* Macro definitions ---------------------------------------------------------*/ /* Type definitions ----------------------------------------------------------*/ typedef struct { float slope; float intercept; }LinearFunction; /* Variable declarations -----------------------------------------------------*/ /* Variable definitions ------------------------------------------------------*/ /* Function declarations -----------------------------------------------------*/ void LinearFunction_PointSlopeInit(LinearFunction *function, float x, float y, float slope); void LinearFunction_TwoPointInit(LinearFunction *function, float x1, float y1, float x2, float y2); void LinearFunction_SetSlope(LinearFunction *function, float slope); void LinearFunction_SetIntercept(LinearFunction *function, float intercept); float LinearFunction_GetSlope(LinearFunction *function); float LinearFunction_GetIntercept(LinearFunction *function); float LinearFunction_GetAbscissa(LinearFunction *function, float y); float LinearFunction_GetOrdinate(LinearFunction *function, float x); float LinearFunction_PointToPointDistance(float x1, float y1, float x2, float y2); float LinearFunction_PointToLineDistance(LinearFunction *function, float x, float y); float LinearFunction_LineToLineDistance(LinearFunction *function1, LinearFunction *function2); float LinearFunction_TwoLineAngle(LinearFunction *function1, LinearFunction *function2); /* Function definitions ------------------------------------------------------*/ #ifdef __cplusplus } #endif #endif /* __LINEARFUNCTION_H */

LinearFunction.c文件

/**
  ******************************************************************************
  * @file    LinearFunction.c
  * @author  XinLi
  * @version v1.0
  * @date    13-September-2018
  * @brief   Linear function module source file.
  ******************************************************************************
  * @attention
  *
  * 

Copyright © 2018 XinLi

* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ****************************************************************************** */ /* Header includes -----------------------------------------------------------*/ #include "LinearFunction.h" #include /* Macro definitions ---------------------------------------------------------*/ /* Type definitions ----------------------------------------------------------*/ /* Variable declarations -----------------------------------------------------*/ /* Variable definitions ------------------------------------------------------*/ /* Function declarations -----------------------------------------------------*/ /* Function definitions ------------------------------------------------------*/ /** * @brief Linear function point slope initialize. * @param [in] function: Linear function data type. * @param [in] x: The abscissa of the point. * @param [in] y: The ordinate of the point. * @param [in] slope: Linear function slope. * @return None. */ void LinearFunction_PointSlopeInit(LinearFunction *function, float x, float y, float slope) { function->slope = slope; function->intercept = y - x * slope; } /** * @brief Linear function two point initialize. * @param [in] function: Linear function data type. * @param [in] x1: The abscissa of the point. * @param [in] y1: The ordinate of the point. * @param [in] x2: The abscissa of the point. * @param [in] y2: The ordinate of the point. * @return None. */ void LinearFunction_TwoPointInit(LinearFunction *function, float x1, float y1, float x2, float y2) { function->slope = (y2 - y1) / (x2 - x1); function->intercept = y1 - x1 * (y2 - y1) / (x2 - x1); } /** * @brief Linear function set slope. * @param [in] function: Linear function data type. * @param [in] slope: Linear function slope. * @return None. */ void LinearFunction_SetSlope(LinearFunction *function, float slope) { function->slope = slope; } /** * @brief Linear function set intercept. * @param [in] function: Linear function data type. * @param [in] intercept: Linear function intercept. * @return None. */ void LinearFunction_SetIntercept(LinearFunction *function, float intercept) { function->intercept = intercept; } /** * @brief Linear function get slope. * @param [in] function: Linear function data type. * @return Linear function slope. */ float LinearFunction_GetSlope(LinearFunction *function) { return function->slope; } /** * @brief Linear function get intercept. * @param [in] function: Linear function data type. * @return Linear function intercept. */ float LinearFunction_GetIntercept(LinearFunction *function) { return function->intercept; } /** * @brief Linear function get abscissa. * @param [in] function: Linear function data type. * @param [in] y: The ordinate of the point on the linear function. * @return The abscissa of the point on the linear function. */ float LinearFunction_GetAbscissa(LinearFunction *function, float y) { return (y - function->intercept) / function->slope; } /** * @brief Linear function get ordinate. * @param [in] function: Linear function data type. * @param [in] x: The abscissa of the point on the linear function. * @return The ordinate of the point on the linear function. */ float LinearFunction_GetOrdinate(LinearFunction *function, float x) { return function->slope * x + function->intercept; } /** * @brief Point to point distance. * @param [in] x1: The abscissa of the point. * @param [in] y1: The ordinate of the point. * @param [in] x2: The abscissa of the point. * @param [in] y2: The ordinate of the point. * @return Point to point distance. */ float LinearFunction_PointToPointDistance(float x1, float y1, float x2, float y2) { return hypotf(x1 - x2, y1 - y2); } /** * @brief Point to line distance. * @param [in] function: Linear function data type. * @param [in] x: The abscissa of the point. * @param [in] y: The ordinate of the point. * @return Point to line distance. */ float LinearFunction_PointToLineDistance(LinearFunction *function, float x, float y) { return fabsf(function->slope * x - y + function->intercept) / hypotf(function->slope, -1.0f); } /** * @brief Line to line distance. * @param [in] function1: Linear function data type. * @param [in] function2: Linear function data type. * @return Line to line distance. */ float LinearFunction_LineToLineDistance(LinearFunction *function1, LinearFunction *function2) { if(function1->slope == function2->slope) { return fabsf(function1->intercept - function2->intercept) / hypotf(function1->slope, -1.0f); } else { return 0.0f; } } /** * @brief Two line angle. * @param [in] function1: Linear function data type. * @param [in] function2: Linear function data type. * @return Two line angle, unit: rad. */ float LinearFunction_TwoLineAngle(LinearFunction *function1, LinearFunction *function2) { return acosf(fabsf(function1->slope * function2->slope + 1.0f) / (hypotf(function1->slope, -1.0f) * hypotf(function2->slope, -1.0f))); }

main.c文件

/**
  ******************************************************************************
  * @file    main.c
  * @author  XinLi
  * @version v1.0
  * @date    13-September-2018
  * @brief   Main program body.
  ******************************************************************************
  * @attention
  *
  * 

Copyright © 2018 XinLi

* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ****************************************************************************** */ /* Header includes -----------------------------------------------------------*/ #include "LinearFunction.h" #include #include /* Macro definitions ---------------------------------------------------------*/ /* Type definitions ----------------------------------------------------------*/ typedef struct { float x; float y; }Point; /* Variable declarations -----------------------------------------------------*/ /* Variable definitions ------------------------------------------------------*/ /* Function declarations -----------------------------------------------------*/ /* Function definitions ------------------------------------------------------*/ /** * @brief Main program. * @param None. * @return None. */ int main(void) { for(;;) { Point pointA = {.x = 2.5f, .y = 3.4f}; Point pointB = {.x = 5.6f, .y = 4.3f}; Point pointC = {.x = 7.8f, .y = 1.5f}; Point pointD = {.x = 4.6f}; Point pointE = {.y = 9.7f}; LinearFunction lineAB = {0}; LinearFunction lineAC = {0}; LinearFunction lineCD = {0}; LinearFunction_TwoPointInit(&lineAB, pointA.x, pointA.y, pointB.x, pointB.y); LinearFunction_TwoPointInit(&lineAC, pointA.x, pointA.y, pointC.x, pointC.y); LinearFunction_PointSlopeInit(&lineCD, pointC.x, pointC.y, LinearFunction_GetSlope(&lineAB)); pointD.y = LinearFunction_GetOrdinate(&lineCD, pointD.x); pointE.x = LinearFunction_GetAbscissa(&lineAC, pointE.y); printf("A(%f, %f)\t", pointA.x, pointA.y); printf("B(%f, %f)\t", pointB.x, pointB.y); printf("C(%f, %f)\t", pointC.x, pointC.y); printf("D(%f, %f)\t", pointD.x, pointD.y); printf("E(%f, %f)\n", pointE.x, pointE.y); printf("AB(y = %fx + %f)\t", LinearFunction_GetSlope(&lineAB), LinearFunction_GetIntercept(&lineAB)); printf("AC(y = %fx + %f)\t", LinearFunction_GetSlope(&lineAC), LinearFunction_GetIntercept(&lineAC)); printf("CD(y = %fx - %f)\n", LinearFunction_GetSlope(&lineCD), LinearFunction_GetIntercept(&lineCD) * (-1.0f)); printf("|AB| = %f\t", LinearFunction_PointToPointDistance(pointA.x, pointA.y, pointB.x, pointB.y)); printf("∠BAC = %f rad\n", LinearFunction_TwoLineAngle(&lineAB, &lineAC)); printf("点 E 到直线 AB 的距离等于 %f\n", LinearFunction_PointToLineDistance(&lineAB, pointE.x, pointE.y)); printf("直线 AB 到直线 CD 的距离等于 %f\n\n", LinearFunction_LineToLineDistance(&lineAB, &lineCD)); Sleep(5000); } }

 

3,运行效果

线性函数,C语言方式实现_第1张图片

你可能感兴趣的:(C/C++)