找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 565|回复: 1

[分享] 空间两直线最近点对

[复制链接]

已领礼包: 40个

财富等级: 招财进宝

发表于 2017-1-21 01:38:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
//#pragma comment(linker, "/STACK:1024000000,1024000000")  
#include<algorithm>  
#include<iostream>  
#include<cstring>  
#include<fstream>  
#include<sstream>  
#include<vector>  
#include<string>  
#include<cstdio>  
#include<bitset>  
#include<queue>  
#include<stack>  
#include<cmath>  
#include<map>  
#include<set>  
#define FF(i, a, b) for(int i=a; i<b; i++)  
#define FD(i, a, b) for(int i=a; i>=b; i--)  
#define REP(i, n) for(int i=0; i<n; i++)  
#define CLR(a, b) memset(a, b, sizeof(a))  
#define debug puts("**debug**")  
#define LL long long  
#define PB push_back  
#define MP make_pair  
#define eps 1e-10  
using namespace std;  

struct Point  
{  
    double x, y, z;  
    Point(double x=0, double y=0, double z=0) : x(x), y(y),z(z){}  
};  
typedef Point Vector;  

Vector operator + (Vector a, Vector b) { return Vector(a.x+b.x, a.y+b.y, a.z+b.z); };  
Vector operator - (Vector a, Vector b) { return Vector(a.x-b.x, a.y-b.y, a.z-b.z); };  
Vector operator * (Vector a, double p) { return Vector(a.x*p, a.y*p, a.z*p); }  
Vector operator / (Vector a, double p) { return Vector(a.x/p, a.y/p, a.z/p); }  

double Dot(Vector a, Vector b) { return a.x*b.x + a.y*b.y + a.z*b.z; }  
double Length(Vector a) { return sqrt(Dot(a, a)); }  
Vector Cross(Point a, Point b)  
{  
    return Vector(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);  
}  

Point a1, b1, a2, b2;  
int main()  
{  
    int n;  
    scanf("%d", &n);  
    while(n--)  
    {  
        scanf("%lf%lf%lf", &a1.x, &a1.y, &a1.z);  
        scanf("%lf%lf%lf", &b1.x, &b1.y, &b1.z);  
        scanf("%lf%lf%lf", &a2.x, &a2.y, &a2.z);  
        scanf("%lf%lf%lf", &b2.x, &b2.y, &b2.z);  
        Vector v1 = (a1-b1), v2 = (a2-b2);  
        Vector N = Cross(v1, v2);  
        Vector ab = (a1-a2);  
        double ans = Dot(N, ab) / Length(N);  
        Point p1 = a1, p2 = a2;  
        Vector d1 = b1-a1, d2 = b2-a2;  
        Point ans1, ans2;  
        double t1, t2;  
        t1 = Dot((Cross(p2-p1, d2)), Cross(d1, d2));  
        t2 = Dot((Cross(p2-p1, d1)), Cross(d1, d2));  
        double dd = Length((Cross(d1, d2)));  
        t1 /= dd*dd;  
        t2 /= dd*dd;  
        ans1 = (a1 + (b1-a1)*t1);  
        ans2 = (a2 + (b2-a2)*t2);  
        printf("%.6f\n", fabs(ans));  
        printf("%.6f %.6f %.6f ", ans1.x, ans1.y, ans1.z);  
        printf("%.6f %.6f %.6f\n", ans2.x, ans2.y, ans2.z);  
    }  
    return 0;  
}  


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 4751个

财富等级: 富可敌国

发表于 2017-1-22 13:57:57 | 显示全部楼层
啥也不说了,感谢楼主分享哇!
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-6-8 09:59 , Processed in 0.160746 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表