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

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 …

## All 2 Replies

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

``````class   Point3D
{
public:
double    x,y,z;
Point3D()
{
initializeK();
}
void  initializeK()
{
x=0.;y=0.;z=0.;
}
~Point3D()
{
}
int   operator ==(const Point3D &pnt)
{
int ires;

ires=0;
if(pnt.x==this->x&&pnt.y==this->y&&pnt.z==this->z)
ires=1;

return ires;
}
};

...
Point3D   pnt1,pnt2;
pnt1.x=1.;pnt1.y=2.;pnt1.z=3.;
pnt2.x=1.;pnt2.y=2.;pnt2.z=3.1;
if(pnt2==pnt1)
printf("=");
else
printf("!=");
...
``````

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)+
(A.y-y)*(A.y-y)+
(A.z-z)*(A.z-z);
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 learning and sharing knowledge.