编程画曲线
‘壹’ 请教:如何用c语言画曲线
最简单的办法是对区域均匀划分,计算各点的函数值,然后把这些点用直线连接起来。例如,下面是绘制正弦曲线:
moveto(0, 100);
for (i=0; i<=100;i++)
lineto(i, 100 + 100 *sin(M_PI * 2/100.0*i));
但是用直线连接起来不够平滑,也有一些插值方法进行改进。比如采用不等间隔划分区域,或把曲线使用一段段的二次、三次曲线连接起来。你可以参考清华大学出版社出版的《C语言常用算法程序集》
‘贰’ 用c语言写程序,此程序能画出来函数y=2x+5的曲线图形。
此题把数学问题转化为用C语言描述即可,很多画图问题可以使用这种方法。
假设屏幕左上角为坐标轴原点
#include<stdio.h>
#defineMAX30//选定曲线要显示的范围,因为一次函数是一条直线,所以它的显示范围无穷
intmain()
{
intx,y;
for(y=30;y>=0;y--)
{
for(x=0;x<MAX;x++)
{
if(y==2*x+5)
{
printf("*");
}
else
{
printf("");//空格,不满足y=2x+5的点
}
}
printf("
");//x轴已经到显示范围,所以需要换行。
}
return0;
}
‘叁’ matlab里一次画两条曲线怎么编程
利用hold
on命名即可实现在同一图形中绘制多条曲线或利用绘图命令一次绘制多条曲线。
hold
on就是将figure中的图形保存,之后再通过绘图命令绘制下一条曲线,使多条曲线同时显示在一个图形之中。
具体实现方法可以参考如下程序段:
t = -5 : 0.01 : 5;
x = sin(t);
y = cos(t);
plot(t, x); // 绘制正弦曲线
hold on; // 将正弦曲线保持在图形中
plot(t, y); // 绘制余弦曲线,完成后图形中就会同时显示正弦曲线和余弦曲线
plot(t,x, t,y); // 同时绘制正弦曲线和余弦曲线,该方法也是在图形中同时显示正弦曲线和余弦曲线
‘肆’ 镐庢牱鐢–++缂栫▼缁桦埗y=sinx 镄勪竴娈垫洸绾匡纻
涓庝綘𨰾ユ湁镄勮蒋浠舵湁鍏炽傜粯镟茬嚎镄勫叧阌鏄璁$畻鍑烘洸绾跨殑x,y鍧愭爣锛岀劧钖庝袱镣硅繛涓绾裤
涓嬮溃鏄鐢∕S VC++ API 鍐欑殑缁樻洸绾跨▼搴忥纴鐢绘椂搴忔洸绾匡纴鍙缂╂斁锛岀敾搴ф爣鍒诲害..锛岀幇鍒犲幓浜嗕竴浜涘姛鑳斤纸链鍒犲噣锛夛纴渚涘弬钥冦
缂栬疟镞惰嫢链夎﹀憡淇℃伅锛屽彲浠ユ垨鐣ャ
璁绬0=0;dx=0.01;1000镣广
#include <afxwin.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <Winuser.h>
#pragma comment (lib, "LIBCMT.LIB")
#define DEBUG 0
HWND hWndMain;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow);
void OnDisplayMessage(char *str);
void set_n_scale_y(int *ndiv, float ymin, float ymax, float *yu, float *yd, float *dy);
void set_n_scale_x(int *ndiv, float t0, float te, float *td, float *tu,float *draw_dt );
static float *yt,rx,ry,y_min,y_max,y_ran,y_shift;
static float draw_yu,draw_yd,draw_dy,draw_min,draw_max;
static float t0=0.0,dt=1.0,draw_tu,draw_td,draw_dt;
int flag_t0=0;
float bt_scale_1=1.0;
int draw_n=4,draw_nt=6;
char pname[40],namein[50],str[120],para2[10],para3[10],para4[10];;
char one_line[80];
int len,NN;
LPTSTR argv;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
MSG Msg;
int i;
//get cmd line
argv = GetCommandLine();
len = strlen(argv);
sprintf(one_line,"%s",argv);
i = sscanf(one_line,"%s %s %s %s %s",&pname[0],&namein[0],¶2[0],¶3[0],¶4[0]);
///===========
t0 = 0;
dt = 0.02;
draw_nt=6;
NN = 1000;
yt = (float *) malloc(NN * sizeof(float));
for (i=0;i<NN;i++) yt[i] = sin(t0+dt*i);
y_min=y_max=yt[0];
for (i=0;i<NN;i++){
if (yt[i] < y_min) y_min=yt[i];
if (yt[i] > y_max) y_max=yt[i];
};
y_ran = y_max - y_min;
ry = 150.0 / y_ran;
for (i=0;i<NN;i++) yt[i]= yt[i] - y_min;
y_shift = 180 ;
draw_max = y_max;
draw_min = y_min;
(void) set_n_scale_y(&draw_n, draw_min, draw_max, &draw_yu, &draw_yd, &draw_dy);
if (DEBUG == 1){
sprintf(str,"yd=%f yu=%f dy=%f y_max=%f y_min=%f n=%d", draw_yd,draw_yu,draw_dy,y_max,y_min,draw_n);
OnDisplayMessage(str);
};
(void) set_n_scale_x(&draw_nt, t0, t0+ dt*(NN), &draw_td, &draw_tu,&draw_dt);
if (DEBUG == 1){
sprintf(str,"td=%f tu=%f dt=%f n=%d", draw_td,draw_tu,draw_dt,draw_nt);
OnDisplayMessage(str);
};
rx = 900.0 / (draw_tu - draw_td);
if(!InitWindowsClass(hInstance))
return FALSE;
if(!InitWindows(hInstance,nCmdShow))
return FALSE;
ShowWindow(hWndMain,nCmdShow);
UpdateWindow(hWndMain);
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static long nXChar,nYChar;
HDC hdc;
short x;
TEXTMETRIC tm;
PAINTSTRUCT ps;
COLORREF color;
HFONT font;
HPEN hP1; // pen
// CPoint aP,mousePos;
int i;
switch(message)
{
case WM_LBUTTONDOWN:
bt_scale_1 = bt_scale_1 + 0.05;
ry=ry*bt_scale_1;
y_shift = y_shift * bt_scale_1;
ShowWindow(hwnd, SW_HIDE);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
return 0;
case WM_RBUTTONDOWN:
ry=ry/bt_scale_1;
y_shift = y_shift / bt_scale_1;
ShowWindow(hwnd, SW_HIDE);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
return 0;
case WM_CREATE:
ScrollWindow(hwnd, 0, -800, NULL, NULL);
hdc=GetDC(hwnd);
GetTextMetrics(hdc,&tm);
nXChar=tm.tmAveCharWidth;
nYChar=tm.tmHeight;
ReleaseDC(hwnd,hdc);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps); // begin paint
SetWindowExtEx(hdc,1200,800,NULL);
SetViewportExtEx(hdc,1200,800*bt_scale_1,NULL);
SetViewportOrgEx(hdc,100,150,NULL);
// draw grids
color=RGB(0,128,128);
hP1=CreatePen(PS_SOLID,0,color);
SelectObject(hdc,hP1);
MoveToEx( hdc,0, y_shift-(draw_yu-y_min)*ry, NULL );
LineTo(hdc,900,y_shift-(draw_yu-y_min)*ry);
LineTo(hdc,900,y_shift-(draw_yd-y_min)*ry);
LineTo(hdc,0,y_shift-(draw_yd-y_min)*ry);
LineTo(hdc,0,y_shift-(draw_yu-y_min)*ry);
for (i=0;i<=draw_n;i++)
{
MoveToEx( hdc, 0, y_shift-(draw_yd -y_min + i* draw_dy)*ry, NULL );
LineTo( hdc, 900, y_shift-(draw_yd -y_min + i * draw_dy)*ry );
}
for (i=1;i<draw_nt;i++) {
MoveToEx( hdc, 900 * i /draw_nt, y_shift-(draw_yd -y_min)*ry, NULL );
LineTo( hdc, 900 * i /draw_nt, y_shift-(draw_yu -y_min)*ry );
}
// draw curve
color=RGB(255,0,0);
hP1=CreatePen(PS_SOLID,0,color);
SelectObject(hdc,hP1);
MoveToEx( hdc, (t0-draw_td)* rx, y_shift-yt[0]*ry, NULL );
for (i=1;i<NN ;i++){
LineTo(hdc,(t0+i*dt-draw_td)*rx, y_shift-yt[i]*ry);
}
font=CreateFont(
24,10,0,0, FW_NORMAL,0,0,0, ANSI_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,NULL,"myfont"
);
SelectObject(hdc,font);
GetTextMetrics(hdc,&tm);
nYChar=tm.tmHeight;
color=RGB(0,0,0);
for (i=0;i<=draw_n;i++)
{
sprintf(one_line,"%.2f",draw_yd +i * draw_dy);
TextOut(hdc,-60,y_shift-(draw_yd-y_min + i* draw_dy)*ry-5,one_line,strlen(one_line));
}
for (i=0;i<=draw_nt;i++)
{
sprintf(one_line,"%.1f",draw_td +i * draw_dt);
TextOut(hdc,900 * i /draw_nt-30,y_shift-(draw_yd -y_min)*ry+20, one_line,strlen(one_line));
}
sprintf(one_line,"%s N=%d",namein,NN);
TextOut(hdc,20,y_shift-(draw_yu-y_min )*ry-40,one_line,strlen(one_line));
EndPaint(hwnd,&ps); // end paint
return 0L;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
// wndclass.hIcon=LoadIcon(NULL,"END");
wndclass.hIcon=LoadIcon(hInstance,"uni03.ico");
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName="Windows Fill";
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
return(RegisterClass(&wndclass));
}
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow)
{
HWND hWnd;
char str[100];
sprintf(str,"%s %s",pname,namein);
hWnd=CreateWindow(
"Windows Fill",
str,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL
);
if(!hWnd)
return FALSE;
hWndMain=hWnd;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
void OnDisplayMessage(char *str)
{
MessageBox(NULL,str,"msg",MB_OK | MB_ICONINFORMATION);
}
void set_n_scale_y(int *ndiv, float ymin, float ymax, float *yu, float *yd, float *dy)
{
int n ;
float yuu,ydd;
float yr;
if (*ndiv <= 0) {n = 2;} else n = *ndiv;
yr = ymax - ymin;
if ( yr >= 0.0001 && yr < 0.001) {
yuu = (float) ((int) (ymax * 10000.0)) / 10000.0f + 0.0001f;
ydd = (float) ((int) (ymin * 10000.0)) / 10000.0f - 0.0001f;
}
else if (yr >= 0.001 && yr < 0.01) {
yuu = (float) ((int) (ymax * 1000.0)) / 1000.0f + 0.001f;
ydd = (float) ((int) (ymin * 1000.0)) / 1000.0f - 0.001f;
}
else if ( yr >= 0.01 && yr < 0.1) {
yuu = (float) ((int) (ymax * 100.0)) / 100.0f + 0.01f;
ydd = (float) ((int) (ymin * 100.0)) / 100.0f - 0.01f;
}
else if ( yr >= 0.1 && yr < 1.0) {
yuu = (float) ((int) (ymax * 10.0)) / 10.0f + 0.1f;
ydd = (float) ((int) (ymin * 10.0)) / 10.0f - 0.1f;
}
else if ( yr >= 1.0 && yr < 10.0) {
yuu = (float) ((int) (ymax)) + 1.0f;
ydd = (float) ((int) (ymin)) - 1.0f;
}
else if ( yr >= 10.0 && yr < 100.0) {
yuu = (float) ((int) (ymax * 0.1)) / 0.1f + 10.0f;
ydd = (float) ((int) (ymin * 0.1)) / 0.1f - 10.0f;
}
else if ( yr >= 100.0 && yr < 1000.0) {
yuu = (float) ((int) (ymax * 0.01)) / 0.01f + 100.0f;
ydd = (float) ((int) (ymin * 0.01)) / 0.01f - 100.0f;
}
else if ( yr >= 1000.0 && yr < 10000.0) {
yuu = (float) ((int) (ymax * 0.001)) / 0.001f + 1000.0f;
ydd = (float) ((int) (ymin * 0.001)) / 0.001f - 1000.0f;
}
else if ( yr >= 10000.0 && yr < 100000.0) {
yuu = (float) ((int) (ymax * 0.0001)) / 0.0001f + 10000.0f;
ydd = (float) ((int) (ymin * 0.0001)) / 0.0001f - 10000.0f;
} else {
yuu = ymax; ydd = ymin;
};
if (yuu == ydd) {
yuu = ydd * 1.1f;
ydd = ydd * 0.9f;
};
*yu = yuu; *yd = ydd;
*dy = (yuu - ydd) / n;
if (*dy > 1.0) *dy = (float) ((int) *dy );
*ndiv = (yuu - ydd)/ (*dy) ;
}
void set_n_scale_x(int *ndiv, float t0, float te, float *td, float *tu,float *draw_dt )
{
int i;
int n ;
double tr,dt,d;
if (*ndiv <= 0) {n = 6;} else n = *ndiv;
tr = (double) (te - t0);
dt = tr / (double) n;
if (dt >= 1.0 ){d = (double) ((int) (dt + 0.9));}
else { d = dt;};
i = (int)( (double) t0 / d);
if (t0 < 0) i=i-1;
// if ( (float)(d * i) < t0) i = i + 1;
*td = (float) (d * i);
*ndiv = (int) ((te - *td) / d);
if ( *ndiv * d < (te - *td) ) *ndiv= *ndiv+1;
*tu = *td + *ndiv * d;
*draw_dt = (float) d;
}
‘伍’ C语言编程 绘制曲线,会的来
TC下调试通过
/*******************************************************
*Author :Wacs5
*Date :20090105(YYYY-MM-DD)
*Function :画简易的曲线图 *********************************************************/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <Graphics.h>
#define n 8
int main()
{
int i,j;
float data[]={7,3,12,6,9,5,8,11};
char str[40];
int gdrive=DETECT,gmode,errorcode;
int maxx,maxy;
int perx,pery;
int x0,x1,y0,y1;
int coloraxis=2,colorline=3;
float mindata,maxdata,ndata;
initgraph(&gdrive,&gmode,""); /*初始化设备*/
if ((errorcode=graphresult())!=grOk) /*查错*/
{
printf("Graphics error:%s\nPress any key to exit:",grapherrormsg(errorcode));
getch();
exit(1);
}
maxx=getmaxx();
maxy=getmaxy();
mindata=maxdata=data[0];
for (i=1;i<n;i++)
{
if (mindata>data[i])
mindata=data[i];
if (maxdata<data[i])
maxdata=data[i];
}
mindata=floor(mindata);
maxdata=ceil(maxdata);
perx=maxx/(n+4);
pery=maxy/(maxdata-mindata+4);
x0=2*perx;
y0=maxy-2*pery;
x1=maxx-2*perx;
y1=2*pery;
setcolor(coloraxis);
line(x0,y0,x1,y0);
line(x0,y0,x0,y1);
line(x1,y0,x1-4,y0+3);
line(x1,y0,x1-4,y0-3);
line(x0,y1,x0+3,y1+4);
line(x0,y1,x0-3,y1+4);
settextjustify(CENTER_TEXT,TOP_TEXT);
for (i=0;i<n;i+=n/3)
{
j=x0+i*perx;
line(j,y0,j,y0+2); /*刻度线*/
sprintf(str,"%d",i);
outtextxy(j,y0+4,str);
}
settextjustify(RIGHT_TEXT,CENTER_TEXT);
for (i=(maxdata-mindata)/3;i<=maxdata-mindata;i+=(maxdata-mindata)/3)
{
j=y0-i*pery;
line(x0,j,x0-2,j); /*刻度线*/
sprintf(str,"%d",(int)mindata+i);
outtextxy(x0-4,j,str);
}
setcolor(colorline);
x1=x0+perx;
y1=y0-(data[0]-mindata)*pery;
circle(x1,y1,2);
moveto(x1,y1);
i=1;
do
{
x1+=perx;
y1=y0-(data[i]-mindata)*pery;
lineto(x1,y1);
circle(x1,y1,2);
moveto(x1,y1);
i++;
}while(i<n);
getch();
closegraph();
return 0;
}