C ++ implements Common geometric problem solving and Geometric Problem Solving for plane Computation
By encapsulating common point and line segment types, and providing calculation of the relationship between points and lines, this provides a basic framework for compiling the ry tool library.
The Code is as follows: (the Code correctness still needs to be tested and used with caution)
// Reference // http://dev.gameres.com/Program/Abstract/Geometry.htm//http://zhan.renren.com/jisuanjihe? From = template & checked = true/* toolbox: Geometry algorithm toolboxauthor: alaclpemail: alaclp@qq.compublish date: */# include <iostream> # include <stdio. h> # include <math. h> using namespace std; // predefined # define Min (x, y) (x) <(y )? (X): (y) # define Max (x, y) (x)> (y )? (X): (y) // Point Object typedef struct Point {double x, y; // constructor Point (double x, double y): x (x ), y (y) {}// Point (): x (0), y (0) When no parameter is set) {} // obtain the distance from the pt Point to double distance (const Point & pt) {return sqrt (x-pt. x) * (x-pt. x) + (y-pt. y) * (y-pt. y);} // determine whether two points are at the same Point bool equal (const Point & pt) {return (x-pt. x) = 0) & (y-pt. y = 0) ;}} Point; // line segment object typedef struct PartLine {Point pa, pb; double length; PartLine () {Length = 0;} // constructor PartLine (Point pa, Point pb): pa (pa), pb (pb) {length = sqrt (pa. x-pb. x) * (pa. x-pb. x) + (pa. y-pb. y) * (pa. y-pb. y);} void assign (const PartLine & pl) {pa = pl. pa; pb = pl. pb; length = pl. length;} // calculate the vertical distance from a point to a line segment using the cross product. // note: the distance between the result is positive and negative. // If the pc point is in the counterclockwise direction, the distance is positive; otherwise, the distance is the sub-value double getDistantToPoint (Point pc) {double area = crossProd (pc)/2; return area * 2/length;/* use the Helen formula to calculate PartLine pl1 (This-> pa, pc), pl2 (this-> pb, pc); double l1 = this-> length, l2 = pl1.length, l3 = pl2.length; double s = (l1 + l2 + l3)/2; // Helen formula double area = sqrt (s * (s-l1) * (s-l2) * (s-l3); return area * 2/l1; * // cross product of the vector/* Cross Product of the calculated vector (ABxAC A (x1, y1) B (x2, y2) C (x3, y3) is the result of calculating the deciding factor | x1-x0 y1-y0 | x2-x0 y2-y0 |) * // calculate the cross product of AB and AC --- the absolute value of the cross product is the area of the parallelogram formed by the two vectors double crossProd (Point & pc) {// calculate AB X acret Urn (pb. x-pa. x) * (pc. y-pa. y)-(pb. y-pa. y) * (pc. x-pa. x) ;}// determine whether the two line segments overlap bool isIntersected (PartLine & pl) {double d1, d2, d3, d4, d5, d6; d1 = pl. crossProd (pb); d2 = pl. crossProd (pa); d3 = crossProd (pl. pa); d4 = crossProd (pl. pb); d5 = crossProd (pl. pa); d6 = crossProd (pl. pb); // printf ("% f \ n", d1, d2, d3, d4, d5, d6 ); bool cond1 = d1 * d2 <= 0, // cond2 = d3 * d4 for pb and pa on both sides of pl or on the extension line of a line or line segment <= 0, // pl. pa and pl. pb are on either side of this or the extension line of a line or line segment. cond3 = d5! = 0, // pl. pa is not on the line segment and extension line. cond4 = d6! = 0; // pl. pb does not return cond1 & cond2 & cond3 & cond4;} // determines whether the two parts are parallel to bool isParallel (PartLine & pl) {double v1 = crossProd (pl. pa), v2 = crossProd (pl. pb); return (v1 = v2) & (v1! = 0);} // rotate thetaPartLine rotateA (double theta) {float nx = pa. x + (pb. x-pa. x) * cos (theta)-(pb. y-pa. y) * sin (theta), ny = pa. y + (pb. x-pa. x) * sin (theta) + (pb. y-pa. y) * cos (theta); return PartLine (pa, Point (nx, ny);} // rotate thetaPartLine rotateB (double theta) {float nx = pb. x + (pa. x-pb. x) * cos (theta)-(pa. y-pb. y) * sin (theta), ny = pb. y + (pa. x-pb. x) * sin (theta) + (pa. y-pb. y) * Cos (theta); return PartLine (Point (nx, ny), pb);} // determine whether two line segments overlap or are collocated bool inSameLine (PartLine & pl) {double v1 = crossProd (pl. pa), v2 = crossProd (pl. pb); if (v1! = V2) return false; if (v1! = 0) return false; return true;} // get the intersection of two line segments --- if there is no intersection, return valid = false // If there are multiple intersections, warning Point getCrossPoint (PartLine & pl, bool & valid) {valid = false; if (! IsIntersected (pl) {// returns Point ();} if (inSameLine (pl) {// returns if (pa. equal (pl. pa) {valid = true; return pa;} if (pa. equal (pl. pb) {valid = true; return pa;} if (pb. equal (pl. pa) {valid = true; return pb;} if (pb. equal (pl. pb) {valid = true; return pb;} // multiple focus cout <"error: the number of intersection result calculation results is infinite" <endl; valid = false; return Point ();} // Intersection Point pt1, pt2, pt3, result; pt1 = pa; pt2 = Pb; pt3.x = (pt1.x + pt2.x)/2; pt3.y = (pt1.y + pt2.y)/2; double L1 = pl. crossProd (pt1), L2 = pl. crossProd (pt2), L3 = pl. crossProd (pt3); printf ("% f = % f \ n", L1, L2, L3, L1 + L2); while (fabs (L1)> 1e-7 | fabs (L2)> 1e-7) {valid = true; if (fabs (L1) <fabs (L2) pt2 = pt3; else pt1 = pt3; pt3.x = (pt1.x + pt2.x)/2; pt3.y = (pt1.y + pt2.y)/2; result = pt3; L1 = pl. crossProd (pt1), L2 = pl. crossProd (Pt2), L3 = pl. crossProd (pt3); printf ("% f = % f \ n", L1, L2, L3, L1-L2);} return pt3 ;} // obtain the Point getNearestPointToPoint (Point & pt) {Point pt1, pt2, pt3, result; pt1 = pa; pt2 = pb on the line segment; pt3.x = (pt1.x + pt2.x)/2; pt3.y = (pt1.y + pt2.y)/2; double L1 = pt1.distance (pt), L2 = pt2.distance (pt ), l3 = pt3.distance (pt); if (L1 = L2) return pt3; while (fabs (L1-L2)> 1e-7) {if (L1 <L2) pt2 = pt3; els E pt1 = pt3; pt3.x = (pt1.x + pt2.x)/2; pt3.y = (pt1.y + pt2.y)/2; result = pt3; L1 = pt1.distance (pt ); l2 = pt2.distance (pt); L3 = pt3.distance (pt); // printf ("% f = % f \ n", L1, L2, L3, l1-L2) ;}return result ;}// obtain the image Point getaskpoint (Point & pc) {}} PartLine on the Point online segment; int main (void) {Point p1 (0, 0), p2 (1, 1), p3 (0, 1.1), p4 (0.5, 0.5 + 1e-10), p5 (0.5, 0.5-1e-10), np; PartLine pl1 (p1, p2), pl2 (p3, p 4), pl3 (p3, p5); cout <pl1.getDistantToPoint (p3) <endl; cout <"intersection of line segments 1 and 2? "<Pl1.isIntersected (pl2) <endl; np = pl1.getNearestPointToPoint (p5); cout <" closest point: "<np. x <"," <np. y <endl; bool isvalid; np = pl1.getCrossPoint (pl3, isvalid); cout <"intersection of two line segments:" <(isvalid? "Valid": "invalid") <"=" <np. x <"," <np. y <endl; PartLine plx = pl1.rotateA (M_PI/2); printf ("after 90 degrees rotation: % f \ n", plx. pa. x, plx. pa. y, plx. pb. x, plx. pb. y); return 0 ;}