The tolerance used to determine vector3d equality and the length of vector3d

  Kiến thức lập trình

I use a tolerance=1e-10 to check whether two vector3ds are equal, using following function is_same:

bool is_same(const vector3d& x, const vector3d& y) {
  var delta = x-y;
  return delta.length() < tolerance; // tolerance is 1e-10
}

But here comes a situation that I can’t fix it. I have a class called lineSegments,

struct lineSegments {
   vector3d s, e; // s means start point, e means end point
}

When I calculate the intersection of two lineSegments. I used equation (e1-s1)*k1+s1 = (e2-s2)*k2+s2 to get k1 and k2, so the intersection point is (e1-s1)*k1+s1.
But when the two lineSegments like that:

auto l1 = lineSegments{
    {639482584.0, 2115435624.0, 0},    // s1
    {1658825857.0, 1245760131.0, 0}};  // e1
auto l2 = lineSegments{
    {1764535160.0, 1562640819.0, 0},   // s2
    {1658825857.0, 1245760131.0, 0}};  // e2

I got the k1=1.0000000000000011, k2=0.99999999999999989. And then I use is_same to check the intersecion point with point P{1658825857.0, 1245760131.0, 0}, it failed. Because the delta length=((e1-s1)*k1+s1-P).length() is 4.76837158203125E-07, which is larger than 1e-10.

How can I fix this bug? Or should I use vector of small length to varify my function?

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT