我有一个要求,我需要将2个轴值存储到线性向量中,有些值[x = 0] [y = 1]和[y = 0] [x = 1]我知道如何保存< / p>
我只是添加了i + j来查找索引,但是现在它在所有情况下都可以工作
我有0> = x <= 200和0> = y <= 103
其中x增量x = x + 1 y增量y = y + 1.5
我可以导出任何通用公式来线性保存所有数据
答案 0 :(得分:1)
vector_1d_index = vector_2d_row_index * vector_2d_row_length + vector_2d_column_index
...假设您的2D向量是i)行大和ii)矩形(等长行)。
({vector_1d_size = vector_2d_row_count * vector_2d_row_length
)。
答案 1 :(得分:0)
您的描述含糊。但是我可以收集的是,您正在寻求将二维索引中的值存储在一维数组中。通用技术是使用类似如下的方法:
假设row, col
是二维索引坐标
<1-d index> = <max number of columns> * row + col
答案 2 :(得分:0)
如果我没看错的话,您想要一种在C ++中存储2D浮点索引数组的方法。您将需要进行一些转换,因为C ++“仅支持1D数组”(严格来说并非如此,但我们会假装确实如此)。
首先,我们需要知道范围和增量。您提供了它们,对于X,范围分别为[0, 200]
,对于Y,[0, 103]
的范围分别为1
和1.5
。
这意味着我们有((200-0)/1) = 200
个可能的X值和((103-0)/1.5) = 68.666...
个Y的可能值。我们将为Y选择69个可能的值。
所以,我们可以有以下数组:
int my_array_of_ints[69 * 200];
例如,项目[X=0][Y=0]
将是我们的[0 * 69 + 0]
索引(项目my_array_of_ints[0]
),而我们的[X=1][Y=1.5]
将是我们的[1 * 69 + 1]
索引(项目{ {1}})。请注意,因为Y增量固定为1.5(即Y必须为0或1.5或3或4.5或6或...),所以我们不能使用[Y = 0.5]或[Y = 1]的项。
用于将2D索引转换为1D线性索引的函数为:
my_array_of_ints[70]
位置:
#include <cmath>
int get_element(float x, float y){
int index_x = std::round(x / 1);
int index_y = std::round(y / 1.5);
if ((0 <= index_x) && (index_x < 200) &&
(0 <= index_y) && (index_y < 69)){
return my_array_of_ints[index_y * 200 + index_x];
} else {
// You should decide what to do if x or y is out-of-range
return 0;
}
}
是x的增量1
是y的增量1.5
是该范围内具有此增量的x的可能值的数量200
是该范围内具有此增量的y的可能值的数量。所以我们可以做这样的事情:
69
它将返回get_element(1, 1.5)
中[X=1][Y=1.5]
的值。
将此代码包装在一个类上,对数组的类型进行模板化,对范围和增量进行概括,并提供一个虚拟的main:
my_array_of_ints
输出:
#include <cmath>
#include <iostream>
template <typename Datatype> class Vector2D {
float x_increment;
float x_minimum;
float x_maximum;
float y_increment;
float y_minimum;
float y_maximum;
// For example, Y range [0, 103] with increment 1.5
// results in 69 possibles values for Y, and we need to
// remember to "linearize" the indexes
int x_possibles;
int y_possibles;
Datatype *array;
public:
Vector2D(float x_increment, float y_increment,
float x_maximum, float y_maximum,
float x_minimum=0, float y_minimum=0)
: x_increment(x_increment), x_minimum(x_minimum),
x_maximum(x_maximum), y_increment(y_increment),
y_minimum(y_minimum), y_maximum(y_maximum),
// These two may seem arcane, but they are the
// generalization of how we found the values initially
x_possibles(std::ceil((x_maximum-x_minimum)/x_increment)),
y_possibles(std::ceil((y_maximum-y_minimum)/y_increment)),
array(new Datatype[y_possibles * x_possibles]) {
// This may help to understand this 2D Vector
std::cout << "Creating 2D vector X in range ["
<< x_minimum << ", " << x_maximum
<< "] with increment of " << x_increment
<< " (totalizing " << x_possibles
<< " possible values for x) "
<< " and Y in range [" << y_minimum
<< ", " << y_maximum << "] with increment of "
<< y_increment << " (totalizing " << y_possibles
<< " values for y)."
<< std::endl;
}
// Frees up the raw array
~Vector2D(){
delete this->array;
}
Datatype& get_element(float x, float y){
int index_x = std::round((x-x_minimum)/this->x_increment);
int index_y = std::round((y-y_minimum)/this->y_increment);
// This debug message may help understand this function
// It is, in some sense, the answer of this question
std::cout << "The 2D point [X=" << x << ", Y=" << y
<< "] is mapped into the vector index ["
<< index_y << " * " << x_possibles
<< " + " << index_x << "]" << std::endl;
if ((0 <= index_x) && (index_x < x_possibles) &&
(0 <= index_y) && (index_y < y_possibles)){
return this->array[index_y * x_possibles + index_x];
} else {
// You should decide what to do if x or y is out-of-range
return this->array[0];
}
}
};
int main(){
// And you could use that class like this:
// A 2D-like vector with X [0, 200] inc. 1
// and Y [0, 103] inc. 1.5 of floats
Vector2D<float> my_data(1, 1.5, 200, 103, 0, 0);
// Sets [X=1][Y=1] to 0.61345
my_data.get_element(1, 1) = 0.61345;
auto elem1 = my_data.get_element(1, 1);
// Prints the [X=1][Y=1] to screen
std::cout << "[X=1][Y=1] is "
<< elem1
<< std::endl;
// Gets a few more interesting points
my_data.get_element(0, 0);
my_data.get_element(1, 1.5);
my_data.get_element(10, 15);
my_data.get_element(200, 103);
// A separator
std::cout << "---" << std::endl;
// Another example, this time using chars
// X is [-10, 1] inc. 0.1 and Y is [-5, 3] inc. 0.05
Vector2D<char> my_chars(0.1, 0.05, 1, 3, -10, -5);
// Sets [X=-4.3][Y=2.25] to '!'
my_chars.get_element(-4.3, 2.25) = '!';
auto elem2 = my_chars.get_element(-4.3, 2.25);
std::cout << "[X=-4.3][Y=2.25] is "
<< elem2
<< std::endl;
}
希望有帮助。