我无法创建尖刺(将通过多边形创建的圆转变为星形)。基本上我想从圆圈中添加点以将其变成星星。这是一门课程的作业。我不知道如何为尖锐的代码创建代码。
注意:
spikiness参数的范围可以从0.0到1.0,其中0.0不是spikey(如足球),1.0是非常spikey(如海胆)。
spikiness参数使用以下公式确定内圆的半径: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
}
}
答案 0 :(得分:0)
具有N“点”的星只是一个具有2N点的圆,其中点0,2,4 ......位于半径R
处,而点1,3,5 ...处于半径spikiness * R
。
您只是创建N
点(不是2N
),而您没有使用spikiness
。这样的事情可能有用(未经测试):
double xDouble[] = new double[2*nPoints];
double yDouble[] = new double[2*nPoints];
for (int i = 0; i < 2*nPoints; i++)
{
double iRadius = (i % 2 == 0) ? radius : (radius * spikiness);
double angle = i * 360.0 / (2*nPoints);
xDouble[i] = ctrX + iRadius * Math.cos(Math.toRadians(angle));
yDouble[i] = ctrY + iRadius * Math.sin(Math.toRadians(angle));
}