2016-11-16 118 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