【发布时间】:2016-11-16 22:15:49
【问题描述】:
我在创建尖峰时遇到了麻烦(将通过多边形创建的圆变成星形)。基本上我想在圆圈外添加点以将其变成星星。这是一门课程的作业。我不知道如何为尖刺创建代码。
注意事项:
spikness 参数的范围为 0.0 到 1.0,其中 0.0 表示不刺穿(如足球),而 1.0 表示非常刺穿(如海胆)。
spikness 参数使用以下公式确定内圆的半径:innerRadius = radius * (1.0 - spikiness)(我尝试实现但没有成功的示例公式)
非常感谢您的帮助!
我的代码:
import java.awt.*;
public class StarSampler {
public static void main(String[] args)
{
DrawingPanel panel = new DrawingPanel(500, 500);
Graphics2D g = panel.getGraphics();
g.setColor(Color.BLUE);
fillStar(g, 250, 250, 100, 36, 1); // The (1) is for extreme spikeness
}
public static void fillStar(Graphics2D g, int ctrX, int ctrY, int radius, int nPoints, double spikiness)
{
double xDouble[] = new double[nPoints];
double yDouble[] = new double [nPoints];
int xPoint[] = new int[nPoints];
int yPoint[]= new int[nPoints];
int angle = 0;
for (int i = 0; i < nPoints; i++) // Continue through use of Prog6 formula
{
xDouble[i] = ctrX + radius * Math.cos(Math.toRadians(angle));
yDouble[i] = ctrY + radius * Math.sin(Math.toRadians(angle));
angle += 10;
}
for (int j = 0; j < nPoints; j++) // Casts for ints and doubles
{
xPoint[j] = (int) xDouble[j];
yPoint[j] = (int) yDouble[j];
}
g.fillPolygon(xPoint, yPoint, nPoints); // Creates polygon
}
}
【问题讨论】:
-
一个有 N 个“点”的星只是一个有 2N 个点的圆,其中点 0、2、4... 的半径为
R,点 1、3、4...位于半径spikiness * R。你只是在创建N点(不是2N),你没有使用spikiness。 -
我知道我没有创建 2N,也没有使用尖峰。我基本上只是复制整个方法然后做尖刺吗?
标签: java loops drawing polygons