📄 arccalc.java
字号:
if (Debug.debugging("arc")) { Debug.output("ArcCalc.generateArc: creating supplimental graphics list"); arcGraphics = new OMGraphicList(); double dist1 = Math.sqrt(Math.pow((double) (arcCenter.x - x1), 2.0) + Math.pow((double) (arcCenter.y - y1), 2.0)); double dist2 = Math.sqrt(Math.pow((double) (arcCenter.x - x2), 2.0) + Math.pow((double) (arcCenter.y - y2), 2.0)); Debug.output("ArcCalc.generate: Center focus for arc is (" + arcCenter.x + ", " + arcCenter.y + ") along slope line of " + (inverseSlope * 180.0 / Math.PI) + " degrees)."); Debug.output("ArcCalc.generate: Distance to point 1 from arc focus = " + dist1 + "\n Distance to point 2 from arc focus = " + dist2); // Let's hightlight the end points. OMRect point1 = new OMRect(x1 - 1, y1 - 1, x1 + 1, y1 + 1); OMRect point2 = new OMRect(x2 - 1, y2 - 1, x2 + 1, y2 + 1); OMRect arcPoint = new OMRect(arcCenter.x - 1, arcCenter.y - 1, arcCenter.x + 1, arcCenter.y + 1); point1.setLinePaint(OMColor.red); point2.setLinePaint(OMColor.red); arcPoint.setLinePaint(OMColor.blue); arcGraphics.add(point1); arcGraphics.add(point2); arcGraphics.add(arcPoint); OMLine line1 = new OMLine(x1, y1, x2, y2); OMLine line2 = new OMLine(midPoint.x, midPoint.y, arcCenter.x, arcCenter.y); arcGraphics.add(line1); arcGraphics.add(line2); } int realCount = 0; // Figure out the arc extents for each endpoint. I think // it's easier to keep track of the angles if they are always // positive, and we always go from smaller to larger. double startSlope = getRealAngle(arcCenter.x, arcCenter.y, x1, y1); double endSlope = getRealAngle(arcCenter.x, arcCenter.y, x2, y2); double smallSlope, largeSlope; double angleIncrement; smallSlope = (startSlope > endSlope) ? endSlope : startSlope; largeSlope = (smallSlope == startSlope) ? endSlope : startSlope; // Have to make sure we take the smaller arc around the // circle. while (Math.abs(smallSlope - largeSlope) > Math.PI) { if (Math.abs(largeSlope - smallSlope - Math.PI) < .001) { // Catch 180 degree angles that are close enough... break; } Debug.message("arc", "ArcCalc.generate: Modifying the starting slope."); double tmpSlope = smallSlope + MoreMath.TWO_PI; smallSlope = largeSlope; largeSlope = tmpSlope; } // Experienced some trouble with vertical and horizonal half // circles. This took care of that. if (arcAngle == Math.PI && arcUp) { Debug.message("arc", "ArcCalc.generate: Modifying 180 angle points."); double tmpSlope = smallSlope + MoreMath.TWO_PI; smallSlope = largeSlope; largeSlope = tmpSlope; } // Figure out the angle increment for grabbing coordinates - // use the larger dimension of the arc end point differences. if (Math.abs(y2 - y1) < Math.abs(x2 - x1)) { angleIncrement = Math.PI / Math.abs(x2 - x1); } else { angleIncrement = Math.PI / Math.abs(y2 - y1); } int numPoints = (int) (Math.abs(smallSlope - largeSlope) / angleIncrement + 2); int[] xPoints = new int[numPoints]; int[] yPoints = new int[numPoints]; if (Debug.debugging("arc")) { Debug.output("ArcCalc.generate: angle to x1, y1 is " + startSlope + " (" + (startSlope * 180.0 / Math.PI) + " degrees), angle to x2, y2 is " + endSlope + " (" + (endSlope * 180.0 / Math.PI) + " degrees)"); Debug.output("ArcCalc.generate: Starting angle is " + smallSlope + "(" + (smallSlope * 180.0 / Math.PI) + " degrees), end angle is " + largeSlope + " (" + (largeSlope * 180.0 / Math.PI) + " degrees), incrementing by " + angleIncrement + " (" + (angleIncrement * 180.0 / Math.PI) + " degrees)"); } reversed = false; // Get the coordinates of the arc from the arc extents. while (smallSlope < largeSlope && realCount < numPoints) { xPoints[realCount] = arcCenter.x + (int) (arcRadius * Math.cos(smallSlope)); yPoints[realCount] = arcCenter.y + (int) (arcRadius * Math.sin(smallSlope)); if (realCount == 0 && xPoints[realCount] == x2) { Debug.message("arc", "ArcCalc: line reversed"); reversed = true; } if (Debug.debugging("arc") && realCount == 0) { OMLine startLine = new OMLine(arcCenter.x, arcCenter.y, xPoints[0], yPoints[0]); startLine.setLinePaint(OMColor.white); arcGraphics.add(startLine); } else if (Debug.debugging("arcdetail")) { Debug.output(" angle " + smallSlope + " (" + smallSlope * 180 / Math.PI + " degrees) = " + xPoints[realCount] + ", " + yPoints[realCount]); } if (Math.abs(largeSlope - smallSlope - (arcAngle / 2.0)) < angleIncrement) { // Found the halfway point, mark it... peakPoint.x = xPoints[realCount]; peakPoint.y = yPoints[realCount]; Debug.message("arc", "ArcCalc: Found a midpoint."); } smallSlope += angleIncrement; realCount++; } // Give the coordinates to the OMLine. xpoints = new int[realCount]; ypoints = new int[realCount]; System.arraycopy(xPoints, 0, xpoints, 0, realCount); System.arraycopy(yPoints, 0, ypoints, 0, realCount); } /** * Given the straight line between two points, figure out the * angle, in radians, of that line in relation to the coordinate * system on the screen. Always returns a positive value, and the * angle is from point 1 to point 2. */ protected double getRealAngle(int x1, int y1, int x2, int y2) { double angle = 0; double horDiff = (double) (x2 - x1); double vertDiff = (double) (y2 - y1); // If there is no horizontal difference, then it's pointing // up or down. if (horDiff == 0) { if (vertDiff > 0) { angle = MoreMath.HALF_PI; } else if (vertDiff < 0) { angle = -MoreMath.HALF_PI; } } else { angle = Math.atan(vertDiff / horDiff); // It's pointed in the wrong direction... fix it here. if (horDiff < 0) { angle += Math.PI; } } // Either way, I think we want to make the angle positive. while (angle < 0) { angle += MoreMath.TWO_PI; } return angle; } public int[] getXPoints() { return xpoints; } public int[] getYPoints() { return ypoints; } public void generate(Projection proj) { if (proj != null && arcGraphics != null) { arcGraphics.generate(proj); } } public void render(Graphics g) { if (arcGraphics != null) { Debug.output("OMLine rendering " + arcGraphics.size() + " arcGraphics."); arcGraphics.render(g); } } public OMGraphicList getArcGraphics() { if (arcGraphics == null) { return new OMGraphicList(); } else { return arcGraphics; } } public boolean getReversed() { return reversed; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -