如何从向量 3 一维数组返回 y 值
How to return y value from vector 3 1D array
我有一个充满顶点位置的向量 3 (x,y,z) 一维动态数组。
如何在给定的 x 和 z 坐标处返回 y 值?
编辑:: 很抱歉缺乏细节。
我正在使用c ++,在Visual Studio 2008中编译。向量是一个向量类,存储 3 个浮点值,定义一个 x、y 和 z 变量,它用于位置。
它就像矢量3 *数组;
数组 = 新向量 3[100];
数组中添加了许多位置值。
当您访问特定值的数组成员时,它就像
数组[0].y
但是我想找到一个对应于特定 x 和 z 的 y 值
喜欢
GetY(浮点数 x, 浮点数 z)
。
返回 y;
我想你有类似的东西
struct Vec3D{
float x, y, z;
};
Vec3D vec3d_arr[20];
然后,要获得所需的内容,您需要遍历数组。
float GetYforXZ(Vec3D* arr, unsigned int length, float x_val, float z_val){
for(unsigned i=0; i < length; ++i){
if(arr[i].x == x_val && arr[i].z == z_val)
return arr[i].y;
}
}
int main(){
Vec3D arr[20];
// init arr
float y = GetYforXZ(arr,20,15.4f,23.3f);
}
编辑:关于您的评论:
#include <map>
#include <math>
using namespace std;
struct Vec3D{
float x, y, z;
};
const float float_eps = 1e-5;
struct float_wrapper{
float _value;
float_wrapper()
: _value(0.0f) {}
float_wrapper& operator=(float f){
_value = f;
return *this;
}
operator float() const{
return _value;
}
};
bool operator==(float_wrapper const& lhs, float_wrapper const& rhs){
float tmp = fabs(lhs._value - rhs._value);
return tmp < float_eps && tmp >= 0;
}
bool operator<(float_wrapper const& lhs, float_wrapper const& rhs){
return lhs._value < rhs._value;
}
typedef map< float_wrapper,float_wrapper > zy_map;
typedef map< float_wrapper,zy_map > xzy_map;
void init_vertex_mapping(xzy_map& a_map, Vec3D* arr, size_t length){
for(size_t i=0; i < length; ++i){
Vec3D& vertex = arr[i];
zy_map& zy = a_map[vertex.x];
zy[vertex.z] = vertex.y;
}
}
int main(){
xzy_map vertex_map;
Vec3D vertex_array[100] = { {0,0,0},{0,0,0},{0,0,0},{-3.14f,42.0f,-13.37f},{0,0,0} };
init_vertex_mapping(vertex_map, vertex_array, 100);
float y = vertex_map[-3.14f][-13.37f];
}
虽然我忘记的一个问题是float
的不准确性,所以也许你会遇到地图问题。如果您这样做,请回复评论。 :)
编辑:添加了更安全的版本,采用float_wrapper
。
您只需要在数组中搜索具有给定 x 和 z 坐标的向量元素,并获取此元素的相应 y 值。
如果你的意思是,你的向量是类似于float vector[3];
那么vector[1]
引用数组中的第二个值,比如你的向量的Y坐标。
你觉得这段代码怎么样:
#include <iostream>
#include <algorithm>
using namespace std;
class Vector3
{
public:
int x;
int y;
int z;
enum SearchType
{
XY, YZ, XZ
};
static SearchType searchType;
Vector3(int x = 0, int y = 0, int z = 0)
: x(x), y(y), z(z)
{
}
bool operator == (const Vector3 & vec)
{
switch(searchType)
{
case XY: return (x == vec.x) && (y == vec.y);
case YZ: return (y == vec.y) && (z == vec.z);
case XZ: return (x == vec.x) && (z == vec.z);
}
return false;
}
};
Vector3::SearchType Vector3::searchType = XY;
int main()
{
Vector3 * array = new Vector3 [100];
array[57].x = 5;
array[57].y = 100;
array[57].z = 6;
Vector3::searchType = Vector3::XZ; // Specify find type as "XZ"
Vector3 * vec = std::find(array, array+100, Vector3(5,0,6));
if (vec != array+100)
{
cout << "Value for X == 5 and Z == 6 is in "
<< (vec-array) << " item and it eq " << (*vec).y;
}
}
请不要对未排序的数组/向量执行搜索。即使它今天只有 100 个元素,明天也可能变成 100000 个。看看 Wiki 排序算法,其中一些很容易理解和实现。然后看看维基二进制搜索。我将从快速排序开始,这也可以让您很好地了解二进制搜索的工作原理。相信我,你以后会感谢我的。
相关文章:
- 将二维数组的所有元素插入到一维数组中
- C++语法差异:二维和一维数组(指针算术)
- 将一维数组写入 CSV C++中的不同列?
- C++:将矩阵存储在一维数组中
- 如何在一维数组中的每个元素中都有多个int值
- 如何返回多维数组?错误C2440
- 以C++填充一维数组
- 用于在一维数组上嵌套循环操作的正确 openmp 指令
- 如何在 <threads> c++ 中使用和一维数组进行矩阵乘法?
- C++按内存地址将多维数组更改为一维数组
- 在 c++ 中返回一维数组时出错
- 一维数组映射方式的性能差异问题
- 如何使用一维数组更改二维数组中的值?
- 从 c++ xll 返回二维数组以 excel,返回
- 以C++返回二维数组
- 使用二维数组作为一维数组是否正确?可能会导致未定义的行为左右?
- 在没有矢量的C 中返回二维数组
- 将一维数组转换为二维数组
- 如何使用一维数组列表初始化二维数组
- 如何从向量 3 一维数组返回 y 值