Test instructions: Finding the shortest distance between two lines of space and the intersection of the shortest line
Exercises
Linear algebra and spatial geometry, mainly with cross product, dot product, geometry.
Knowing that two direction vectors are s1,s2, the cross product can derive their common vertical vectors, and then the common vertical vector gamma and the dots on the two lines form a vector to do the inner product,
When the gamma is removed the length is projected, which is the shortest distance.
Then two points can be used to describe a plane with the cross product of gamma and S2 and a point on the L2, and then to find the intersection of plane and line,
Divides (P2-P1) *n and (P0-P1) *n divide calculates the proportion multiply on P2-P1 obtains the intersection point and the P1 difference, together with the P1 to find the intersection
Learning points: Some things to calculate geometry
#include <cstdio>#include<cmath>inlineDoubleFunDoubleADoubleBDoubleCDoubled) { returna*d-b*C;}#defineSqu (x) ((x) * (x))structpoi{Doublex, Y, Z Poi (DoubleX =0,DoubleY =0,DoubleZ =0) {x= X; y = y; z =Z; } voidinput () {scanf ("%LF%LF%LF",&x,&y,&z); } Poioperator+ (ConstPoi &RHS) { returnPoi (x+rhs.x,y+rhs.y,z+rhs.z); } Poioperator-(Poi &RHS) { returnPoi (x-rhs.x,y-rhs.y,z-rhs.z); } Poioperator^ (Poi &RHS) { returnPoi (Y,Z,RHS.Y,RHS.Z),-Fun (x,z,rhs.x,rhs.z), Fun (X,Y,RHS.X,RHS.Y)); } Poioperator*(Doublet) { returnPoi (x*t,y*t,z*t); } Double operator*(ConstPoi &RHS) { returnx*rhs.x+y*rhs.y+z*rhs.z; }};typedef Poi Vector;DoubleDot (ConstVector & A,Constpoi&b) {returna.x*b.x+a.y*b.y+a.z*b.z;} Poi Lineplaneins (Poi&p1,poi &p2,poi &p0,vector &N) {Vector v= P2-P1; DoubleRatio = (dot (n,p0-p1))/(dot (n,v));//Guaranteed Intersection returnp1+v*Ratio;}DoubleLength (ConstVector &x) { returnsqrt (squ (x.x) +squ (x.y) +squ (X.Z));}intMain () {//freopen ("In.txt", "R", stdin); intT; scanf ("%d",&T); while(t--) {Poi p1,p2,p3,p4; P1.input (); P2.input (); P3.input (); P4.input (); Vector Alpha= P2-P1; Vector Beta= P4-P3; Vector Gamma= alpha^Beta; DoubleDistance = Fabs (Dot (Gamma, (P1-P3))/Length (gamma)); Vector N1= alpha^Gamma; Vector N2= beta^Gamma; Poi ins1=Lineplaneins (P1,P2,P3,N2); Poi Ins2=Lineplaneins (P3,P4,P1,N1); printf ("%.6lf\n%.6lf%.6lf%.6lf%.6lf%.6lf%.6lf\n", distance,ins1.x,ins1.y,ins1.z,ins2.x,ins2.y,ins2.z); } return 0;}
HDU 4741 Save Labman no.004