关于数据位数出现的问题?
已知两个点:(tx1,ty1),(tx,ty)画一直线,
还有一个以点(tx0,ty0)为圆心,r为半径的圆,要求得到圆与折线相交的第一个点!
现在知道这两个点到圆心的距离,一个大于r,一个小于r。(保证直线与圆有两个交点)
现在编程序运行,一般的点值都能运算出来,基本正确,
只是当计算时出现double值位数错误,甚至有b2-4ac小于0的情况(这是不可能的,因为两点到圆心距离是一长一短!),知道是运算时x0和x1太接近,做分母产生巨大数据,引起的计算失误,有哪位大侠给分析一下,如何解决?
重要的是:还有就是b2-4ac虽然大于0,但是得到的(x0,y0),(x1,y1)不在(tx1,ty1),(tx,ty)范围之内(这是不可能的, 因为两点到圆心距离是一长一短!),这种情况是由于位数不够引起的,如何调整位数?我的tx,ty,tx1,ty1,tx0,ty0都是大地坐标,整数位 为六到七位。
程序代码:
public const int weishu = 1000000; public const int bushu = 100000; public static int jiadian = 0; NumberFormatInfo thisnumberformatinfo = new CultureInfo("en-US", false).NumberFormat; thisnumberformatinfo.NumberDecimalDigits = 90; double tx = 0,ty = 0,tx0 = 0,ty0 = 0,tx1 = 0,ty1 = 0; double px = 0,py = 0,px0 = 0,py0 = 0,px1 = 0,py1 = 0; int t=0; int bianweishu = weishu; for (int i1 = 0; i1 < numpoint1; i1++) { tx = 0; ty = 0; BinaryFile1.BaseStream.Seek(152 + gg + numpart1 * 4 + i1 * 16, 0);(提取tx) tx = Convert.ToDouble(BinaryFile1.ReadDouble()); tx = tx * bianweishu; BinaryFile1.BaseStream.Seek(152 + gg + numpart1 * 4 + i1 * 16 + 8, 0);(提取ty) ty = Convert.ToDouble(BinaryFile1.ReadDouble()); ty = ty * bianweishu; if (i1 == 0 && t == 0) { tx0 = tx; ty0 = ty; tx1 = tx; ty1 = ty; px1 = tx; py1 = ty; px0 = tx; py0 = ty; } else { double changdu = Math.Sqrt((tx - tx0) * (tx - tx0) + (ty - ty0) * (ty - ty0)); if (changdu < banjing) { tx1 = tx; ty1 = ty; if (i1 == (numpoint1 - 1) && (t == 0)) { MessageBox.Show("没有交点,不创建文件!"); System.Environment.Exit(0); } } else { double aa = (tx - tx1) * (tx - tx1) + (ty - ty1) * (ty - ty1); if (aa != 0) { double b = 0; double c = 0; double x0 = 0; double y0 = 0; double x1 = 0; double y1 = 0; double s = 0; if (tx == tx1) { x0 = tx; y0 = ty + banjing; x1 = tx; y1 = ty - banjing; } else { double a = 0; a = 1 + (ty - ty1) * (ty - ty1) / ((tx - tx1) * (tx - tx1)); b = (ty - ty1) / (tx - tx1) * ((ty1 * tx - ty * tx1) / (tx - tx1) - ty0) - tx0; double d = ((ty1 * tx - ty * tx1) / (tx - tx1) - ty0); double dd = d * d; c = tx0 * tx0 - banjing * banjing + dd; double bb = b * b; double ac = a * c; s = b * b - a * c; //s = (2 * tx0 * (ty - ty1) * (ty0 * tx - ty0 * tx1 - ty1 * tx + ty * tx1) // + (ty1 * tx - ty * tx1) * (2 * tx * ty0 - 2 * tx1 * ty0 - ty1 * tx + ty * tx1) // - tx0 * tx0 * (ty - ty1) * (ty - ty1) - ty0 * ty0 * (tx - tx1) * (tx - tx1) // + banjing * banjing * ((ty - ty1) * (ty - ty1) + (tx - tx1) * (tx - tx1))) / ((tx - tx1) * (tx - tx1)); if (s > 0) { x0 = (-b + Math.Sqrt(s)) / a; y0 = (ty - ty1) * (x0 - tx1) / (tx - tx1) + ty1; x1 = (-b - Math.Sqrt(s)) / a; y1 = (ty - ty1) * (x1 - tx1) / (tx - tx1) + ty1; } else { ///////?????????//// } } double maxx = 0, maxy = 0, minx = 0, miny = 0; if (tx > tx1) { maxx = tx; minx = tx1; } else { maxx = tx1; minx = tx; } if (ty > ty1) { maxy = ty; miny = ty1; } else { maxy = ty1; miny = ty; } px1 = px0; py1 = py0; if (t != 0) { px0 = px; py0 = py; } double julix0 = (px1 - x0) * (px1 - x0) + (py1 - y0) * (py1 - y0); double julix1 = (px1 - x1) * (px1 - x1) + (py1 - y1) * (py1 - y1); if (julix0 > julix1) { if (((x0 >= minx) && (x0 <= maxx)) && ((y0 >= miny) && (y0 <= maxy))) { px = x0; py = y0; } else { if (((x1 >= minx) && (x1 <= maxx)) && ((y1 >= miny) && (y1 <= maxy))) { px = x1; py = y1; } else { MessageBox.Show("错误!"); System.Environment.Exit(0); } } } else { if (((x1 >= minx) && (x1 <= maxx)) && ((y1 >= miny) && (y1 <= maxy))) { px = x1; py = y1; } else { if (((x0 >= minx) && (x0 <= maxx)) && ((y0 >= miny) && (y0 <= maxy))) { px = x0; py = y0; } else { MessageBox.Show("错误!"); System.Environment.Exit(0); } } } t = t + 1; tx0 = px; ty0 = py; i1 = i1 - 1; } } } } }