C语言 二维线性查表linearInterpolation 100行实现通用线性查表

介绍

linearInterpolation 模块是基于C语言提供的一维/二维线性插值算法。表格值是为float类型,适合在单片机中使用。

软件架构

序号文档说明
1linearInterpolation.h线性查表头文件
2linearInterpolation.c线性查表实现
3main.c演示程序

引入模块

git submodule add git@gitee.com:DyyYq/linearInterpolation.git submodules/linearInterpolation

引入头文件

以下代码引入了linearInterpolation.h头文件,定定义了演示用的表变量 demoTable。 表 demoTable可以当一维表用,也可以当二维表用。

#include "linearInterpolation.h"

linearInterpolation_t demoTable;

填充表格内容(一维表)

以下代码对表demoTable进行了一维内容填充👇

/*
* Y
* ↑
* | 
* 20-------------*
* |
* 15--------*
* |
* 10---* 
* |
* *----2----4----8-------------> X
*/
//static的约束保障了这些变量的空间不会被收回
static float xValues[] = {2, 4, 8};
static float yValues[] = {10.0f, 15.0f, 20.0f};
tableInitXY_withValues(&demoTable, 3, xValues,yValues);

填充表格内容(二维表)

以下代码对表demoTable进行了二维内容填充👇

/*
* *----1----2-----------------> X
* |
* 1    2    3
* |
* 2    3    4
* |
* 3    4    8
* |
* ↓
* Y
*/
//static的约束保障了这些变量的空间不会被收回
static float xValues[] = {1,2};
static float yValues[] = {1,2,3};

static float zValues[][2] = {   {2,3},
                                {3,4},
                                {4,8}};
tableInitXYZ_withValues(&demoTable, 2, 3, xValues, yValues, zValues);

一维查表

如果你对表格进行了一维填充,则可以对这个表格进行一维查表。如下是对表demoTable进行一维查表👇

float userX=0;
while (1)
{
    printf("please input X value:"); scanf("%f", &userX);
    printf("the x is: %f, then the y should be: %f\n", userX, demoTable.getY(&demoTable, userX));
}

方法 getY 可以根据指定的x值查表获取对应的 y 值。

二维查表

如果你对表格进行了二维填充,则可以对这个表格进行二维查表。如下是对表demoTable进行二维查表👇

float userX=0, userY=0;
while (1)
{
    printf("please input X value:"); scanf("%f", &userX);
    printf("please input Y value:"); scanf("%f", &userY);
    printf("the coord is: (%f, %f), then the z should be: %f\n", userX, userY, demoTable.getZ(&demoTable, userX, userY));
}

方法 getZ 可以根据指定的 xy 值,查表获取对应的 z值。

小结

以上就是本模块所提供的线性查表方法了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

团圆吧

1 分钱,求鼓励。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值