2016-11-16 109 views
-1

創建尖刺(將多邊形創建的圓形變成星形)時遇到了問題。基本上我想從圈子中添加點來將它變成明星。這是一門課程的任務。我不知道如何去創建棘手的代碼。Java:使用創建的多邊形創建星形

注:

  1. 的spikiness參數的範圍可以從0.0到1.0以0.0爲未spikey(如英式足球)和1.0是極端spikey(如海膽)。

  2. 的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 
     } 
} 
+0

與N「點」阿星就是用2N點,其中點0,2,4 ......都在半徑'R',並點1的圓,3,4 ...的半徑爲'spikiness * R'。你只創建'N'點(不是'2N'),並且你沒有使用'spikiness'。 –

+0

我知道我沒有創建2N,而且我也沒有使用spikiness。我基本上只是複製整個方法,然後做尖刺嗎? – Aramza

回答

0

與N 「點」 A星只是用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)); 
} 
+0

我很難讓程序繪製多邊形drawPolygon(int,int,int),我也試過了。 – Aramza