Which ortho axis is closest to this vector?

   130   1   0
User Avatar
Member
272 posts
Joined: 8月 2018
Offline
Seeking your kind help on a VEX question...

I have a vector, and I'd like to find the ortho axis which is nearest to it, and whether it faces in the positive or negative direction on that axis - ie: one of six results.

Can anyone suggest a VEX solution, or point me in the right direction? (pun happily accepted : )


With thanks.
User Avatar
Member
218 posts
Joined: 5月 2017
Offline
You could simply compare the distance to each ortho axis:

printf('------------ \n');
vector p =  point(1, 'P', 0);
addpoint(0, 0);
addpoint(0, p);
addprim(0, 'polyline', 0, 1);

vector ort_axis = 0;
float min_dist = 0;

for (int i = 0; i < 6; ++i)
{
    vector tmp = 0;
    tmp[i / 2] = (i % 2 * 2) - 1;
    
    float tmp_dist = distance(p, tmp);
    if (min_dist < tmp_dist)
    {
        min_dist = tmp_dist;
        ort_axis = floor(tmp * -1);
    }
}

printf('%d \n', ort_axis );
  • Quick Links