各位在线的,帮我看一下关于动态数据显示的程序 点击:936 | 回复:1



唐菲

    
  • 精华:0帖
  • 求助:0帖
  • 帖子:9帖 | 25回
  • 年度积分:0
  • 历史总积分:52
  • 注册:2004年9月14日
发表于:2004-10-14 20:27:00
楼主
同时画两条动态正弦曲线,为什么PaintBox2上的曲线比PaintBox1上的多一条横线,代码完全一样,唯一的区别是:PaintBox1上的曲线的数组定义在前 如果我把PaintBox2上的曲线的数组定义在前,PaintBox1上的曲线比PaintBox2上的多一条横线 谁能帮我解释一下,多谢了 下面是源代码 //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" #include "math.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; float cx1,cy1,cx2,cy2,cn,scy1,scy2; TPoint polyLine[1025]; TPoint polyLine1[1025]; //存放动态数据曲线数据值的数组 static int pLinePoints = 0; //当前需绘制曲线的点数 static int i=0; static int col = 0; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { cx1 = 0; cy1 = (PaintBox1->Left+PaintBox1->Width)/4+10; scy1 = (PaintBox2->Left+PaintBox2->Width)/4+30; polyLine[0].x = cx1; polyLine[0].y = cy1; polyLine1[0].x = cx1; polyLine1[0].y = scy1; cn = 0; Timer1->Enabled = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender) { if(pLinePoints >= 2) //用背景色重绘原曲线,使之产生动画效果 { PaintBox1->Canvas->Pen->Color = clBlack; PaintBox2->Canvas->Pen->Color = clBlack; PaintBox1->Canvas->Polyline(polyLine, pLinePoints-1); PaintBox2->Canvas->Polyline(polyLine1, pLinePoints-1); } //用来存放当前需要显示的实时数据值 if(col < 1025) { col++; cx2 = cx2+3.1415926/12; cy2 = cy1-40*sin(cn); scy2 = scy1-30*sin(cn); polyLine[col].x = cx2; polyLine[col].y = cy2; polyLine1[col].x = cx2; polyLine1[col].y = scy2; pLinePoints++; cn = cn+3.1415926/180; } else //当数组装满时顺次前移,将数组第一个元素丢掉 { pLinePoints = 1025; for(int t=0; t<1024; t++) { polyLine[t].y = polyLine[t+1].y; polyLine1[t].y = polyLine1[t+1].y; } polyLine[1024].y = cy1-40*sin(cn); polyLine1[1024].y = scy1-30*sin(cn); cn = cn+3.1415926/180; } if(pLinePoints >= 2) //当有两个或以上有效数据点时开始画动态数据曲线 { PaintBox1->Canvas->Pen->Color = clGreen; PaintBox1->Canvas->Polyline(polyLine, pLinePoints-1); PaintBox2->Canvas->Pen->Color = clRed; PaintBox2->Canvas->Polyline(polyLine1, pLinePoints-1); } } //---------------------------------------------------------------------------



唐菲

  • 精华:0帖
  • 求助:0帖
  • 帖子:9帖 | 25回
  • 年度积分:0
  • 历史总积分:52
  • 注册:2004年9月14日
发表于:2004-10-15 11:14:00
1楼
不好意思了,是由数组越界引起的

热门招聘
相关主题

官方公众号

智造工程师