Jump to content

Java: Creating a Star from a Circle Created Polygon

Greetings,

 

I'm having trouble creating the spikiness (to turn the circle created via polygon into a star). Basically I want to add points out of the circle to turn it into a star. I don't know how to go about creating the code for the spikiness (turning the circle polygon into a star). I believe the error is with drawing the final polygon and casting but I am not quite sure.

Notes:

  1. The spikiness parameter can range from 0.0 to 1.0 with 0.0 being not spikey (like a soccer ball) and 1.0 being extremely spikey (like a sea urchin).

  2. The spikiness parameter determines the radius of the inner circle using the formula: innerRadius = radius * (1.0 - spikiness) (An example formula I tried implementing but with no success)

I'd appreciate the help!

My code:

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.YELLOW);
	       
		   fillStar(g, 250, 250, 100, 36, 1);
	   }
	   
	   public static void fillStar(Graphics2D g, int ctrX, int ctrY, int radius, int nPoints, double 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));
		   
		   for (int j = 0; j < nPoints; j++) // Casts for ints and doubles
		   {
			   int xPoint[];
			   int yPoint[];
			   xPoint[j] = (int) xDouble[j];
			   yPoint[j] = (int) yDouble[j];
		   }
		   }
		   
		   g.fillPolygon(xPoint, yPoint, nPoints); // Creates polygon
	   }
}

 

CPU - I7 4770k @ 4.3GHz GPU - EVGA Nvidia GeForce GTX 780 Motherboard - ASUS Maximus VI Extreme RAM - 16GB of Corsair Dominator Platinum @ 2133MHz  Case - Corsair 800D PSU - Corsair AX1,200i CPU Cooler - Custom Liquid CPU Cooler SSD - Samsung 840 Pro 256GB Hard Drive - Western Digital Black 1TB.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×