0

I have a customized array implementation for a geometry (vertex) . Each element of the array is represented by the vertex which has a Point . Now I want to check the distance between each point for the vertex in the array . So essentially for every vertex in the array of size n I will loop till n and calculate the distance of vertex point with all n vertex points in the array . So a pseudo code will look like this

    func MyFunc( Array iVrtxList , vrtx inpVertex )
     {
        point refPt = inpVertex->getPoint(); 
       for ( i=0 ; i < iVrtxList.size(); i++)   
       {
            if( distanceBetween(iVertexList(i).point ,rePt ) == 0 )
               return 
       }
       iVrtxList.add(inpVertex);
      }
}

So I want to avoid N X N looping . I thought of sorting the container and then check only the subsequent element for the distance . However I seem to miss some elements

sameer karjatkar
  • 2,017
  • 4
  • 23
  • 43
  • I think what you are trying to do is an instance of Single-source shortest path problem. Your can read about the problem and it's time complexity at: https://en.wikipedia.org/wiki/Shortest_path_problem – gst Nov 02 '21 at 11:44
  • @gst its like graph having vertices . But I want to known the distance between each of them . I have a single edge , which has n vertices . Some times u might end up having duplicate vertices , so in order to filter them we check if the distance between them is 0 – sameer karjatkar Nov 02 '21 at 12:45
  • It seems like you are just checking to make sure that you don't add the same Point twice. A faster way to do this would be to keep the Points in a Set/Map/Hashset/Dictionary and then just check to see if the Point is already in the Hashmap (or whatever it's called in the programming language that you are using). – RBarryYoung Nov 02 '21 at 16:01
  • I am sure this could be a classic geometry situation . You have a wire edge and there are vertices of the edge which u want to check . so there is a start vertex and a end vertex . So if there are 100 vertices , I should have 50 unique vertices since the remaining 50 will be duplicate as startVertex == endVertex of the subsequent point – sameer karjatkar Nov 03 '21 at 06:14
  • I see this R implementation https://stackoverflow.com/questions/40999545/method-for-calculating-distance-between-all-points-in-a-dataframe-containing-a-l but I want its algorithmic implementation – sameer karjatkar Nov 07 '21 at 08:04

1 Answers1

0

I kind of achieved our objective to skip the duplicate vertices without using a N X N approach . I used a multimap to keep track of the vertices . The key was hashed with the x,y,z values which we adjusted to precision to make it work with the dataset . However the hash calculation is vulnerable as any mapping causing collision will defeat the purpose . Following are the definitions

class PointCoords
{
public:

    PointCoords(double  ix, double iy, double iz) : _x(ix), _y(iy), _z(iz) { };

    double _x;
    double _y;
    double _z;

};

class PointCoords_hash
{
public:
    size_t operator()(const PointCoords& v) const
    {
        auto f1 = std::hash<double>{}(round(v._x * 10) / 10);
        auto f2 = std::hash<double>{}(round(v._y * 10) / 10);
        auto f3 = std::hash<double>{}(round(v._z * 10) / 10);
        size_t hCode = (f1 ^ f2 ^ f3) << 1;
        return ((f1 ^ f2 ^ f3) << 1);
    };
};

class PointCoords_equal
{
public:
    bool operator()(const PointCoords& u, const PointCoords& v) const
    {
        return (equal(u._x, v._x, 1e-6) &&
            equal(u._y, v._y, 1e-6) &&
            equal(u._z, v._z, 1e-6));
    };
};
bool :equal( double d1, double d2, double err) 
{
    
    return ( fabs( d1 -d2) <= err);
}
sameer karjatkar
  • 2,017
  • 4
  • 23
  • 43