问题
I'm trying to draw a number of points on a circle such that each point is the same distance from the next. Eventually I'll be drawing lines between all of the points to make a little drawing. The current hurdle I am dealing with is the points don't come out on the circle uniformly. The way I am figuring the points is, for a given point with angle theta, the X coordinate and Y coordinate are calculated as follows
xc = radius * (Math.cos(theta)) + horizontal center
yc = radius * (Math.sin(theta)) + vertical center
I also have an extra variable so I can make a spinning animation. Here is the code:
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.util.Random;
import java.awt.event.*;
import java.lang.*;
import java.awt.geom.Line2D;
import java.awt.geom.Ellipse2D;
class JavaPaintUI extends JPanel{
int x,y,rad,i;
static Random r = new Random();
BufferedImage image;
Graphics2D g2d;
Timer timer;
double vertices = 24.0;
double angle = 0.0;
double delta = 0.01;
double [] xs = new double[24];
double [] ys = new double[24];
JavaPaintUI(){
image = new BufferedImage(600, 600, BufferedImage.TYPE_INT_ARGB);
g2d = (Graphics2D)image.getGraphics();
setBackground(Color.black);
g2d.setColor(Color.white);
i=0;
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
angle += delta;
angle = angle % 360.0;
iterate();
}
};
timer = new Timer(100, listener);
timer.start();
}
public void iterate(){
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, 600, 600);
g2d.setColor(Color.WHITE);
for(double i = 0; i < vertices; i++){
xs[(int)i] = 250.0 * (Math.cos(((2*Math.PI)/360) * (angle+(i*(360.0/vertices))))) + 300.0;
ys[(int)i] = 250.0 * (Math.sin(((2*Math.PI)/360) * (angle+(i*(360.0/vertices))))) + 300.0;
}
for(double i = 0; i < vertices; i++){
g2d.draw(new Ellipse2D.Double(xs[(int)i], ys[(int)i], 8.0, 8.0));
}
/*
for(double i = 0; i < vertices; i++){
for(double j = 0; j < vertices; j++){
g2d.draw(new Line2D.Double(xs[(int)i],ys[(int)i],xs[(int)j],ys[(int)j]));
}
} */
repaint();
if (i==1000){timer.stop();}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(image,0,0,null);
}
}
I used code from another stack overflow post.Here is a picture of what the circle looks like as of now.

As you can see, there are some extra space between some of the points. Does anyone know what causes this?
EDIT
Thanks guys! It looks much better!

回答1:
I believe the trig functions in Math
take their parameters in radians, not degrees. So you need to do 2 * Math.PI
instead of 360.0
.
回答2:
Math.sin()
and Math.cos()
take an angle in radians and you are providing angles in degrees.
回答3:
A circle has 360 degrees.
So you need an
int numSteps = 32;
double angleStep = 360 / numStpes; // You also can do that with 2*Math.PI / numSteps
for (int i = 0; i < numSteps; i++) {
double angle = i * anglStep;
// now use the formula with x0 + r * sin(angle * toRadians);
}
来源:https://stackoverflow.com/questions/16179240/java-plotting-points-uniformly-on-a-circle-using-graphics2d