Post

Visualizzazione dei post da marzo, 2021

Find Point of Intersection of Two Lines

  pdd LinesIntersection(pdd A, pdd B, pdd C, pdd D) {      // Line AB represented as a1x + b1y = c1      double a1 = B.second - A.second;      double b1 = A.first - B.first;      double c1 = a1*(A.first) + b1*(A.second);          // Line CD represented as a2x + b2y = c2      double a2 = D.second - C.second;      double b2 = C.first - D.first;      double c2 = a2*(C.first)+ b2*(C.second);          double determinant = a1*b2 - a2*b1;          if (determinant == 0)      {          // The lines are parallel. This is simplified          // by returning a pair of FLT_MAX          return make_pair(FLT_MAX, FLT_MAX);      }      else      {          double x = (b2*c1 - b1*c2)/determinant;          double y = (a1*c2 - a2*c1)/determinant;          return make_pair(x, y);      } }

Finding points on a line with a given distance

Immagine
Start point - (x0, y0) End point - (x1, y1) We need to find a point (xt, yt) at a distance dt from start point towards end point. The distance between Start and End point is given by d = sqrt((x1 - x0)^2 + (y1 - y0)^2) Let the ratio of distances, t = dt / d Then the point (xt, yt) = (((1 - t) * x0 + t * x1), ((1 - t) * y0 + t * y1)) When 0 < t < 1 , the point is on the line. When t < 0 , the point is outside the line near to (x0, y0) . When t > 1 , the point is outside the line near to (x1, y1) .