Is there any method for comparing two points of 3d space?

Recommended Answers

All 2 Replies

Even more, you can use strict comparison operator == created by yourself.

class   Point3D
  double    x,y,z;
  void  initializeK()
  int   operator ==(const Point3D &pnt)
    int ires;


    return ires;

  Point3D   pnt1,pnt2;

Sorry but I really don't like this piece of code from skaa.

The problem for me is that double precision numbers have floating point error, so you really don't know [most of the time] if they are going to be equal e.g. is 1e-76 equal to 1.1e-76. This kind of thing occurs all the time when doing arithmatic with double precision numbers and is often refered to as round off error.

For a 3d vector space, the one way to compare is by the Euclidian distance between two vector points. This can be readily found by calculating the inner product [often called the dot product] of the difference vector and then comparing the result, either to a fraction of the magnatude of the original vectors or to a fixed tolerance. e.g.

bool Point3D::operator==(const Point3D& A) const
  const double Tol(1e-8*1e-8);   // Squared tolerance  
  if (this!=&A)
       const double D=(A.x-x)*(A.x-x)+
       if (fabs(D)>Tol) return 0;
  return 1;

Not the best code (as normally Point3D would provide its own dot product method and you would use that.

Note that a dot product would normal have a square root e.g. sqrt(D), but since it is only a comparison, we can avoid that extra computation.

However, that is not the only comparison that you might like to make. You might be interested in direction, e.g. are the point vector parallel regardless of size: e.g. a.b / |a| |b|
[which is is dot product of a with b divided by the magnitude of a and b] and that equation will give you cosine of the angle between the vectors [ if it is nearly +/-1.0 then they are parallel].

If you give us an application example we can provide a bit more help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.