python - Python 中的 Epitrocoid 没有给出正确的情节
问题描述
我在 C# 中编写了以下代码:
using System;
using System.Drawing;
using ZedGraph;
namespace _1_12_Epitrocoid
{
class Epitrocoid
{
double A = 1.0;
double a = 0.4;
double λ = 1.4;
public double X(double ϕ)
{
return (A + a) * Math.Cos(ϕ) - λ * a * Math.Cos(((A + a) / a) * ϕ);
}
public double Y(double ϕ)
{
return (A + a) * Math.Sin(ϕ) - λ * a * Math.Sin(((A + a) / a) * ϕ);
}
}
class Program
{
static void Main(string[] args)
{
Epitrocoid e = new Epitrocoid();
PointPairList list = new PointPairList();
for (double ϕ = 0; ϕ < 10; ϕ += 0.01)
{
double x = e.X(ϕ);
double y = e.Y(ϕ);
list.Add(x, y);
}
PlotForm f = new PlotForm("Epitrocoid");
f.Add(list, "Epitrocoid", Color.Black);
f.AxisChange();
f.ShowDialog();
Console.ReadLine();
}
}
}
我将此源代码转换为 Python,如下所示:
import math
import matplotlib.pyplot as plt
A = 1.0;
a = 0.4;
λ = 1.4;
def X(ϕ):
return (A + a) * math.cos(ϕ) - λ * a * math.cos(((A + a) / a) * ϕ);
def Y(ϕ):
return (A + a) * math.sin(ϕ) - λ * a * math.sin(((A + a) / a) * ϕ);
x_list = []
y_list = []
for i in range(0, 1001, 1):
ϕ = i / 1000.0
x_list.append(X(ϕ))
y_list.append(Y(ϕ))
print(len(x_list))
print(len(y_list))
plt.plot(x_list, y_list)
有人可以告诉我这里出了什么问题吗?
解决方案
您在 Python 中构建迭代器的方式(最高 phi = 1)与在 C# 中的构建方式不同(最高 phi = 10)。
使用正确的 phi 可以帮助您使用 Python。此外,使用 numpy 可以大大简化事情。
import numpy
from matplotlib import pyplot
A = 1.0;
a = 0.4;
λ = 1.4;
def X(ϕ):
return (A + a) * numpy.cos(ϕ) - λ * a * numpy.cos(((A + a) / a) * ϕ);
def Y(ϕ):
return (A + a) * numpy.sin(ϕ) - λ * a * numpy.sin(((A + a) / a) * ϕ);
ϕ = numpy.arange(0, 10, 0.01)
x = X(ϕ)
y = Y(ϕ)
fig, ax = pyplot.subplots()
ax.plot(x, y)
ax.set_aspect('equal')
推荐阅读
- javascript - 未从 omdbapi.com 获取数据
- python - 根据一帧的行值和另一帧的列值合并两个数据帧
- javascript - 一起注释掉 HTML、CSS、Javascript 代码
- sql-server - Entity Framework 复杂搜索功能
- android - 我正在为 IOS 和 android 移动应用程序创建测试用例脚本
- python - 发送带有 python 错误“连接意外关闭”的电子邮件
- java - 关于 ParallelStream 列表的潜在竞争条件的问题
- git - 在什么情况下 git 替换会被推/拉
- react-native - 自定义导航栏 - 不同场景下的不同图像
- typescript - 在命名空间扩充中使用模块中的类型