Post
Find Point of Intersection of Two Lines
- Ottieni link
- X
- Altre app
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); ...
Finding points on a line with a given distance
- Ottieni link
- X
- Altre app
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
- Ottieni link
- X
- Altre app
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;; ...
Rotate Point Around Origin
- Ottieni link
- X
- Altre app
//---------------------------------------------------------------------------- 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; }
Angle between two points
- Ottieni link
- X
- Altre app
//---------------------------------------------------------------------------- double CalculateAngle(Point point1, Point point2, Point origin) { double angleToP1 = atan2((point1.X - origin.X), (point1.Y - origin.Y)); double angleToP2 = atan2((point2.X - origin.X), (point2.Y - origin.Y)); double angle = angleToP2 - angleToP1; if (angle < 0) angle += (2 * vtkMath::Pi()); return angle; }