极坐标点生成函数上界不是2Pi for theta吗?
Polar coordinate point generation function upper bound is not 2Pi for theta?
所以我编写了以下函数来获取一个框架和极坐标函数,并通过在该框架内生成笛卡尔坐标来绘制它。这是代码。
func cartesianCoordsForPolarFunc(frame: CGRect, thetaCoefficient:Double, cosScalar:Double, iPrecision:Double, largestScalar:Double) -> Array<CGPoint> {
// Frame: The frame in which to fit this curve.
// thetaCoefficient: The number to scale theta by in the cos.
// cosScalar: The number to multiply the cos by.
// largestScalar: Largest cosScalar used in this frame so that scaling is relative.
// iPrecision: The step for continuity. 0 < iPrecision <= 2.pi. Defaults to 0.1
// Clean inputs
var precision:Double = 0.1 // Default precision
if iPrecision != 0 {// Can't be 0.
precision = iPrecision
}
// This is ther polar function
// var theta: Double = 0 // 0 <= theta <= 2pi
// let r = cosScalar * cos(thetaCoefficient * theta)
var points:Array<CGPoint> = [] // We store the points here
for theta in stride(from: 0, to: Double.pi * 2 , by: precision) { //TODO: Try to recreate continuity. WHY IS IT NOT 2PI
let x = cosScalar * cos(thetaCoefficient * theta) * cos(theta) // Convert to cartesian
let y = cosScalar * cos(thetaCoefficient * theta) * sin(theta) // Convert to cartesian
// newvalue = (max'-min')/(max-min)*(value-max)+max'
let scaled_x = (Double(frame.width) - 0)/(largestScalar*2)*(x-largestScalar)+Double(frame.width) // Scale to the frame
let scaled_y = (Double(frame.height) - 0)/(largestScalar*2)*(y-largestScalar)+Double(frame.height) // Scale to the frame
points.append(CGPoint(x: scaled_x, y:scaled_y)) // Add the result
}
print("Done points")
return points
}
我传递的极坐标函数是 r = 100*cos(9/4*theta)
,看起来像这样。
我想知道为什么当 theta 从 0 变为 2 时我的函数 returns 如下。(请注意,我在这张图片中画了不同大小的花朵,因此图案重复)
如您所见,这是错误的。奇怪的是,当 theta 从 0 变为 2Pi*100(也适用于其他随机值,例如 2Pi*4、2Pi*20 但不适用于 2Pi*2 或 2Pi*10)时,它起作用了,我明白了。
这是为什么?域不是0到2Pi吗?我注意到当达到 2Pi*100 时它会重新绘制一些花瓣所以有一个限制,但它是什么?
PS:这里的精度是 0.01(足以表现得像它是连续的)。在我的图片中,我绘制了不同大小和重叠的函数(最后一张图片有 2 个内花)。
不,域不会是 2π。将代码设置为缓慢绘制,每 2π 花费 2 秒,然后观察。它做了一系列完整的圆圈,每次局部最大值和最小值都落在不同的点。那就是你的花瓣。看起来你的公式在 8π 之后重复。
看起来周期是theta系数* 2π的分母。你的 theta 系数是 9/4,分母是 4,所以系数是 4*2π,即 8π。
(这是基于玩 Wolfram Alpha 并观察结果。我可能是错的。)
所以我编写了以下函数来获取一个框架和极坐标函数,并通过在该框架内生成笛卡尔坐标来绘制它。这是代码。
func cartesianCoordsForPolarFunc(frame: CGRect, thetaCoefficient:Double, cosScalar:Double, iPrecision:Double, largestScalar:Double) -> Array<CGPoint> {
// Frame: The frame in which to fit this curve.
// thetaCoefficient: The number to scale theta by in the cos.
// cosScalar: The number to multiply the cos by.
// largestScalar: Largest cosScalar used in this frame so that scaling is relative.
// iPrecision: The step for continuity. 0 < iPrecision <= 2.pi. Defaults to 0.1
// Clean inputs
var precision:Double = 0.1 // Default precision
if iPrecision != 0 {// Can't be 0.
precision = iPrecision
}
// This is ther polar function
// var theta: Double = 0 // 0 <= theta <= 2pi
// let r = cosScalar * cos(thetaCoefficient * theta)
var points:Array<CGPoint> = [] // We store the points here
for theta in stride(from: 0, to: Double.pi * 2 , by: precision) { //TODO: Try to recreate continuity. WHY IS IT NOT 2PI
let x = cosScalar * cos(thetaCoefficient * theta) * cos(theta) // Convert to cartesian
let y = cosScalar * cos(thetaCoefficient * theta) * sin(theta) // Convert to cartesian
// newvalue = (max'-min')/(max-min)*(value-max)+max'
let scaled_x = (Double(frame.width) - 0)/(largestScalar*2)*(x-largestScalar)+Double(frame.width) // Scale to the frame
let scaled_y = (Double(frame.height) - 0)/(largestScalar*2)*(y-largestScalar)+Double(frame.height) // Scale to the frame
points.append(CGPoint(x: scaled_x, y:scaled_y)) // Add the result
}
print("Done points")
return points
}
我传递的极坐标函数是 r = 100*cos(9/4*theta)
,看起来像这样。
我想知道为什么当 theta 从 0 变为 2 时我的函数 returns 如下。(请注意,我在这张图片中画了不同大小的花朵,因此图案重复)
如您所见,这是错误的。奇怪的是,当 theta 从 0 变为 2Pi*100(也适用于其他随机值,例如 2Pi*4、2Pi*20 但不适用于 2Pi*2 或 2Pi*10)时,它起作用了,我明白了。
这是为什么?域不是0到2Pi吗?我注意到当达到 2Pi*100 时它会重新绘制一些花瓣所以有一个限制,但它是什么?
PS:这里的精度是 0.01(足以表现得像它是连续的)。在我的图片中,我绘制了不同大小和重叠的函数(最后一张图片有 2 个内花)。
不,域不会是 2π。将代码设置为缓慢绘制,每 2π 花费 2 秒,然后观察。它做了一系列完整的圆圈,每次局部最大值和最小值都落在不同的点。那就是你的花瓣。看起来你的公式在 8π 之后重复。
看起来周期是theta系数* 2π的分母。你的 theta 系数是 9/4,分母是 4,所以系数是 4*2π,即 8π。
(这是基于玩 Wolfram Alpha 并观察结果。我可能是错的。)