【问题标题】:Java: Creating a Star from Circle Created PolygonJava:从圆形创建的多边形创建星形
【发布时间】:2016-11-16 22:15:49
【问题描述】:

我在创建尖峰时遇到了麻烦(将通过多边形创建的圆变成星形)。基本上我想在圆圈外添加点以将其变成星星。这是一门课程的作业。我不知道如何为尖刺创建代码。

注意事项:

  1. spikness 参数的范围为 0.0 到 1.0,其中 0.0 表示不刺穿(如足球),而 1.0 表示非常刺穿(如海胆)。

  2. 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


【解决方案1】:

有 N 个“点”的星只是一个有 2N 个点的圆,其中点 0、2、4... 的半径为 R,点 1、3、5... 的半径为 @987654322 @。

您只是在创建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));
}

【讨论】:

  • 我很难让程序绘制多边形 drawPolygon(int, int, int),我也尝试过投射。
猜你喜欢
  • 2020-10-24
  • 1970-01-01
  • 2016-01-07
  • 2021-11-08
  • 2012-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-11
相关资源
最近更新 更多