📄 drawutil.java
字号:
public final static float OLD_distance_to_line(int x1, int y1, int x2, int y2, int x, int y) { float m; /* slope of the line */ float c; /* slope of a line perpendicular to the line */ float b; /* y intercept of line */ float d; /* y intercept of a line perpendicular to the line */ int xi, yi; /* intersection of line and perpendicular */ /* vertical line */ if (x2 == x1) { if (y1 <= y && y <= y2 || y2 <= y && y <= y1) return (float) Math.abs(x - x1); // mouse is alongside // line return distance_to_endpoint(x1, y1, x2, y2, x, y); } /* horizontal line */ if (y2 == y1) { if (x1 <= x && x <= x2 || x2 <= x && x <= x1) return (float) Math.abs(y - y1); // mouse is alongside // line return distance_to_endpoint(x1, y1, x2, y2, x, y); } m = ((float) (y2 - y1)) / ((float) (x2 - x1)); /* * slope of the * line */ c = -1.0f / m; /* slope of perpendicular line */ d = (float) y - c * (float) x;/* * perpendicular line through * mouse */ b = (float) y1 - m * (float) x1; /* the line in the drawing */ // NOTE: round error xi = (int) ProjMath.qint((d - b) / (m - c));// x intersection yi = (int) ProjMath.qint(c * (float) xi + d);// y intersection /* * If intersection is on the line segment distance is distance * from mouse to it. */ if ((x1 <= xi && xi <= x2 || x2 <= xi && xi <= x1) && (y1 <= yi && yi <= y2 || y2 <= yi && yi <= y1)) return distance(xi, yi, x, y); /* distance is distance from mouse to nearest endpt */ return distance_to_endpoint(x1, y1, x2, y2, x, y); } /** * Compute perpendicular distance from point to line. * <p> * * @param x1 line x coord1 * @param y1 line y coord1 * @param x2 line x coord2 * @param y2 line y coord2 * @param x3 point x coord * @param y3 point y coord * @return float distance to line * */ public final static float perpendicular_distance_to_line(int x1, int y1, int x2, int y2, int x3, int y3) { int x12 = x2 - x1; int y12 = y2 - y1; int x13 = x3 - x1; int y13 = y3 - y1; float D12 = distance(x1, y1, x2, y2); return Math.abs((/* Math.abs */(x12 * y13) - /* Math.abs */(x13 * y12)) / D12); } /** * Computes the distance from a point to a line segment. * <p> * Variable usage as follows: * <p> * <ul> * <li>x12 x distance from the first endpoint to the second. * <li>y12 y distance from the first endpoint to the second. * <li>x13 x distance from the first endpoint to point being * tested. * <li>y13 y distance from the first endpoint to point being * tested. * <li>x23 x distance from the second endpoint to point being * tested. * <li>y23 y distance from the second endpoint to point being * tested. * <li>D12 Length of the line segment. * <li>pp distance along the line segment to the intersection of * the perpendicular from the point to line extended. * </ul> * * Procedure: * <p> * * Compute D12, the length of the line segment. Compute pp, the * distance to the perpendicular. If pp is negative, the * intersection is before the start of the line segment, so return * the distance from the start point. If pp exceeds the length of * the line segment, then the intersection is beyond the end point * so return the distance of the point from the end point. * Otherwise, return the absolute value of the length of the * perpendicular line. The sign of the length of the perpendicular * line indicates whether the point lies to the right or left of * the line as one travels from the start point to the end point. * <p> * * @param x1 line x coord1 * @param y1 line y coord1 * @param x2 line x coord2 * @param y2 line y coord2 * @param x3 point x coord * @param y3 point y coord * @return float distance to line segment * */ public final static float distance_to_line(int x1, int y1, int x2, int y2, int x3, int y3) { // algorithm courtesy of Ray 1/16/98 float x12 = x2 - x1; float y12 = y2 - y1; float x13 = x3 - x1; float y13 = y3 - y1; float D12 = (float) Math.sqrt(x12 * x12 + y12 * y12); float pp = (x12 * x13 + y12 * y13) / D12; if (pp < 0.0) { return (float) Math.sqrt(x13 * x13 + y13 * y13); } if (pp > D12) { float x23 = x3 - x2; float y23 = y3 - y2; return (float) Math.sqrt(x23 * x23 + y23 * y23); } return (float) Math.abs(((x12 * y13 - y12 * x13) / D12)); } /** * Generates a line with width lw, returns an ArrayList of 4 x-y * coords. * <p> * * @param lw line width * @param x1 line x coord1 * @param y1 line y coord1 * @param x2 line x coord2 * @param y2 line y coord2 * @return ArrayList of int[], int[] */ public static ArrayList generateWideLine(int lw, int x1, int y1, int x2, int y2) { ArrayList ret_val = new ArrayList(2); int[] x = new int[4]; int[] y = new int[4]; // calculate the offsets // lw = lw -1; int off1 = (int) lw / 2; int off2 = (lw % 2 == 1) ? (int) lw / 2 + 1 : (int) lw / 2; // slope <= 1 if (Math.abs((float) (y2 - y1) / (float) (x2 - x1)) <= 1f) { x[0] = x[3] = x1; x[1] = x[2] = x2; y[0] = y1 + off1; y[1] = y2 + off1; y[2] = y2 - off2; y[3] = y1 - off2; ret_val.add(x); ret_val.add(y); } // slope > 1 else { x[0] = x1 + off1; x[1] = x2 + off1; x[2] = x2 - off2; x[3] = x1 - off2; y[0] = y[3] = y1; y[1] = y[2] = y2; ret_val.add(x); ret_val.add(y); } return ret_val; } /** * Generates a polygon or polyline with positive width lw. * <p> * Returns ArrayList of x-y array pairs of coordinates of polygon * segments. the parameter altx must either be null, or an * alternate array of points to draw. * <p> * * @param lw line width * @param xpts int[] x coords * @param ypts int[] y coords * @param altx int[] altx coords * @param connect polygon or polyline? * @return ArrayList * */ final public static ArrayList generateWidePoly(int lw, int[] xpts, int[] ypts, int[] altx, boolean connect) { return generateWidePoly(lw, xpts.length, xpts, ypts, altx, connect); } /** * Generates a polygon or polyline with positive width lw. * <p> * Returns ArrayList of x-y array pairs of coordinates of polygon * segments. the parameter altx must either be null, or an * alternate array of points to draw. * <p> * * @param lw line width * @param len numcoords * @param xpts int[] x coords * @param ypts int[] y coords * @param altx int[] altx coords * @param connect polygon or polyline? * @return ArrayList * */ final public static ArrayList generateWidePoly(int lw, int len, int[] xpts, int[] ypts, int[] altx, boolean connect) { // HACK - altx deprecated? ArrayList ret_val = new ArrayList(len * 4); int off1 = 0, off2 = 0; int[] x = null, y = null, a_x = null; float slope; int end = (connect) ? len : len - 1; if (len <= 1) return new ArrayList(); // lw = lw -1; // calculate the offsets - HACK: +1 side not consistent... off1 = (int) lw / 2; off2 = (int) Math.ceil((float) lw / 2f); // System.out.print("DrawUtil.generateWidePoly Points for // lw="+lw); // for (int i=0;i<len;i++) { // System.out.print("(" + xpts[i] + "," + ypts[i] + ")"); // } // Debug.output(""); for (int i = 0, j = (i + 1) % len; i < end; i++, j = (i + 1) % len) { x = new int[4]; y = new int[4]; // handle division by zero if (xpts[i] == xpts[j]) slope = Float.POSITIVE_INFINITY; else slope = Math.abs((float) (ypts[j] - ypts[i]) / (float) (xpts[j] - xpts[i])); // slope <= 1 if (slope <= 1f) { x[0] = x[3] = xpts[i]; x[1] = x[2] = xpts[j]; y[0] = ypts[i] + off1; y[1] = ypts[j] + off1; y[2] = ypts[j] - off2; y[3] = ypts[i] - off2; ret_val.add(x); ret_val.add(y); if (altx != null) { a_x = new int[4]; a_x[0] = a_x[3] = altx[i]; a_x[1] = a_x[2] = altx[j]; ret_val.add(a_x); ret_val.add(y); } } // slope > 1 else { x[0] = xpts[i] + off1; x[1] = xpts[j] + off1; x[2] = xpts[j] - off2; x[3] = xpts[i] - off2; y[0] = y[3] = ypts[i]; y[1] = y[2] = ypts[j]; ret_val.add(x); ret_val.add(y); if (altx != null) { a_x = new int[4]; a_x[0] = altx[i] + off1; a_x[1] = altx[j] + off1; a_x[2] = altx[j] - off2; a_x[3] = altx[i] - off2; ret_val.add(a_x); ret_val.add(y); } } } return ret_val; } /* * public static void main(String[] args) { * * * Debug.output("distance_to_line(0,0,4,4, 2,0): " + * distance_to_line(0,0,4,4, 2,0)); * Debug.output("OLD_distance_to_line(0,0,4,4, 2,0): " + * OLD_distance_to_line(0,0,4,4, 2,0)); * Debug.output("distance_to_line(0,0,4,4, 50,50): " + * distance_to_line(0,0,4,4, 50,50)); * Debug.output("OLD_distance_to_line(0,0,4,4, 50,50): " + * OLD_distance_to_line(0,0,4,4, 50,50)); * Debug.output("distance_to_line(-34,12,44,104, -44,-50): " + * distance_to_line(-34,12,44,104, -44,-50)); * Debug.output("OLD_distance_to_line(-34,12,44,104, -44,-50): " + * OLD_distance_to_line(-34,12,44,104, -44,-50)); System.exit(0); * // 3-4-5 triangle Debug.output(distance(0,0,3,4)); * Debug.output(distance(0,0,-3,4)); * Debug.output(distance(0,0,-3,-4)); * Debug.output(distance(0,0,3,-4)); Debug.output(); * * Debug.output(distance_to_line(0,0,2,2, 0,2)); // root 2 * Debug.output(distance_to_line(0,0,2,0, 0,2)); // 2 * Debug.output(distance_to_line(0,0,2,0, -1,-1)); // root 2 * Debug.output(distance_to_line(0,0,2,0, 1,0)); // 0 * Debug.output(distance_to_line(0,0,2,2, 1,0)); // rounded! * Debug.output(); * * int[] xpts = new int[3]; int[] ypts = new int[3]; xpts[0] = 0; * ypts[0] = 0; xpts[1] = 3; ypts[1] = 0; xpts[2] = 3; ypts[2] = * 4; * * Debug.output(closestPolyDistance(xpts, ypts, 0,4, true)); * Debug.output(closestPolyDistance(xpts, ypts, 0,4, false));//3 * * xpts[0] = 0; ypts[0] = 0; xpts[1] = 2; ypts[1] = 0; xpts[2] = * 2; ypts[2] = 2; Debug.output(closestPolyDistance(xpts, ypts, * 0,1, true));//round Debug.output(closestPolyDistance(xpts, * ypts, 0,1, false));//1 * // linewidth testing * * Debug.output(""); ArrayList vec = generateWideLine(3, 0, 0, 5, * 5); * * int[] x = (int[])vec.elementAt(0); int[] y = * (int[])vec.elementAt(1); System.out.print("wide line: "); for * (int i = 0; i <x.length; i++) { System.out.print(x[i] + "," + * y[i] + " "); } Debug.output(""); * * Debug.output(""); vec = generateWideLine(4, 0, 0, -5, -3); * * x = (int[])vec.elementAt(0); y = (int[])vec.elementAt(1); * System.out.print("wide line: "); for (int i = 0; i <x.length; * i++) { System.out.print(x[i] + "," + y[i] + " "); } * Debug.output(""); Debug.output(""); * * xpts = new int[4]; ypts = new int[4]; xpts[0] = 0; ypts[0] = 0; * xpts[1] = 5; ypts[1] = 2; xpts[2] = 4; ypts[2] = 8; xpts[3] = * -2; ypts[3] = 6; vec = generateWidePoly(3, xpts, ypts, null, * false); int size = vec.size(); for (int j = 0; j < size; j+=2) { * x = (int[])vec.elementAt(j); y = (int[])vec.elementAt(j+1); * System.out.print("wide poly: "); for (int i = 0; i <x.length; * i++) { System.out.print(x[i] + "," + y[i] + " "); } * Debug.output(""); } } */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -