[英]Passing values from 2D array into a function
我有一个具有255个四倍的数组,如下所示。
对于i的每次迭代,我想将每个四元组的前三个值传递给一个函数(下面的getColourDistance中的三个?),以便返回计算结果。
在Arduino的C ++变体中该如何完成?
谢谢!
const int SAMPLES[][4] ={{2223, 1612, 930, 10}, {1855, 814, 530, 20}, {1225, 463, 438, 30}, {1306, 504, 552, 40}, ...};
byte samplesCount = sizeof(SAMPLES) / sizeof(SAMPLES[0]);
for (byte i = 0; i < samplesCount; i++)
{
tcs.getRawData(&r, &g, &b, &c);
colourDistance = getColourDistance(r, g, b, ?, ?, ?);
// do something based on the value of colourDistance
}
int getColourDistance(int sensorR, int sensorG, int sensorB, int sampleR, int sampleG, int sampleB)
{
return sqrt(pow(sensorR - sampleR, 2) + pow(sensorG - sampleG, 2) + pow(sensorB - sampleB, 2));
}
在这种情况下,可以将数组SAMPLES视为二维数组,因此SAMPLES [0] [0]将给出第1个1维数组的第1个元素,SAMPLES [0] [1]将给出第2个第一个1维样本数组的元素,以此类推,考虑到我们可以做到的这一术语,
#include <iostream>
const int SAMPLES[][4] = {{2223, 1612, 930, 10}, {1855, 814, 530, 20}, {1225, 463, 438, 30}, {1306, 504, 552, 40}, ...};
byte samplesCount = sizeof(SAMPLES) / sizeof(SAMPLES[0]);
for (byte i = 0; i < samplesCount; i++)
{
//taking values of r,g,b as before
a=SAMPLES[i][0];//getting values of r,g,b
b=SAMPLES[i][1];//using the knowledge that SAMPLES[i][j]
c=SAMPLES[i][2];//denotes jth element of ith 1-d array of SAMPLES
colourDistance = getColourDistance(r, g, b, a, b, c);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.