将代码从使用双打转换为bigdecimal
我一直在写一个CAD类型程序,以供爪哇娱乐。前几天,我编写了一些代码来定义与2个圆圈相切的线路。我一直在使用商业CAD计划检查我的电话号码,它们相当接近。通常到第9点。我的结果实际上只需要存储在一个小数点的阵列中。在成功定义了与两个圆圈切线的线后,我决定对其进行测试并定义一个点,即线的相交和一个圆圈。 在一种情况下,我得到了我正在寻找的结果,在另一种情况下,我没有交叉点。在查看了一些计算之后,我意识到我的变化很小,大概是9或十进制位置。我正在考虑使用BigDecimal重写代码。
这是我需要重写的一些代码的小片段。一旦我开始,它就变得比我想做的要繁琐。我正在考虑仅使用BigDecimal并使用原始代码转换结果,除非有一种简单的方法将以下代码转换为GimDecimal类型的格式。
private float[] offsetLine(float lnx1, float lny1, float lnz1, float lnx2, float lny2, float lnz2, String direction, float offset) {
double deltax = Math.abs(lnx2 - lnx1);
double deltay = Math.abs(lny2 - lny1);
double lineLength = Math.sqrt(deltax * deltax + deltay * deltay);
double stepx = (offset * deltay) / lineLength;
double stepy = (offset * deltax) / lineLength;
I've been writing a CAD type program for fun in JAVA. The other day I wrote some code to define a line which was tangent to 2 circles. I've been checking my numbers with a commercial CAD program and they have been fairly close. Usually to the 9th decimal point. My results really only need to be stored in an array to 7 decimal points. After successfully defining the line tangent to the 2 circles, I decided to test it and define a point which was the intersection of the line and one of the circles.
In one case I got the result I was looking for, in another case I got no intersection. After looking at a few of the calculations I realized I was getting a very very small variation of maybe 9 or ten decimal places. I'm thinking of rewriting the code using BigDecimal.
This is a small snippet of some of the code I need to rewrite. Once I started it became much more cumbersome than I was wanting to do. I'm thinking about just converting the results using BigDecimal and using the original code unless there is an easy way to convert the following code to a BigDecimal type of format.
private float[] offsetLine(float lnx1, float lny1, float lnz1, float lnx2, float lny2, float lnz2, String direction, float offset) {
double deltax = Math.abs(lnx2 - lnx1);
double deltay = Math.abs(lny2 - lny1);
double lineLength = Math.sqrt(deltax * deltax + deltay * deltay);
double stepx = (offset * deltay) / lineLength;
double stepy = (offset * deltax) / lineLength;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我会回答自己的问题。这是我挖出的一些代码。我只能到达6个小数点的位置,以获取想要的舍入。一旦我以双重值进行计算,我称之为subroutine rounddbl
Ok I'll answer my own question. Here's some code I dug up. I could only round to 6 decimal places to get the rounding I wanted. Once I did my calculations in double values I called the subroutine roundDbl