如何从向量 3 一维数组返回 y 值

发布于 2024-11-07 01:06:52 字数 401 浏览 0 评论 0原文

我有一个充满顶点位置的向量 3 (x,y,z) 1D 动态数组。

如何返回给定 x 和 z 坐标处的 y 值?

编辑::抱歉缺乏细节。

我使用的是c++,在Visual Studio 2008中编译。 该向量是一个向量类,存储 3 个浮点值,定义 x、y 和 z 变量,用于位置。

就像 Vector3 *array;

数组=新的Vector3[100];

许多位置值被添加到数组中。

当您访问数组成员以获得特定值时,它就像

array[0].y

但我想找到与特定 x 和 z 相对应的 y 值,

例如

GetY(float x, float z)

...

return y;

I have a vector 3 (x,y,z) 1D dynamic array full of vertex positions.

How would I return the y value at a given x and z coordinate?

Edit:: Sorry about the lack of detail.

I am using c++, compiling in Visual Studio 2008.
The vector is a vector class storing 3 float values defining an x, y and z variable, it is used for position.

It's like Vector3 *array;

array = new Vector3[100];

Lots of positional values are added to the array.

When you access a member of the array for a specific value it's like

array[0].y

But I want to find a the y value that corresponds to a specific x and z

like

GetY(float x, float z)

...

return y;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

多彩岁月 2024-11-14 01:06:52

我想你有类似的东西

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

I suppose you have something like

struct Vec3D{
  float x, y, z;
};

Vec3D vec3d_arr[20];

Then, to get what you want, you'll need to iterate over the array.

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);
}

Edit: On your comment:

#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];
}

Though a problem that I forgot is the inaccuracy of floats, so maybe you get problems with the map. Comment back if you do. :)


Edit:
Added a more safe version, employing a float_wrapper.

浅紫色的梦幻 2024-11-14 01:06:52

您只需在数组中搜索具有给定 x 和 z 坐标的向量元素,并获取该元素对应的 y 值。

You just have to search your array for the vector element with the given x and z coordinates and the get this element's corresponding y value.

怎会甘心 2024-11-14 01:06:52

如果你的意思是,你的向量类似于 float vector[3]; 那么 vector[1] 引用数组中的第二个值,表示向量的 Y 坐标。

If you mean, that your vector is something like float vector[3]; then vector[1] references the second value in the array, said your Y coordinate of the vector.

沒落の蓅哖 2024-11-14 01:06:52

您觉得这段代码怎么样:

#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;
    }
}

How do you like this code:

#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;
    }
}
拍不死你 2024-11-14 01:06:52

永远不要对未排序的数组/向量执行搜索。即使今天只有 100 个元素,明天也可能会变成 100000 个。看看Wiki排序算法,其中一些很容易理解和实现。然后查看 Wiki 二分搜索。我将从快速排序开始,这也让您很好地了解二分搜索的工作原理。
相信我,以后你会感谢我的。

Please, NEVER EVER perform a search on an unsorted array/vector. Even if it has only 100 elements today, it might become 100000 tomorrow. Take a look at Wiki Sorting Algorithm, some of them are easy to understand and implement. Then look at Wiki Binary search. I would start with quick sort, which also gives you a good idea how binary search works.
Believe me, you will thank me later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文