Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4618486
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:16:26+00:00 2026-05-22T02:16:26+00:00

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

  • 0

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;

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:16:26+00:00Added an answer on May 22, 2026 at 2:16 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.