Post

ABATON... The End

Immagine
 

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) .  

Sort Points by clockwise Angle

Immagine
    Points[4] = {...};     Point origin; ...     // Sort Points by Angles     for(int m=0; m<3; m++)         for (int i = m + 1; i < 4; i++)         {             if (GetClockwiseAngle(Points[i], origin) < GetClockwiseAngle(Points[m], origin);)             {                                SwapPoint(Points, i, m);                }         }         ... double GetClockwiseAngle(Point point, Point origin) {     double angle = 0.0;     angle = atan2(point.Y - origin.Y, point.X - origin.X) * 180 / M_PI;;     return angle; }

Middle point of a line segment

Immagine
A(x,y) B(x,y) MidAB.X = (A.X + B.X) / 2; MidAB.Y = (A.Y + B.Y) / 2;

Rotate Point Around Origin

//---------------------------------------------------------------------------- void RotatePointAroundOrigin(&Point, Point origin, double angle) {     float s = sin(angle);     float c = cos(angle);     // Translate point back to origin:     point.X -= origin.X;     point.Y -= origin.Y;     // Rotate point     float xnew = point.X * c - point.Y * s;     float ynew = point.X * s + point.Y * c;     // Translate point back:     point.X = xnew + origin.X;     point.Y = ynew + origin.Y; }