java - 射线和球体的交点 - 求解一个二次方程
问题描述
我不知道如何求解我的二次方程来检查射线和球体之间的交点。球体应如下所示:
// r = radius = sphere.radius;
// m = center = sphere.center;
float t;
// ray intersection uses quadratic equation
float a, b, c, d;
// NOT SURE ABOUT THIS PART HERE !
a = rayVx * rayVx + rayVy * rayVy + rayVz * rayVz;
b = 2.0f * (rayEx * rayVx + rayEy * rayVy + rayEz * rayVz - rayVx * sphere.center[0] - rayVy * sphere.center[1] - rayVz * sphere.center[2]);
c = rayEx * rayEx - 2 * rayEx * sphere.center[0] + sphere.center[0] * sphere.center[0] + rayEy * rayEy - 2 * rayEy * sphere.center[1] + sphere.center[1] * sphere.center[1] + rayEz * rayEz - 2 * rayEz * sphere.center[2] + sphere.center[2] * sphere.center[2] - sphere.radius * sphere.radius;
// positive discriminant determines intersection
//d = -42;
d = (float) Math.pow(b, 2) - (4 * a * c);
// no intersection point? => next object
if (d <= 0)
continue;
// from here: intersection takes place!
}
}
// no intersection point found => return with no result
if (minObjectsIndex == -1)
return null;
// intermediate version
Random rd = new Random();
return new Color(rd.nextFloat(), rd.nextFloat(), rd.nextFloat());
}
public static void main(String[] args) {
Raytracer00 rt = new Raytracer00();
rt.doRayTrace();
}
}
我现在一无所知。我真的不明白如何求解a,b,c的二次方程。如果有人想查看完整的项目,我在workupload上上传了整个项目。
我来自 iOS 世界,之前从未在 Java 中做过任何事情(遗憾的是)。如果有人可以帮助我,我将不胜感激!提前致谢。
解决方案
推荐阅读
- html - 如何使用闪亮的应用程序将 pdf 文件嵌入到数据表中?
- mysql - 插入 Base64Encoded 字符串返回不正确的字符串值(MySQL)
- java - Spring Boot: spring.profiles.active=dev/test/prod
- java - java.lang.IllegalArgumentException:用户名不能为空或空,AbstractXMPPConnection.java:484
- docker - 如何通过 docker RUN 运行复制命令
- c++ - 将浮点数转换为字符串时如何加速此代码?
- android - 如何在运行时直接(无动画)设置 FAB 图标旋转和颜色以及 FAB 背景?
- php - 带有 laravel 项目的 Css 不起作用(布局使用)
- javascript - Lodash 从选定的键创建新数组
- html - HTML:将表格中的两列设置为比其他列宽固定像素数