问题描述
我有$ C $下圆圈圈交汇。但我需要把它扩展到三维。能否请你帮我写的功能?
I have the code for circle-circle intersection. But I need to expand it to 3-D. Could you please help me to write the functions?
static class Point{
double x, y, z;
int dimension;
Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
dimension = 3;
}
Point sub(Point p2) {
return new Point(x - p2.x, y - p2.y, z - p2.z);
}
Point add(Point p2) {
return new Point(x + p2.x, y + p2.y, z + p2.z);
}
double distance(Point p2) {
return Math.sqrt((x - p2.x)*(x - p2.x) + (y - p2.y)*(y - p2.y) + (z - p2.z)*(z - p2.z));
}
Point normal() {
double length = Math.sqrt(x*x + y*y + z*z);
return new Point(x/length, y/length, z/length);
}
Point scale(double s) {
return new Point(x*s, y*s, z*s);
}
double[] array()
{
return new double[]{x,y,z};
}
}
static class Circle {
double x, y, r, left;
Circle(double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
left = x - r;
}
Circle(double[] c, double r) {
this(c[0], c[1], r);
}
Circle(Point c, double r)
{
this(c.x, c.y, r);
}
Point[] intersections(Circle c)
{
Point P0 = new Point(x, y,0);
Point P1 = new Point(c.x, c.y,0);
double d, a, h;
d = P0.distance(P1);
a = (r*r - c.r*c.r + d*d)/(2*d);
h = Math.sqrt(r*r - a*a);
if(Double.isNaN(h))
return null;
Point P2 = P1.sub(P0).scale(a/d).add(P0);
double x3, y3, x4, y4;
x3 = P2.x + h*(P1.y - P0.y)/d;
y3 = P2.y - h*(P1.x - P0.x)/d;
x4 = P2.x - h*(P1.y - P0.y)/d;
y4 = P2.y + h*(P1.x - P0.x)/d;
return new Point[]{new Point(x3, y3, 0), new Point(x4, y4, 0)};
}
}
static class Sphere
{
double x,y,z,r,left;
Sphere(double x, double y, double z, double r)
{
this.x = x;
this.y = y;
this.z = z;
this.r = r;
left = x-r;
}
Circle intersection(Sphere s)
{
Point P0 = new Point(x, y, z);
Point P1 = new Point(s.x, s.y, s.z);
double d, a, h;
d = P0.distance(P1);
a = (r*r - s.r*s.r + d*d)/(2*d);
h = Math.sqrt(r*r - a*a);
if(Double.isNaN(h))
return null;
Point P2 = P1.sub(P0).scale(a/d).add(P0);
return new Circle(P2, h);
}
Point[] intersections(Circle c)
{
Point P0 = new Point(0,0,0);
Point P1 = new Point(0,0,0);
//...
return new Point[]{P0, P1};
}
}
我已经检查这链接和的链接,但我不明白的逻辑,在他们身后,以及如何code它们。
I have checked this link and this link, but I could not understand the logic behind them and how to code them.
我想ProGAL库球球球交集,但由此产生的坐标四舍五入。我需要precise结果。
I tried ProGAL library for sphere-sphere-sphere intersection, but the resulting coordinates are rounded. I need precise results.
推荐答案
当然,你需要的是如何相交3球?如果是这样,您当前的数据结构圈也没用。让说你要交3球:S1,S2,S3,S1和S2的交集将是一个圆,你将与S3的最终结果相交,但保持介意圆不上XOY平面,它的中心可能是对任何三维坐标和将面临在三维空间中的方向。存储你需要这样的圈信息,的centerX,centerY,centerZ,半径和三维矢量称为正常。
To be sure , what you need is how to intersect 3 spheres?if so , your current data structure for circle is useless.let say you want to intersect 3 spheres : s1 , s2 , s3, intersection of s1 and s2 will be a circle that you will intersect with s3 for final results, BUT keep it mind that the circle is not on XoY plane , it's center may be on any 3D coordinate and it will face a direction in the 3D space. to store information of such a circle you need , centerX , centerY , centerZ , radius and a 3d vector called normal.
class Circle3D
{
Point center;
double r;
Point normal;
...
}
您完成这个新类之后,你可以用它来存储球,球交叉口信息。之后,你必须实现球体Circle3D路口:
after you completed this new class, you can use it to store information about Sphere-Sphere intersection. after that you must implement Sphere-Circle3D intersection:
Circle3D Intersect(Sphere s1 , Sphere s2)
{
double d = dist(s1.center , center)
double x = (d*d + s1.r*s1.r - s2.r*s2.r)/(2*d)
Point normal = normalize(s2.Center - s1.Center)
Point center = s1.center + x*normal;
double radius = sqrt(s1.r*s1.r - x*x)
return new Circle3D(center , radius , normal);
}
如果数学似乎乱不慌,阅读 网页
don't panic if math seems chaotic , read this Page
现在是timee OFR球形circle3D交汇,为:
now it is timee ofr sphere-circle3D intersection, for that:
point[] Intersect(Sphere s , Circle3D c)
{
/*
first we check if sphere even intersects with plane of c
I assume you know how to implement some if these functions
*/
if(GetDistanceOfPointFromPlane(s.center , new Plane(c.center , normal))>s.radius)
return NULL;
/*
again we check possibility of avoiding math of intersection
*/
point dir = Normalize(s.center - c.center);
if(!DoesRayIntersectWithSphere(s , new Ray(c.center , c.radius*dir)))
return NULL;
/*
this is the ugly part of code that unfortunately is deep trig math.
you must describe sphere and circle equations in polar system , then
you must solve sphere = circle
I hope the link below be helpful
*/
}
下面是上述
http://mathworld.wolfram.com/SphericalCoordinates.html 一>
这篇关于球,球路口圈 - 圈交汇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!