gpt4 book ai didi

math - 如何生成沿椭圆周长均匀分布的一组点?

转载 作者:行者123 更新时间:2023-12-02 16:16:05 24 4
gpt4 key购买 nike

如果我想生成一堆均匀分布在圆周围的点,我可以这样做(python):

r = 5  #radius
n = 20 #points to generate
circlePoints = [
(r * math.cos(theta), r * math.sin(theta))
for theta in (math.pi*2 * i/n for i in range(n))
]

但是,相同的逻辑不会在椭圆上生成均匀的点:“端”上的点比“边”上的点间隔更近。

r1 = 5
r2 = 10
n = 20 #points to generate
ellipsePoints = [
(r1 * math.cos(theta), r2 * math.sin(theta))
for theta in (math.pi*2 * i/n for i in range(n))
]

是否有一种简单的方法可以在椭圆周围生成等距点?

最佳答案

这是一个旧线程,但由于我正在寻求沿椭圆形创建均匀间隔的点的相同任务,并且无法找到实现,因此我提供了实现 Howard 伪代码的 Java 代码:

 package com.math;

public class CalculatePoints {

public static void main(String[] args) {
// TODO Auto-generated method stub

/*
*
dp(t) = sqrt( (r1*sin(t))^2 + (r2*cos(t))^2)
circ = sum(dp(t), t=0..2*Pi step 0.0001)

n = 20

nextPoint = 0
run = 0.0
for t=0..2*Pi step 0.0001
if n*run/circ >= nextPoint then
set point (r1*cos(t), r2*sin(t))
nextPoint = nextPoint + 1
next
run = run + dp(t)
next
*/


double r1 = 20.0;
double r2 = 10.0;

double theta = 0.0;
double twoPi = Math.PI*2.0;
double deltaTheta = 0.0001;
double numIntegrals = Math.round(twoPi/deltaTheta);
double circ=0.0;
double dpt=0.0;

/* integrate over the elipse to get the circumference */
for( int i=0; i < numIntegrals; i++ ) {
theta += i*deltaTheta;
dpt = computeDpt( r1, r2, theta);
circ += dpt;
}
System.out.println( "circumference = " + circ );

int n=20;
int nextPoint = 0;
double run = 0.0;
theta = 0.0;

for( int i=0; i < numIntegrals; i++ ) {
theta += deltaTheta;
double subIntegral = n*run/circ;
if( (int) subIntegral >= nextPoint ) {
double x = r1 * Math.cos(theta);
double y = r2 * Math.sin(theta);
System.out.println( "x=" + Math.round(x) + ", y=" + Math.round(y));
nextPoint++;
}
run += computeDpt(r1, r2, theta);
}
}

static double computeDpt( double r1, double r2, double theta ) {
double dp=0.0;

double dpt_sin = Math.pow(r1*Math.sin(theta), 2.0);
double dpt_cos = Math.pow( r2*Math.cos(theta), 2.0);
dp = Math.sqrt(dpt_sin + dpt_cos);

return dp;
}

}

关于math - 如何生成沿椭圆周长均匀分布的一组点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6972331/

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