【发布时间】:2018-06-12 20:30:12
【问题描述】:
我的作业被困住了;有人有想法吗?
任务: 实现一个递归方法 boxed(double[] center, double radius) 绘制如下图:
(外部正方形是图像边缘。)数组中心包含所有正方形中心的 x 和 y 坐标,而半径是正方形长度的一半。内部正方形总是旋转 45°。当半径小于一个像素时,该方法应该停止绘制。
给出了其他方法。 这个画了一个双角旋转的正方形:
private static void squareRotated(double[] center, double radius, double angle) {
double[] upperLeft = {-radius, radius};
double[] upperRight = {radius, radius};
double[] lowerLeft = {-radius, -radius};
double[] lowerRight = {radius, -radius};
double[] rotUpperLeft = rotatePoint(upperLeft,angle);
double[] rotUpperRight = rotatePoint(upperRight,angle);
double[] rotLowerLeft = rotatePoint(lowerLeft,angle);
double[] rotLowerRight = rotatePoint(lowerRight,angle);
StdDraw.polygon(new double[]{rotUpperLeft[0]+center[0],rotUpperRight[0]+center[0],
rotLowerRight[0]+center[0],rotLowerLeft[0]+center[0]},
new double[]{rotUpperLeft[1]+center[1],rotUpperRight[1]+center[1],
rotLowerRight[1]+center[1],rotLowerLeft[1]+center[1]});
}
这个将一个点旋转双角:
private static double[] rotatePoint(double[] point, double angle) {
double[] result = new double[2];
result[0] = point[0]*Math.cos(angle) - point[1]*Math.sin(angle);
result[1] = point[0]*Math.sin(angle) + point[1]*Math.cos(angle);
return result;
}
}
这是我的代码:
private static double angle = 0;
private static void boxed(double[] center, double radius) {
if (radius > (double) 1/512) {
squareRotated(center, radius, angle);
angle += Math.PI/4;
boxed(center, Math.sqrt(radius*radius + radius*radius)/2);
}
}
它有效,但有没有办法可以避免私有静态双角?我不允许在方法中添加第三个参数,我必须通过递归来解决它。
【问题讨论】:
-
这样的声音应该在 CodeReview 上...
-
我投票结束这个问题,因为它属于Code Review
标签: java function recursion graphic