創建尖刺(將多邊形創建的圓形變成星形)時遇到了問題。基本上我想從圈子中添加點來將它變成明星。這是一門課程的任務。我不知道如何去創建棘手的代碼。Java:使用創建的多邊形創建星形
注:
的spikiness參數的範圍可以從0.0到1.0以0.0爲未spikey(如英式足球)和1.0是極端spikey(如海膽)。
的spikiness參數決定使用下式的內圓的半徑:innerRadius =半徑*(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,而且我也沒有使用spikiness。我基本上只是複製整個方法,然後做尖刺嗎? – Aramza