- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以去年我用 C 语言为我的类(class)编写了一个程序,该程序使用 Lorenz 方程和 Runge Kutta 方法求解微分方程来演示混沌。
我最近决定重新审视这个问题并制作一个能够绘制出粒子轨迹的程序。我让它成功工作,但现在想扩展它,以便用户可以输入参数,例如粒子的起始位置和其他参数(在我的例子中是 a、b 和 r)。目前我在程序运行后立即绘制轨迹,但我想延迟此操作,直到用户将他们的参数输入某些文本框然后按下按钮。为此,我想我应该创建一个新类并将我当前的代码放入其中,然后在 btn1_Click 方法下的主 .cs 文件中调用它。但是,我在这方面遇到了很大的麻烦,主要是因为我真的不知道该怎么做。到目前为止,在我的最佳尝试中,我对涉及“createGraphics()”的行有一个错误,即类文件中没有它的定义。我在类顶部的使用部分的顶部有所有相同的引用,就像在主文件中一样工作正常。
此外,如果有人可以给我任何关于我的代码的反馈(即任何不好的做法或我使事情过于复杂的地方)或任何让它变得更好的建议,我将非常感激,如果您需要更多信息由我来帮助,那么我会尽力回答!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Lorenz_chaos
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
double a = 10, b = (8 / 3), r = 28; //standard values for lorenz model
/*m defines the number of iterations of the for loop so the number of lines drawn
good idea to keep m inversely proportional to dt (the time interval). A smaller dt will
mean smaller lines so smoother overall drawing m=50000 and dt=0.0005 is a good starting point
that demonstrates chaos well*/
double m = 500000, dt = 0.00005;
//EVOLUTION VALUE FOR RUNGE_KUTTA METHOD
//values for first particle
double y11, y12, y13;
double y21, y22, y23;
double y31, y32, y33;
double y41, y42, y43;
double y51, y52, y53;
double xi, yi, xf, yf; //coordinates for drawing particle 1 trajectory
double f10, f11, f12, f13; //function values to be calculated,
double f20, f21, f22, f23; //for fxy (x>1) these are intermediate fn calculations at different
double f30, f31, f32, f33; //times in Runga Kutta
//values for second particle
double z11, z12, z13;
double z21, z22, z23;
double z31, z32, z33;
double z41, z42, z43;
double z51, z52, z53;
double ai, bi, af, bf; //coordinates for drawing particle 2 trajectory (these are badly named...)
double g10, g11, g12, g13; //equivalent of f values for particle 2
double g20, g21, g22, g23;
double g30, g31, g32, g33;
//OTHER NEEDED QUANTITIES
int i; //for loop iteration integer
int k1 = 20; //scaling factors to make drawing fill form
int k2 = 9;
int y0 = 450; //offset values to centre drawing on form
int x0 = 550;
int start = 10; //starting position for calculations
double diff = 0.01;//initial displacement between two particles
//starting positions for particles
y11 = start;//particle 1
y12 = start;
y13 = start;
z11 = start + diff;//particle 2
z12 = start + diff;
z13 = start + diff;
//initial coords for particles at t=0
xi = (y11) * k1 + x0;
yi = (y12) * k2 + y0;
ai = (z11) * k1 + x0;
bi = (z12) * k2 + y0;
for (i = 1; i <= m; i++)
{
f10 = a * (y12 - y11);
f20 = r * y11 - y12 - y11 * y13;
f30 = y11 * y12 - b * y13;
y21 = y11 + f10 * dt / 2; //finding y1 y2 y3 at the first
y22 = y12 + f20 * dt / 2; //fraction of dt
y23 = y13 + f30 * dt / 2;
f11 = a * (y22 - y21);
f21 = r * y21 - y22 - y21 * y23;
f31 = y21 * y22 - b * y23;
y31 = y11 + f11 * dt / 2; //finding y1 y2 y3 at the second
y32 = y12 + f21 * dt / 2; //fraction of dt
y33 = y13 + f31 * dt / 2;
f12 = a * (y32 - y31);
f22 = r * y31 - y32 - y31 * y33;
f32 = y31 * y32 - b * y33;
y41 = y11 + f12 * dt; //finding y1 y2 y3 at the third
y42 = y12 + f22 * dt; //fraction of dt
y43 = y13 + f32 * dt;
f13 = a * (y42 - y41);
f23 = r * y41 - y42 - y41 * y43;
f33 = y41 * y42 - b * y43;
y51 = y11 + (f10 + 2 * f11 + 2 * f12 + f13) * dt / 6; //final y values at y(t+dt)
y52 = y12 + (f20 + 2 * f21 + 2 * f22 + f23) * dt / 6; //then to be repesated in for loop for all steps
y53 = y13 + (f30 + 2 * f31 + 2 * f32 + f33) * dt / 6;
xf = (y51) * k1 + x0;
yf = (y52) * k2 + y0;
//second particle calculation
g10 = a * (z12 - z11);
g20 = r * z11 - z12 - z11 * z13;
g30 = z11 * z12 - b * z13;
z21 = z11 + g10 * dt / 2; //finding y1 y2 y3 at the first
z22 = z12 + g20 * dt / 2; //fraction of dt
z23 = z13 + g30 * dt / 2;
g11 = a * (z22 - z21);
g21 = r * z21 - z22 - z21 * z23;
g31 = z21 * z22 - b * z23;
z31 = z11 + g11 * dt / 2; //finding y1 y2 y3 at the second
z32 = z12 + g21 * dt / 2; //fraction of dt
z33 = z13 + g31 * dt / 2;
g12 = a * (z32 - z31);
g22 = r * z31 - z32 - z31 * z33;
g32 = z31 * z32 - b * z33;
z41 = z11 + g12 * dt; //finding y1 y2 y3 at the third
z42 = z12 + g22 * dt; //fraction of dt
z43 = z13 + g32 * dt;
g13 = a * (z42 - z41);
g23 = r * z41 - z42 - z41 * z43;
g33 = z41 * z42 - b * z43;
z51 = z11 + (g10 + 2 * g11 + 2 * g12 + g13) * dt / 6; //final y values at y(t+dt)
z52 = z12 + (g20 + 2 * g21 + 2 * g22 + g23) * dt / 6; //then to be repesated in for loop for all steps
z53 = z13 + (g30 + 2 * g31 + 2 * g32 + g33) * dt / 6;
af = (z51) * k1 + x0;
bf = (z52) * k2 + y0;
//DRAWING LINE JUST CALCULATED
System.Drawing.Graphics graphicsObj;
graphicsObj = this.CreateGraphics();
Pen myPen = new Pen(System.Drawing.Color.Red, 1);
//myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot;
graphicsObj.DrawLine(myPen, (int)xi, (int)yi, (int)xf, (int)yf);
myPen.Color = System.Drawing.Color.RoyalBlue;
graphicsObj.DrawLine(myPen, (int)ai, (int)bi, (int)af, (int)bf);
//REDEFINING COORDS AND VALUES FOR NEXT LOOP
//first particle
xi = (y51) * k1 + x0;
yi = (y52) * k2 + y0;
y11 = y51;
y12 = y52;
y13 = y53;
//second particle
ai = (z51) * k1 + x0;
bi = (z52) * k2 + y0;
z11 = z51;
z12 = z52;
z13 = z53;
/*even at 1 the below makes the program far too slow, need an alternative
intention was for it to allow user to see the particle trajectories better*/
//System.Threading.Thread.Sleep(1);
}
}
}
}
最佳答案
将 Form1_Paint
的代码放在一个单独的方法中,例如 DrawLorenzChaos(Graphics graphicsObj , double a, double b, double r)
。当您在表单中设置参数时,只需将一些 bool
值设置为 true
。检查代码
private void Form1_Paint(object sender, PaintEventArgs e)
{
if(startDrawing)
DrawLorenzChaos(e.Graphics, aVal, bVal, rVal);
}
此外,在 DrawLorenzChaos 方法中只需删除这两行:
System.Drawing.Graphics graphicsObj;
graphicsObj = this.CreateGraphics();
编辑: 您可以从一开始就尝试此代码,然后您可以逐渐改进它,这就是我的做法(我会添加更好的同步,但基本上就是这样)。为了尝试代码,您需要一个按钮和一个大小为 (1000,1000) 的 PictureBox。请注意,我稍微更改了起始位置。
基本上这里有一个单独的线程在位图上绘制洛伦兹混沌。在单独的线程中绘制每条线后,该位图在 UI 线程中的 PictureBox 上绘制。您有 Mutex 来控制对位图的访问。
public partial class Form1 : Form
{
Bitmap offScrBuff;
Mutex mut;
int index = 0;
public Form1()
{
InitializeComponent();
offScrBuff = new Bitmap(1000, 1000);
mut = new Mutex();
pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
button1.Click += new System.EventHandler(this.button1_Click);
}
void pictureBox1_Paint(object sender, PaintEventArgs e)
{
mut.WaitOne();
e.Graphics.DrawImage(offScrBuff, 0, 0);
mut.ReleaseMutex();
}
void DrawLorenzChaos(double a, double b, double r)
{
//double a = 10, b = (8.0 / 3.0), r = 28; //standard values for lorenz model
/*m defines the number of iterations of the for loop so the number of lines drawn
good idea to keep m inversely proportional to dt (the time interval). A smaller dt will
mean smaller lines so smoother overall drawing m=50000 and dt=0.0005 is a good starting point
that demonstrates chaos well*/
double m = 500000, dt = 0.00005;
//EVOLUTION VALUE FOR RUNGE_KUTTA METHOD
//values for first particle
double y11, y12, y13;
double y21, y22, y23;
double y31, y32, y33;
double y41, y42, y43;
double y51, y52, y53;
double xi, yi, xf, yf; //coordinates for drawing particle 1 trajectory
double f10, f11, f12, f13; //function values to be calculated,
double f20, f21, f22, f23; //for fxy (x>1) these are intermediate fn calculations at different
double f30, f31, f32, f33; //times in Runga Kutta
//values for second particle
double z11, z12, z13;
double z21, z22, z23;
double z31, z32, z33;
double z41, z42, z43;
double z51, z52, z53;
double ai, bi, af, bf; //coordinates for drawing particle 2 trajectory (these are badly named...)
double g10, g11, g12, g13; //equivalent of f values for particle 2
double g20, g21, g22, g23;
double g30, g31, g32, g33;
//OTHER NEEDED QUANTITIES
int i; //for loop iteration integer
int k1 = 20; //scaling factors to make drawing fill form
int k2 = 9;
int y0 = 280; //offset values to centre drawing on form
int x0 = 400;
int start = 10; //starting position for calculations
double diff = 0.01;//initial displacement between two particles
//starting positions for particles
y11 = start;//particle 1
y12 = start;
y13 = start;
z11 = start + diff;//particle 2
z12 = start + diff;
z13 = start + diff;
//initial coords for particles at t=0
xi = (y11) * k1 + x0;
yi = (y12) * k2 + y0;
ai = (z11) * k1 + x0;
bi = (z12) * k2 + y0;
for (i = 1; i <= m; i++)
{
f10 = a * (y12 - y11);
f20 = r * y11 - y12 - y11 * y13;
f30 = y11 * y12 - b * y13;
y21 = y11 + f10 * dt / 2; //finding y1 y2 y3 at the first
y22 = y12 + f20 * dt / 2; //fraction of dt
y23 = y13 + f30 * dt / 2;
f11 = a * (y22 - y21);
f21 = r * y21 - y22 - y21 * y23;
f31 = y21 * y22 - b * y23;
y31 = y11 + f11 * dt / 2; //finding y1 y2 y3 at the second
y32 = y12 + f21 * dt / 2; //fraction of dt
y33 = y13 + f31 * dt / 2;
f12 = a * (y32 - y31);
f22 = r * y31 - y32 - y31 * y33;
f32 = y31 * y32 - b * y33;
y41 = y11 + f12 * dt; //finding y1 y2 y3 at the third
y42 = y12 + f22 * dt; //fraction of dt
y43 = y13 + f32 * dt;
f13 = a * (y42 - y41);
f23 = r * y41 - y42 - y41 * y43;
f33 = y41 * y42 - b * y43;
y51 = y11 + (f10 + 2 * f11 + 2 * f12 + f13) * dt / 6; //final y values at y(t+dt)
y52 = y12 + (f20 + 2 * f21 + 2 * f22 + f23) * dt / 6; //then to be repesated in for loop for all steps
y53 = y13 + (f30 + 2 * f31 + 2 * f32 + f33) * dt / 6;
xf = (y51) * k1 + x0;
yf = (y52) * k2 + y0;
//second particle calculation
g10 = a * (z12 - z11);
g20 = r * z11 - z12 - z11 * z13;
g30 = z11 * z12 - b * z13;
z21 = z11 + g10 * dt / 2; //finding y1 y2 y3 at the first
z22 = z12 + g20 * dt / 2; //fraction of dt
z23 = z13 + g30 * dt / 2;
g11 = a * (z22 - z21);
g21 = r * z21 - z22 - z21 * z23;
g31 = z21 * z22 - b * z23;
z31 = z11 + g11 * dt / 2; //finding y1 y2 y3 at the second
z32 = z12 + g21 * dt / 2; //fraction of dt
z33 = z13 + g31 * dt / 2;
g12 = a * (z32 - z31);
g22 = r * z31 - z32 - z31 * z33;
g32 = z31 * z32 - b * z33;
z41 = z11 + g12 * dt; //finding y1 y2 y3 at the third
z42 = z12 + g22 * dt; //fraction of dt
z43 = z13 + g32 * dt;
g13 = a * (z42 - z41);
g23 = r * z41 - z42 - z41 * z43;
g33 = z41 * z42 - b * z43;
z51 = z11 + (g10 + 2 * g11 + 2 * g12 + g13) * dt / 6; //final y values at y(t+dt)
z52 = z12 + (g20 + 2 * g21 + 2 * g22 + g23) * dt / 6; //then to be repesated in for loop for all steps
z53 = z13 + (g30 + 2 * g31 + 2 * g32 + g33) * dt / 6;
af = (z51) * k1 + x0;
bf = (z52) * k2 + y0;
//DRAWING LINE JUST CALCULATED
mut.WaitOne();
System.Drawing.Graphics graphicsObj = Graphics.FromImage(offScrBuff);
graphicsObj.DrawLine(Pens.Red, (int)xi, (int)yi, (int)xf, (int)yf);
graphicsObj.DrawLine(Pens.RoyalBlue, (int)ai, (int)bi, (int)af, (int)bf);
graphicsObj.Dispose();
mut.ReleaseMutex();
pictureBox1.Invalidate();
//REDEFINING COORDS AND VALUES FOR NEXT LOOP
//first particle
xi = (y51) * k1 + x0;
yi = (y52) * k2 + y0;
y11 = y51;
y12 = y52;
y13 = y53;
//second particle
ai = (z51) * k1 + x0;
bi = (z52) * k2 + y0;
z11 = z51;
z12 = z52;
z13 = z53;
/*even at 1 the below makes the program far too slow, need an alternative
intention was for it to allow user to see the particle trajectories better*/
//System.Threading.Thread.Sleep(1);
}
}
private void button1_Click(object sender, EventArgs e)
{
Task.Factory.StartNew(() => { DrawLorenzChaos(10.0, 8.0 / 3.0, 28); });
}
}
关于c# - Visual Studio,在用户点击按钮时绘制 Lorenz 混沌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13499030/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!