gpt4 book ai didi

java - 计算不完美的圆

转载 作者:行者123 更新时间:2023-11-29 05:09:16 25 4
gpt4 key购买 nike

我想创建一个“非完美圆”的生成器,圆有点扭曲且更随机,但看起来仍然有点像圆或云。

这就是我所说的不完美的圆: enter image description here

我想创建一个函数来获取“非完美圆”的最大和最小比例并获取其所有点。我知道圆的公式:X^2+Y^2=R^2 但我想不出一种方法让它更随机一些。有人有什么想法吗?

编辑:试图用点画一个完美的圆但它不会工作:

    for (int step = 0; step < 300; ++step) {
double t = step / 300 * 2 * Math.PI;
c.drawPoint(300+(float)(33 * Math.cos(t)), 300+(float)(33 * Math.sin(t)), p);
}

编辑 2:

    for (int step = 0; step < 20; ++step) {
double t = step / 20.0 * 2 * Math.PI;
double imperfectR = 50.0+randInt(10, 50);
//I do it here?
points[step]=new PointF();
points[step].set((300+(float)(imperfectR * Math.cos(t))), 300+(float)(imperfectR * Math.sin(t)));
if(step==0){
pp.moveTo(points[step].x, points[step].y);
}
else
pp.quadTo(points[step-1].x, points[step-1].y,points[step].x, points[step].y);

}

编辑 3:

double t=0;
for (int i = 0; i < points.length/4; i++) {
if(t==1){
t=0;
}
t+=0.10;
double imperfectR=0.5*((2*points[i+1].y)+(-points[i].y+points[i+2].y)*t+(2*points[i].y-5*points[i+1].y+4*points[i+2].y-points[i+3].y)*(t*t)+((-points[i].y+3*points[i+1].y-3*points[i+2].y+points[i+3].y)*(t*t*t)));
newPoints[i].set((300+(float)(imperfectR * Math.cos(t))), 300+(float)(imperfectR * Math.sin(t)));
t+=0.10;
imperfectR=0.5*((2*points[i+1].y)+(-points[i].y+points[i+2].y)*t+(2*points[i].y-5*points[i+1].y+4*points[i+2].y-points[i+3].y)*(t*t)+((-points[i].y+3*points[i+1].y-3*points[i+2].y+points[i+3].y)*(t*t*t)));
newPoints[i+1].set((300+(float)(imperfectR * Math.cos(t))), 300+(float)(imperfectR * Math.sin(t)));
t+=0.10;
imperfectR=0.5*((2*points[i+1].y)+(-points[i].y+points[i+2].y)*t+(2*points[i].y-5*points[i+1].y+4*points[i+2].y-points[i+3].y)*(t*t)+((-points[i].y+3*points[i+1].y-3*points[i+2].y+points[i+3].y)*(t*t*t)));
newPoints[i+2].set((300+(float)(imperfectR * Math.cos(t))), 300+(float)(imperfectR * Math.sin(t)));
t+=0.10;
imperfectR=0.5*((2*points[i+1].y)+(-points[i].y+points[i+2].y)*t+(2*points[i].y-5*points[i+1].y+4*points[i+2].y-points[i+3].y)*(t*t)+((-points[i].y+3*points[i+1].y-3*points[i+2].y+points[i+3].y)*(t*t*t)));
newPoints[i+3].set((300+(float)(imperfectR * Math.cos(t))), 300+(float)(imperfectR * Math.sin(t)));
if(i==0){
pp.moveTo(newPoints[i].x, newPoints[i].y);
}
pp.lineTo(newPoints[i].x, newPoints[i].y);
pp.lineTo(newPoints[i+1].x, newPoints[i+1].y);
pp.lineTo(newPoints[i+2].x, newPoints[i+2].y);
pp.lineTo(newPoints[i+3].x, newPoints[i+3].y);

}
pp.close();

最佳答案

另一种更容易绘制的圆方程是其参数形式之一:

x = R * cos(t);
y = R * sin(t);

其中 R 是标称半径,t 是介于 02 * pi 之间的参数。因此,您可以像这样在圆周上画点“完美”圆:

for (int step = 0; step < NSTEPS; ++step) {
double t = step / (double) NSTEPS * 2 * pi;
drawPoint(R * cos(t), R * sin(t));
}

按照@nikis 的建议,您可以通过向圆的半径添加随机量来使圆“不完美”:

for (int step = 0; step < NSTEPS; ++step) {
double t = step / (double) NSTEPS * 2 * pi;
double imperfectR = R + randn(); // Normally distributed random
drawPoint(imperfectR * cos(t), imperfectR * sin(t));
}

但是,这可能会给您带来尖尖的形状,因为没有什么可以使 stepstep + 1 处的半径相似。不失一般性,您可以将上面的代码重写为:

for (int step = 0; step < NSTEPS; ++step) {
double t = step / (double) NSTEPS * 2 * pi;
double imperfectR = f(t);
drawPoint(imperfectR * cos(t), imperfectR * sin(t));
}

其中 f(t) 是为参数 t 生成半径的某个函数。现在,您可以为 t 选择任何函数,但您可能希望选择在圆上连续的函数,即 f(t) 没有变化的点值突然。

这里有很多选择。我上面建议的例子是建议使用余弦函数的总和:

double f(double t) {
double f = 0;
for (int i = 0; i < N; ++i) {
f += A[i] * cos(i * t + w[i]);
}
return f;
}

其中Aw是随机选择的值; A[0] 应设置为 R。这里的重点是余弦函数的周期为2 * pi,所以f(alpha) = f(alpha + 2 * pi),满足被连续的。

然而,这远非唯一的选择。您也许可以选择像高斯内核之和这样的东西,它将“凸起”放置在以 w[i] 为中心的位置,并在 sigma[i]圆圈:

double f(double t) {
double f = 0;
for (int i = 0; i < N; ++i) {
f += A[i] * exp(-Math.pow(t-w[i], 2) / sigma[i]);
}
return f;
}

(这不太行,它不处理 t 的环绕)

您需要尝试一下,看看什么样的函数和什么样的随机选择值可以为您提供所需的形状。

关于java - 计算不完美的圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29254223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com