Basic Mathematical formulae for general geometry of ACM
1. Helen formula to calculate the area
Formula Description : The A,B,C in the formula are triangular three sides long, p is half circumference, and S is the area of the triangle.
2. Vector vector to find area
3. Distance formula for point-to-linemethod One: The distance formula directly asks
Equation Description: the equation is ax+by+c=0, the coordinates of point P is (x0,y0). But the linear equation is not straightforward enough. It is recommended to use method two.
method Two: First use Helen formula to find the area and then ask the Triangle high4. Point-to-line distance formula [or: Point to the nearest point of the segment]
There are four scenarios:
- Point on the line segment, the distance is 0;
- The line segment is a point, which is calculated with two points;
- Three points constitute right triangle or obtuse triangle, then the right angle or obtuse point is the point to the nearest segment of the line;
- Three points constitute the acute triangle, then the distance is the height of the triangle, point to the nearest point of the segment.
Here is the code, which has been tested in Nod 1298 circle and Triangle .
#include <cmath> #include <queue> #include <vector> #include <cstdio> #include <string># Include <cstring> #include <iomanip> #include <iostream> #include <algorithm>using namespace std;//#pragma COMMENT (linker, "/stack:1024000000,1024000000") #define FIN freopen ("Input.txt", "R", stdin) #define FOUT freopen ("Output.txt", "w", stdout) #define FST first#define snd secondtypedef __int6 4 ll;//typedef Long long ll;typedef unsigned int uint;typedef pair<int, int> pii;const int INF = 0x3f3f3f3f;const D ouble EPS = 1e-6;const int maxn = 3 + 5;const int MAXM = 600 + 5;//Determine the size of the floating-point number with 0 int sgn (double x) {if (Fabs (x) < EP s) return 0; if (x < 0) return-1; else return 1;} struct point {double x, y; Point () {} (double _x, double _y): X (_x), Y (_y) {}} pc, P[maxn];typedef points vect;/** * Two pips distance formula * */double Getdis T (const point &P1, const point &P2) {int tx = p1.x-p2.x; int ty = p1.y-p2.y; return sqrt (TX * TX + ty * ty);} /** * from two points vector * */vect getvect (const point& p1, const point &p2) {return Vect (p2.x-p1.x, p2.y-p1.y);} /** * Find vector cross product * @param v1 [description] * @param v2 [description] * @return [description] */double xmult (const VECT& ; V1, Const vect& v2) {return v1.x * v2.y-v2.x * V1.Y;} /** * Vector cross product area * */double getArea1 (const point &P0, const point &P1, const point &p2) {Vect V1 = getvect (p0 , p1); Vect v2 = Getvect (P0, p2); Return 0.5 * getvectproduct (v1, v2);} /** * Helen formula to find area * */double getArea2 (const point &P0, const point &P1, const point &p2) {Double p0p1 = Getdis T (P0, p1); Double p0p2 = getdist (P0, p2); Double p1p2 = Getdist (P1, p2); Double x = (P0p1 + p0p2 + p1p2)/2.0; return sqrt (x * (X-P0P1) * (X-P0P2) * (X-P1P2));} /** * Use the Helen formula or the cross product formula to find the distance to the line * @param p0 [Point] * @param p1 [point 1 on the line] * @param p2 [point 2 on the line] * @return [point to Line distance] */double PO Int2line (consT-point &p0, const-point-&P1, const-point-&P2) {Double area = getArea1 (P0, p1, p2); Double area = GETAREA2 (P0, p1, p2); Double p1p2 = Getdist (P1, p2); Return 2 * AREA/P1P2;} /** * Gets the minimum distance from the point to the segment * @param p0 [Point] * @param p1 [segment End 1] * @param p2 [segment End 2] * @return [point to segment distance] */double point2lineseg _near (const point &P0, const point &P1, const point &p2) {Double p0p1 = getdist (P0, p1); Double p0p2 = getdist (P0, p2); Double p1p2 = Getdist (P1, p2); Point on the line segment if (SGN (p0p1 + p0p2-p1p2) = = 0) return 0; Segment two Endpoints p1,p2 coincident if (SGN (p1p2) = = 0) return p0p1; ∠P0P1P2 for right angle or obtuse if (P0P2 * p0p2 >= p0p1 * p0p1 + p1p2 * p1p2) return P0P1; ∠p0p2p1 for right angle or obtuse if (P0P1 * p0p1 >= p0p2 * p0p2 + p1p2 * p1p2) return P0P2; ∠P0P1P2 and ∠P0P2P1 are acute angles, equivalent to the distance from the point to the line return Point2line (P0, p1, p2);} /** * Find the longest distance to the segment * @param p0 [Point] * @param p1 [segment End 1] * @param p2 [segment End 2] * @return [longest distance] */double Point2lineseg_far (Const point &P0, const point &P1, const point &p2) {Double p0p1 = getdist (P0, p1); Double p0p2 = getdist (P0, p2); Return Max (P0P1, P0P2);} int t;double R;int Main () {#ifndef Online_judge FIN; #endif//Online_judge scanf ("%d", &t); while (T--) {scanf ("%lf%lf%lf", &pc.x, &pc.y, &r); for (int i = 0; i < 3; i + +) {scanf ("%lf%lf", &p[i].x, &P[I].Y); } double mi[3], ma[3]; Mi[0] = Point2lineseg_near (PC, p[0], p[1]); MI[1] = Point2lineseg_near (PC, p[0], p[2]); MI[2] = Point2lineseg_near (PC, p[1], p[2]); Ma[0] = Point2lineseg_far (PC, p[0], p[1]); MA[1] = Point2lineseg_far (PC, p[0], p[2]); MA[2] = Point2lineseg_far (PC, p[1], p[2]); BOOL suc = false; for (int i = 0; i < 3; i + +) {if (SGN (Mi[i]-R) <= 0 && sgn (ma[i]-R) >= 0) { Suc = true; Break }} puts (suc? "Yes": "NO "); } return 0;}
5. Judging the total line of three points
The method of judging the collinear of three points can be judged by seeking the slope, seeking the circumference and calculating the area.
- to judge by the slope: (ay-by)/(AX-BX) = = (cy-by)/(CX-BX) need to compare the case that the denominator is not 0, and the division accuracy may also be lost.
- through the perimeter to judge: if AC > ab>bc, Judge ac = = AB+BC, the distance will be used when the SQRT, loss of precision.
- Judging by the area : by the above-mentioned cross-product area method to determine the area of 0, proving three points collinear.
Therefore, only need to judge can.
6. Determine if four points are coplanarSimilar to the above method, you canVolumeTo judge. The four points constitute three vectors, and if the three vectors have a mixed product of 0, then the volume is 0, then the four points are coplanar. The code below "includes the three-dimensional space of dot product, cross product, mixed product calculation", the code has been tested in the 51Nod 12,654-point coplanar.
#include <cmath> #include <queue> #include <vector> #include <cstdio> #include <string># Include <cstring> #include <iomanip> #include <iostream> #include <algorithm>using namespace std;//#pragma COMMENT (linker, "/stack:1024000000,1024000000") #define FIN freopen ("Input.txt", "R", stdin) #define FOUT freopen ("Output.txt", "w", stdout) #define FST first#define snd secondtypedef __int6 4 ll;//typedef Long long ll;typedef unsigned int uint;typedef pair<int, int> pii;const int INF = 0x3f3f3f3f;const D ouble EPS = 1e-6;const int maxn = 4 + 5;const int maxm = + 5;int sgn (double x) {if (Fabs (x) < EPS) return 0; if (x < 0) return-1; else return 1; } struct Point {Double x, y, z;}; typedef point Vect;int T; Point pnts[maxn];/** * Three-dimensional space in two points vector * */vect getvect (const point& p1, const point& p2) {Vect v; v.x = p1.x-p2.x; V.y = p1.y-p2.y; V.z = P1.z-p2.Z return v;} /** * The two-vector cross product of three-dimensional space (v1xv2) * */vect xmulti (const vect& v1, Const vect& v2) {Vect V; v.x = v1.y * v2.z-v1.z * V2.Y; V.Y = v1.z * v2.x-v1.x * V2.Z; v.z = v1.x * V2.Y-V1.Y * v2.x; return v;} /** * For two-vector dot product in three-dimensional space (v1 v2) * */double dotmulti (const vect& v1, Const vect& v2) {return v1.x * v2.x + v1.y * V2.Y + v1.z * V2.Z;} /** * To find a mixed product of three vectors in three-dimensional space (V1XV2) · (v3) * */double mixmulti (const vect& v1, const vect& v2, const vect& v3) {return Dotmulti (Xmulti (v1, v2), V3);} /** * Determine if four points are coplanar * Based on the mixed product volume, determine whether the volume is 0 */bool isinarea (const point& p1, const point& p2, const point& P3, const PO int& p4) {Vect p1p2, p1p3, P1P4; P1P2 = Getvect (P1, p2); P1P3 = Getvect (P1, p3); P1P4 = Getvect (P1, p4); Return SGN (Mixmulti (P1P2, p1p3, p1p4) = = 0;} int main () {#ifndef Online_judge FIN, #endif//Online_judge scanf ("%d", &t); while (T--) {for (int i = 0; i < 4; i + +) {scanf ("%lf%LF %lf ", &pnts[i].x, &pnts[i].y, &pnts[i].z); } BOOL ret = Isinarea (Pnts[0], pnts[1], pnts[2], pnts[3]); Puts (ret?) "Yes": "No"); } return 0;}
6. Determine if segments intersectthe code has been tested on the line of Nod 1264.
#include <cmath> #include <queue> #include <vector> #include <cstdio> #include <string># Include <cstring> #include <iomanip> #include <iostream> #include <algorithm>using namespace std;//#pragma COMMENT (linker, "/stack:1024000000,1024000000") #define FIN freopen ("Input.txt", "R", stdin) #define FOUT freopen ("Output.txt", "w", stdout) #define FST first#define snd secondtypedef __int6 4 ll;//typedef Long long ll;typedef unsigned int uint;typedef pair<int, int> pii;const int INF = 0x3f3f3f3f;const D ouble EPS = 1e-6;const int maxn = 4 + 5;const int maxm = + 5;int sgn (double x) {if (Fabs (x) < EPS) return 0; if (x < 0) return-1; else return 1;} struct point {double x, y; Point () {}-(double _x, double _y): X (_x), Y (_y) {}} pnts[maxn];typedef point Vect; Vect getvect (const point& p1, const point &p2) {return Vect (p2.x-p1.x, p2.y-p1.y);} Double Xmulti (CONST vect& v1, const vect& v2) {return v1.x * v2.y-v2.x * V1.Y;} /** * Determine if segments intersect * */bool seginter (const point& p1, const point& p2, const point& P3, const point& p4) { Return Max (p1.x, p2.x) >= min (p3.x, p4.x) && Max (p3.x, p4.x) >= min (p1.x, p2.x) && Max (P1.Y, p2.y) >= min (p3.y, p4.y) && Max (P3.Y, p4.y) >= min (p1.y, p2.y) && SGN (Xmulti (Getvect (P3, p2), Getvect (P1, p2)) * SGN (Xmulti (Getvect, p4), P2 (Getvect, p1)) P2 0 && SGN (Xmulti (Getvect (P1, p4), Getvect (P3, p4)) * SGN (Xmulti (Getvect, p2), P4 (Getvect, p3)) P4 0;} int T;int Main () {#ifndef Online_judge FIN, #endif//Online_judge scanf ("%d", &t); while (T--) {for (int i = 0; i < 4; i + +) {scanf ("%lf%lf", &pnts[i].x, &PNTS[I].Y); } BOOL ret = Seginter (Pnts[0], pnts[1], pnts[2], pnts[3]); Puts (ret?) "Yes": "No"); } return 0;}
Supplemental Links:
- Summary of common Mathematical formulas for ACM: http://blog.csdn.net/qq_26891045/article/details/51490709
- For a given 4 point coordinates, tetrahedron volume: http://blog.csdn.net/foreverlin1204/article/details/6124184
Basic mathematical formulae for general geometry of ACM