📄 genmath.java
字号:
private boolean value; /** * Constructor creates a MutableBoolean object with an initial value. * @param value the initial value. */ public MutableBoolean(boolean value) { this.value = value; } /** * Method to change the value of this MutableBoolean. * @param value the new value. */ public void setValue(boolean value) { this.value = value; } /** * Method to return the value of this MutableBoolean. * @return the current value of this MutableBoolean. */ public boolean booleanValue() { return value; } /** * Returns a printable version of this MutableBoolean. * @return a printable version of this MutableBoolean. */ public String toString() { return Boolean.toString(value); } } /** * Method to compare two objects for equality. * This does more than a simple "equal" because they may have the same value * but have diffent type (one Float, the other Double). * @param first the first object to compare. * @param second the second object to compare. * @return true if they are equal. */ public static boolean objectsReallyEqual(Object first, Object second) { // a simple test if (first.equals(second)) return true; // better comparison of numbers (because one may be Float and the other Double) boolean firstNumeric = false, secondNumeric = false; double firstValue = 0, secondValue = 0; if (first instanceof Float) { firstNumeric = true; firstValue = ((Float)first).floatValue(); } else if (first instanceof Double) { firstNumeric = true; firstValue = ((Double)first).doubleValue(); } else if (first instanceof Integer) { firstNumeric = true; firstValue = ((Integer)first).intValue(); } if (second instanceof Float) { secondNumeric = true; secondValue = ((Float)second).floatValue(); } else if (second instanceof Double) { secondNumeric = true; secondValue = ((Double)second).doubleValue(); } else if (second instanceof Integer) { secondNumeric = true; secondValue = ((Integer)second).intValue(); } if (firstNumeric && secondNumeric) { if (firstValue == secondValue) return true; } if (firstNumeric && second instanceof Boolean) return ((Boolean)second).booleanValue() == (firstValue != 0); if (secondNumeric && first instanceof Boolean) return ((Boolean)first).booleanValue() == (secondValue != 0); return false; } /** A transformation matrix that does nothing (identity). */ public static final AffineTransform MATID = new AffineTransform(); /** * Method to detect if rotation represents a 90 degree rotation in Electric * @param rotation the rotation amount. * @return true if it is a 90-degree rotation. */ public static boolean isNinetyDegreeRotation(int rotation) { return rotation == 900 || rotation == 2700; } /** * Method to return the angle between two points. * @param end1 the first point. * @param end2 the second point. * @return the angle between the points (in tenth-degrees). */ public static int figureAngle(Point2D end1, Point2D end2) { return figureAngle(end2.getX() - end1.getX(), end2.getY() - end1.getY()); } /** * Method to return the angle of a vector. * Return 0 if vector is zero or has NaN components. * @param x x-coordinate of a vector. * @param y y-coordinate of a vector. * @return the angle of a vector. */ public static int figureAngle(double x, double y) { int ang = 0; if (x == 0) return y > 0 ? 900 : y < 0 ? 2700 : 0; if (y == 0) return x < 0 ? 1800 : 0; if (y < 0) { x = -x; y = -y; ang = 1800; } else if (!(y > 0)) { return 0; } if (x < 0) { double t = x; x = y; y = -t; ang += 900; } else if (!(x > 0)) { return 0; } assert x > 0 && y > 0; if (x == y) return ang + 450; ang += (int)(y < x ? 0.5 + Math.atan(y/x)*(1800/Math.PI) : 900.5 - Math.atan(x/y)*(1800/Math.PI)); if (ang >= 3600) ang -= 3600; return ang; } /** * Method to return the angle between two points. * @param end1 the first point. * @param end2 the second point. * @return the angle between the points (in radians). */ public static double figureAngleRadians(Point2D end1, Point2D end2) { double dx = end2.getX() - end1.getX(); double dy = end2.getY() - end1.getY(); if (dx == 0.0 && dy == 0.0) { System.out.println("Warning: domain violation while figuring angle in radians"); return 0; } double ang = Math.atan2(dy, dx); if (ang < 0.0) ang += Math.PI * 2.0; return ang; } /** * Method to compute the area of a given rectangle. * @param rect the rectangle * @return the area of the rectangle */ public static double getArea(Rectangle2D rect) { return rect.getWidth() * rect.getHeight(); } /** * Method to compute the area of a polygon defined by an array of points. * Returns always positive numbers * @param points the array of points. * @return the area of the polygon defined by these points. */ public static double getAreaOfPoints(Point2D [] points) { double area = 0.0; double x0 = points[0].getX(); double y0 = points[0].getY(); double y1 = 0; for(int i=1; i<points.length; i++) { double x1 = points[i].getX(); y1 = points[i].getY(); // triangulate around the polygon double p1 = x1 - x0; double p2 = y0 + y1; double partial = p1 * p2; area += partial / 2.0; x0 = x1; y0 = y1; } double p1 = points[0].getX() - x0; double p2 = points[0].getY() + y1; double partial = p1 * p2; area += partial / 2.0; return Math.abs(area); } /** * Method to return the sum of two points. * @param p the first point. * @param dx the X component of the second point * @param dy the T component of the second point * @return the sum of two points. */ public static Point2D addPoints(Point2D p, double dx, double dy) { return new Point2D.Double(p.getX()+dx, p.getY()+dy); } /** * Method to tell whether a point is on a given line segment. * <p>NOTE: If you are comparing Electric database units, DO NOT * use this method. Use the corresponding method from DBMath. * @param end1 the first end of the line segment. * @param end2 the second end of the line segment. * @param pt the point in question. * @return true if the point is on the line segment. */ public static boolean isOnLine(Point2D end1, Point2D end2, Point2D pt) { // trivial rejection if point not in the bounding box of the line if (pt.getX() < Math.min(end1.getX(), end2.getX())) return false; if (pt.getX() > Math.max(end1.getX(), end2.getX())) return false; if (pt.getY() < Math.min(end1.getY(), end2.getY())) return false; if (pt.getY() > Math.max(end1.getY(), end2.getY())) return false; // handle manhattan cases specially if (end1.getX() == end2.getX()) { if (pt.getX() == end1.getX()) return true; return false; } if (end1.getY() == end2.getY()) { if (pt.getY() == end1.getY()) return true; return false; } // handle nonmanhattan if ((pt.getX()-end1.getX()) * (end2.getY()-end1.getY()) == (pt.getY()-end1.getY()) * (end2.getX()-end1.getX())) return true; return false; } /** * Method to find the point on a line segment that is closest to a given point. * @param p1 one end of the line segment. * @param p2 the other end of the line segment. * @param pt the point near the line segment. * @return a point on the line segment that is closest to "pt". * The point is guaranteed to be between the two points that define the segment. */ public static Point2D closestPointToSegment(Point2D p1, Point2D p2, Point2D pt) { // find closest point on line Point2D pi = closestPointToLine(p1, p2, pt); // see if that intersection point is actually on the segment if (pi.getX() >= Math.min(p1.getX(), p2.getX()) && pi.getX() <= Math.max(p1.getX(), p2.getX()) && pi.getY() >= Math.min(p1.getY(), p2.getY()) && pi.getY() <= Math.max(p1.getY(), p2.getY())) { // it is return pi; } // intersection not on segment: choose one endpoint as the closest double dist1 = pt.distance(p1); double dist2 = pt.distance(p2); if (dist2 < dist1) return p2; return p1; } /** * Method to find the point on a line that is closest to a given point. * @param p1 one end of the line. * @param p2 the other end of the line. * @param pt the point near the line. * @return a point on the line that is closest to "pt". * The point is not guaranteed to be between the two points that define the line. */ public static Point2D closestPointToLine(Point2D p1, Point2D p2, Point2D pt) { // special case for horizontal line if (p1.getY() == p2.getY()) { return new Point2D.Double(pt.getX(), p1.getY()); } // special case for vertical line if (p1.getX() == p2.getX()) { return new Point2D.Double(p1.getX(), pt.getY()); } // compute equation of the line double m = (p1.getY() - p2.getY()) / (p1.getX() - p2.getX()); double b = -p1.getX() * m + p1.getY(); // compute perpendicular to line through the point double mi = -1.0 / m; double bi = -pt.getX() * mi + pt.getY(); // compute intersection of the lines double t = (bi-b) / (m-mi); return new Point2D.Double(t, m * t + b); } /** * Method to determine whether an arc at angle "ang" can connect the two ports * whose bounding boxes are "lx1<=X<=hx1" and "ly1<=Y<=hy1" for port 1 and * "lx2<=X<=hx2" and "ly2<=Y<=hy2" for port 2. Returns true if a line can * be drawn at that angle between the two ports and returns connection points * in (x1,y1) and (x2,y2) */ public static Point2D [] arcconnects(int ang, Rectangle2D bounds1, Rectangle2D bounds2) { // first try simple solutions Point2D [] points = new Point2D[2]; double lx1 = bounds1.getMinX(), hx1 = bounds1.getMaxX(); double ly1 = bounds1.getMinY(), hy1 = bounds1.getMaxY(); double lx2 = bounds2.getMinX(), hx2 = bounds2.getMaxX(); double ly2 = bounds2.getMinY(), hy2 = bounds2.getMaxY(); if ((ang%1800) == 0) { // horizontal angle: simply test Y coordinates if (ly1 > hy2 || ly2 > hy1) return null; double y = (Math.max(ly1, ly2) + Math.min(hy1, hy2)) / 2; points[0] = new Point2D.Double((lx1+hx1) / 2, y); points[1] = new Point2D.Double((lx2+hx2) / 2, y); return points; } if ((ang%1800) == 900) { // vertical angle: simply test X coordinates if (lx1 > hx2 || lx2 > hx1) return null; double x = (Math.max(lx1, lx2) + Math.min(hx1, hx2)) / 2; points[0] = new Point2D.Double(x, (ly1+hy1) / 2); points[1] = new Point2D.Double(x, (ly2+hy2) / 2); return points; } // construct an imaginary line at the proper angle that runs through (0,0) double a = DBMath.sin(ang) / 1073741824.0; double b = -DBMath.cos(ang) / 1073741824.0; // get the range of distances from the line to port 1 double lx = lx1; double hx = hx1; double ly = ly1; double hy = hy1; double d = lx*a + ly*b; double low1 = d; double high1 = d;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -