Find the closest distance between two straight lines in space and the coordinates of the nearest point (C + +)

Source: Internet
Author: User

Keywords: space geometry

Use: There will always be a place to use it.

Article type: C + + function display

@Author: V_shawn

@Date: 2016-11-19

@Lab: [Email protected]

For two straight lines within a space A, I assume they must not be parallel or intersect. If it is known that a on two points A1, A2,b on the two points B1, B2, then it is easy to get two straight lines of the equation.

The function can then be called to solve the result:

The following is a class for solving this problem:
#include <math.h>//is used to solve the closest distance of two space lines, and their nearest two-point coordinate//author @vshawn//url:http://www.cnblogs.com/singlex/p/ 6091659.html//date:2016-11-22class getdistanceof2linesin3d{public://Enter two points of line A to get the equation of a void Setlinea (double A1x, Double a1y, double a1z, double a2x, double a2y, double a2z) {a1_x = A1x;a1_y = A1y;a1_z = a1z;a2_x = a2x;a2_y = A2y;a2_z = A2Z;} Enter two points of line B to get the equation of B void Setlineb (double b1x, double b1y, double b1z, double b2x, double b2y, double b2z) {b1_x = B1x;b1_y = B1y;b1_z = b1z;b2_x = b2x;b2_y = B2y;b2_z = b2z;} After inputting A and B equations with Setlinea, SETLINEB//Call this function to solve the result void Getdistance () {///method from: http://blog.csdn.net/pi9nc/article/details/ 11820545double d1_x = a2_x-a1_x;double d1_y = a2_y-a1_y;double D1_z = a2_z-a1_z;double d2_x = b2_x-b1_x;double d2_ y = b2_y-b1_y;double D2_z = b2_z-b1_z;double e_x = b1_x-a1_x;double e_y = b1_y-a1_y;double e_z = B1_z-a1_z;doubl E cross_e_d2_x, Cross_e_d2_y, Cross_e_d2_z;cross (e_x, e_y, E_z, d2_x, d2_y, D2_z, cross_e_d2_x, cross_e_d2_y, cross_e_d2_ Z);d OublE cross_e_d1_x, Cross_e_d1_y, Cross_e_d1_z;cross (e_x, e_y, E_z, d1_x, d1_y, D1_z, cross_e_d1_x, cross_e_d1_y, cross_e_d1_ z);d ouble cross_d1_d2_x, cross_d1_d2_y, Cross_d1_d2_z;cross (d1_x, d1_y, D1_z, d2_x, d2_y, D2_z, cross_d1_d2_x, cross_d1_ D2_y, cross_d1_d2_z);d ouble t1, t2;t1 = dot (cross_e_d2_x, cross_e_d2_y, Cross_e_d2_z, cross_d1_d2_x, Cross_d1_d2_y, CROSS_D1_D2_Z); t2 = dot (cross_e_d1_x, cross_e_d1_y, Cross_e_d1_z, cross_d1_d2_x, cross_d1_d2_y, cross_d1_d2_z);d ouble DD = Norm (cross_d1_d2_x, cross_d1_d2_y, cross_d1_d2_z); T1/= dd*dd;t2/= dd*dd;//get nearest position pona_x = (a1_x + (a2_x-a1_x) *t1) ; Pona_y = (a1_y + (a2_y-a1_y) *t1); Pona_z = (a1_z + (a2_z-a1_z) *t1); Ponb_x = (b1_x + (b2_x-b1_x) *t2); Ponb_y = (b1_y + (b2_y-b1_y) *t2); Ponb_z = (b1_z + (b2_z-b1_z) *t2);d istance = Norm (ponb_x-pona_x, ponb_y-pona_y, ponb_z-pona_z);} Double pona_x;//two The x-coordinate of the point on the a line of the nearest point of the line, double pona_y;//two, the y-coordinate of the point on line a of the nearest point, double pona_z;//two, the z-coordinate of the point on the a line of the nearest point, double ponb_x;// The x-coordinate of the point on the B-line of the nearest point of the two lines double ponb_y;//two the line of the nearest point on the B line of the Y-seatThe z-coordinate of the point on the B line of the ponb_z;//two line nearest point double distance;//two straight line distance private://The first point of the line a double a1_x;d ouble a1_y;d ouble a1_z;// The second point of the line a double a2_x;d ouble a2_y;d ouble a2_z;//The first point of a straight line B double b1_x;double b1_y;d ouble b1_z;//second point of line B double B2_x;doubl E b2_y;d ouble b2_z;//point multiplication double dot (double ax, double ay, double az, double bx, double by, double BZ) {return ax*bx + ay* by + Az*bz; }//Vector Fork Gets the normal vector, the last three parameters are output parameters void Cross (double ax, double ay, double az, double bx, double by, double BZ, double& x, double & y, double& z) {x = Ay*bz-az*by;y = Az*bx-ax*bz;z = AX*BY-AY*BX;} Vector modulo double norm (double ax, double ay, double az) {return sqrt (dot (ax, ay, AZ, ax, ay, AZ));};
How to use:
Getdistanceof2linesin3d g;//Initialize G. Setlinea (a1.x, A1.y, A1.z, a2.x, A2.y, a2.z);//Enter two point coordinates on line a g.setlineb (b1.x, B1.y, B1.z, b2.x, B2.y, b2.z);// Enter the two point coordinates on line B g.getdistance ();//calculate distance double d = g.distance;//get distance Double x = g.pona_x;//Obtain the x-coordinate of the point on line a in the nearest two points between AB and double y = g. pona_y;//get the y-coordinate of the point on line a in the nearest two point of the AB double z = g.pona_z;//to get the z-coordinate of the point on line a in the nearest two points between AB

 

Download:

Https://1drv.ms/u/s!AmjBsmo7u4eFmDpYL9FSBJurEQQ7

Find the closest distance between two straight lines in space and the coordinates of the nearest point (C + +)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.