问题描述
我试图在圆上绘制多个点,以使每个点到下一个点的距离相同.最终,我将在所有点之间画线以进行一点绘图.我目前面临的障碍是积分不能均匀地出现在圆上.我计算这些点的方式是,对于角度为theta的给定点,X坐标和Y坐标的计算方式如下
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 =半径*(Math.cos(theta))+水平中心
xc = radius * (Math.cos(theta)) + horizontal center
yc =半径*(Math.sin(theta))+垂直中心
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?
编辑
谢谢大家!看起来好多了!
Thanks guys! It looks much better!
推荐答案
我相信Math
中的trig函数采用弧度而不是度数作为参数.因此,您需要执行2 * Math.PI
而不是360.0
.
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
.
这篇关于Java:使用Graphics2d在圆上均匀地绘制点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!