📄 genmath.java
字号:
break; case 4: switch (q2) { case 1: // 3 quadrants clockwise from Q4 to Q1 hx = Math.max(x1,x2) + c.getX(); break; case 2: // 2 quadrants clockwise from Q4 to Q2 hx = x1 + c.getX(); hy = y2 + c.getY(); break; case 3: // 1 quadrant clockwise from Q4 to Q3 lx = x2 + c.getX(); hx = x1 + c.getX(); hy = Math.max(y1,y2) + c.getY(); break; } break; } return new Rectangle2D.Double(lx, ly, hx-lx, hy-ly); } /** * compute the quadrant of the point x,y 2 | 1 * Standard quadrants are used: ----- * 3 | 4 */ private static int db_quadrant(double x, double y) { if (x > 0) { if (y >= 0) return 1; return 4; } if (y > 0) return 2; return 3; } /** * Method to find the two possible centers for a circle whose radius is * "r" and has two points (x01,y01) and (x02,y02) on the edge. The two * center points are returned in (x1,y1) and (x2,y2). The distance between * the points (x01,y01) and (x02,y02) is in "d". The routine returns * false if successful, true if there is no intersection. This code * was written by John Mohammed of Schlumberger. */ public static Point2D [] findCenters(double r, double x01, double y01, double x02, double y02, double d) { /* quit now if the circles concentric */ if (x01 == x02 && y01 == y02) return null; /* find the intersections, if any */ double r2 = r * r; double delta_1 = -d / 2.0; double delta_12 = delta_1 * delta_1; /* quit if there are no intersections */ if (r2 < delta_12) return null; /* compute the intersection points */ double delta_2 = Math.sqrt(r2 - delta_12); Point2D [] points = new Point2D[2]; double x1 = x02 + ((delta_1 * (x02 - x01)) + (delta_2 * (y02 - y01))) / d; double y1 = y02 + ((delta_1 * (y02 - y01)) + (delta_2 * (x01 - x02))) / d; double x2 = x02 + ((delta_1 * (x02 - x01)) + (delta_2 * (y01 - y02))) / d; double y2 = y02 + ((delta_1 * (y02 - y01)) + (delta_2 * (x02 - x01))) / d; points[0] = new Point2D.Double(x1, y1); points[1] = new Point2D.Double(x2, y2); return points; } /** * Small epsilon value. * set so that 1+DBL_EPSILON != 1 */ private static double DBL_EPSILON = 2.2204460492503131e-016; /** * Method to round a value to the nearest increment. * @param a the value to round. * @param nearest the increment to which it should be rounded. * @return the value, rounded to the nearest increment. * For example:<BR> * toNearest(10.3, 1.0) = 10.0<BR> * toNearest(10.3, 0.1) = 10.3<BR> * toNearest(10.3, 0.5) = 10.5 */ public static double toNearest(double a, double nearest) { long v = Math.round(a / nearest); return v * nearest; } /** * Method to compare two double-precision numbers within an acceptable epsilon. * @param a the first number. * @param b the second number. * @return true if the numbers are equal to 16 decimal places. */ public static boolean doublesEqual(double a, double b) { return doublesEqual(a, b, DBL_EPSILON); } /** * Method to compare two double-precision numbers within a given epsilon * @param a the first number. * @param b the second number. * @param myEpsilon the given epsilon * @return true if the values are close. */ public static boolean doublesEqual(double a, double b, double myEpsilon) { return (Math.abs(a-b) <= myEpsilon); } /** * Method to compare two numbers and see if one is less than the other within an acceptable epsilon. * @param a the first number. * @param b the second number. * @return true if "a" is less than "b" to 16 decimal places. */ public static boolean doublesLessThan(double a, double b) { if (a+DBL_EPSILON < b) return true; return false; } /** * Method to compare two double-precision numbers within an approximate epsilon. * <p>NOTE: If you are comparing Electric database units, DO NOT * use this method. Use the corresponding method from DBMath. * @param a the first number. * @param b the second number. * @return true if the numbers are approximately equal (to a few decimal places). */ public static boolean doublesClose(double a, double b) { if (b != 0) { double ratio = a / b; if (ratio < 1.00001 && ratio > 0.99999) return true; } if (Math.abs(a-b) < 0.001) return true; return false; }// /**// * Method to round floating-point values to sensible quantities.// * Rounds these numbers to the nearest thousandth.// * @param a the value to round.// * @return the rounded value.// */// public static double smooth(double a)// {// long i = Math.round(a * 1000.0);// return i / 1000.0;// } // ************************************* CLIPPING ************************************* private static final int LEFT = 1; private static final int RIGHT = 2; private static final int BOTTOM = 4; private static final int TOP = 8; /** * Method to clip a line against a rectangle (in double-precision). * @param from one end of the line. * @param to the other end of the line. * @param lX the low X bound of the clip. * @param hX the high X bound of the clip. * @param lY the low Y bound of the clip. * @param hY the high Y bound of the clip. * The points are modified to fit inside of the clip area. * @return true if the line is not visible. */ public static boolean clipLine(Point2D from, Point2D to, double lX, double hX, double lY, double hY) { for(;;) { // compute code bits for "from" point int fc = 0; if (from.getX() < lX) fc |= LEFT; else if (from.getX() > hX) fc |= RIGHT; if (from.getY() < lY) fc |= BOTTOM; else if (from.getY() > hY) fc |= TOP; // compute code bits for "to" point int tc = 0; if (to.getX() < lX) tc |= LEFT; else if (to.getX() > hX) tc |= RIGHT; if (to.getY() < lY) tc |= BOTTOM; else if (to.getY() > hY) tc |= TOP; // look for trivial acceptance or rejection if (fc == 0 && tc == 0) return false; if (fc == tc || (fc & tc) != 0) return true; // make sure the "from" side needs clipping if (fc == 0) { double x = from.getX(); double y = from.getY(); from.setLocation(to); to.setLocation(x, y); int t = fc; fc = tc; tc = t; } if ((fc&LEFT) != 0) { if (to.getX() == from.getX()) return true; double t = (to.getY() - from.getY()) * (lX - from.getX()) / (to.getX() - from.getX()); from.setLocation(lX, from.getY() + t); } if ((fc&RIGHT) != 0) { if (to.getX() == from.getX()) return true; double t = (to.getY() - from.getY()) * (hX - from.getX()) / (to.getX() - from.getX()); from.setLocation(hX, from.getY() + t); } if ((fc&BOTTOM) != 0) { if (to.getY() == from.getY()) return true; double t = (to.getX() - from.getX()) * (lY - from.getY()) / ( to.getY() - from.getY()); from.setLocation(from.getX() + t, lY); } if ((fc&TOP) != 0) { if (to.getY() == from.getY()) return true; double t = (to.getX() - from.getX()) * (hY - from.getY()) / (to.getY() - from.getY()); from.setLocation(from.getX() + t, hY); } } } /** * Method to clip a line against a rectangle (in integer). * @param from one end of the line. * @param to the other end of the line. * @param lx the low X bound of the clip. * @param hx the high X bound of the clip. * @param ly the low Y bound of the clip. * @param hy the high Y bound of the clip. * The points are modified to fit inside of the clip area. * @return true if the line is not visible. */ public static boolean clipLine(Point from, Point to, int lx, int hx, int ly, int hy) { for(;;) { // compute code bits for "from" point int fc = 0; if (from.x < lx) fc |= LEFT; else if (from.x > hx) fc |= RIGHT; if (from.y < ly) fc |= BOTTOM; else if (from.y > hy) fc |= TOP; // compute code bits for "to" point int tc = 0; if (to.x < lx) tc |= LEFT; else if (to.x > hx) tc |= RIGHT; if (to.y < ly) tc |= BOTTOM; else if (to.y > hy) tc |= TOP; // look for trivial acceptance or rejection if (fc == 0 && tc == 0) return false; if (fc == tc || (fc & tc) != 0) return true; // make sure the "from" side needs clipping if (fc == 0) { int t = from.x; from.x = to.x; to.x = t; t = from.y; from.y = to.y; to.y = t; t = fc; fc = tc; tc = t; } if ((fc&LEFT) != 0) { if (to.x == from.x) return true; int t = (to.y - from.y) * (lx - from.x) / (to.x - from.x); from.y += t; from.x = lx; } if ((fc&RIGHT) != 0) { if (to.x == from.x) return true; int t = (to.y - from.y) * (hx - from.x) / (to.x - from.x); from.y += t; from.x = hx; } if ((fc&BOTTOM) != 0) { if (to.y == from.y) return true; int t = (to.x - from.x) * (ly - from.y) / (to.y - from.y); from.x += t; from.y = ly; } if ((fc&TOP) != 0) { if (to.y == from.y) return true; int t = (to.x - from.x) * (hy - from.y) / (to.y - from.y); from.x += t; from.y = hy; } } } /** * Method to clip a polygon against a rectangular region. * @param points an array of points that define the polygon. * @param lx the low X bound of the clipping region. * @param hx the high X bound of the clipping region. * @param ly the low Y bound of the clipping region. * @param hy the high Y bound of the clipping region. * @return an array of Points that are clipped to the region. */ public static Point [] clipPoly(Point [] points, int lx, int hx, int ly, int hy) { // see if any points are outside int count = points.length; int pre = 0; for(int i=0; i<count; i++) { if (points[i].x < lx) pre |= LEFT; else if (points[i].x > hx) pre |= RIGHT; if (points[i].y < ly) pre |= BOTTOM; else if (points[i].y > hy) pre |= TOP; } if (pre == 0) return points;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -