将一维数组转化成曲线图
譬如我有一个20大小的数组void main()
{
int a[20]={。。。。。。。。。。。};//被赋值
int i;
。。。。。。
} 通过什么方式才能得到以i(0到20)为横坐标,以a[i]为纵坐标的离散点图,或者曲线
或者如何用C++得到曲线图
求大神解答
程序基本看懂了,得到了我想要的曲线 主程序(作图): #include <Windows.h> #include <ctime> #include <conio.h> const int MaxValue(100); const RECT Border = { 20, 20, 620, 350 };//const RECT Border = { 10, 10, 620, 350 }; const POINT Origin = { 20, 170 }; const POINT Sp = { 20, 70 }; #define N 500 struct arrar stepmain(); struct arrar { float y[N]; }; void wmain(void) { float Data[N]; struct arrar p; p=stepmain(); int i; for(i=0;i<N;i++) { Data[i]=p.y[i]; } HDC hDC(GetDC(GetConsoleWindow())); HPEN hPen(CreatePen(PS_SOLID, 1, RGB(233, 19, 7))); HBRUSH hBrush(CreateSolidBrush(RGB(52, 184, 72))); SelectObject(hDC, hPen); SelectObject(hDC, hBrush); Rectangle(hDC, Border.left, Border.top, Border.right, Border.bottom); hPen = CreatePen(PS_SOLID, 1, RGB(54, 54, 182)); SelectObject(hDC, hPen); MoveToEx(hDC, Origin.x, Origin.y, NULL); LineTo(hDC, Border.right, Origin.y); hPen = CreatePen(PS_SOLID, 1, RGB(98, 24, 102)); SelectObject(hDC, hPen); MoveToEx(hDC, Sp.x, Sp.y, NULL); LineTo(hDC, Border.right, Sp.y); hPen = CreatePen(PS_SOLID, 1, RGB(255, 255, 0)); SelectObject(hDC, hPen); MoveToEx(hDC, Origin.x, Origin.y - Data[0], NULL); for (size_t i = 1; i != _countof(Data); ++i) { LineTo(hDC, Origin.x + i * 0.7, Origin.y - Data[i]); } _getwch(); DeleteObject(hBrush); DeleteObject(hPen); ReleaseDC(NULL, hDC); } 子程序(提供Data[]数据): #include<stdio.h> #include<conio.h> #include<math.h> #include<stdlib.h> #define ts 0.001 #define N 500 #define pi 3.14 float yd(int i,int s); typedef struct arrar { float y[N]; }; struct arrar stepmain() { arrar data; float den[4]={1.0000,-2.9063,2.8227,-0.9164}; float num[4]={ 0,0.0853*1.0e-003,0.3338*1.0e-003,0.0817*1.0e-003}; float kp,ki,kd; float cu[3]={0,0,0}; float cy[3]={0,0,0}; float x[3]={0,0,0}; float error_1=0; float u[N],error[N]; int s,i; printf("输入数字1为step signal、2为square wave signal\n"); scanf("%d",&s); if((s<1)||(s>3)) { printf("请按要求输入1-2的整数,选择跟踪信号类型\n"); return data; } if(s==1) { kp=0.5;ki=0.001;kd=0.001; } else { kp=0.5;ki=0.001;kd=0.001; } for(i=0;i<N;i++) { u[i]=kp*x[0]+kd*x[1]+ki*x[2]; u[i]=(u[i]>10?10:u[i]); u[i]=(u[i]<-10?-10:u[i]); data.y[i]=-den[1]*cy[0]-den[2]*cy[1]-den[3]*cy[2]+num[1]*cu[0]+num[2]*cu[1]+num[3]*cu[2]; //printf("y[%d]=%f u[%d]=%f\n",i,y[i],i,u[i]); error[i]=yd(i,s)-data.y[i]; cu[2]=cu[1];cu[1]=cu[0];cu[0]=u[i]; cy[2]=cy[1];cy[1]=cy[0];cy[0]=data.y[i]; x[0]=error[i]; x[1]=(error[i]-error_1)/ts; x[2]+=error[i]*ts; error_1=error[i]; } return data; } float yd(int i,int s) { float y; if(s==1) { y=100; return y; } else { y=100*sin(8*pi*i*ts); return y; } }