當前位置:首頁 » 編程軟體 » 編程畫曲線

編程畫曲線

發布時間: 2024-03-29 17:48:59

『壹』 請教:如何用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語言描述即可,很多畫圖問題可以使用這種方法。

  1. 假設屏幕左上角為坐標軸原點



#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;
}

熱點內容
中心機編程 發布:2024-04-27 17:00:11 瀏覽:116
lms濾波演算法 發布:2024-04-27 16:55:37 瀏覽:444
蘋果電腦遠程桌面連接伺服器 發布:2024-04-27 16:53:08 瀏覽:234
為什麼安卓手機沒有回響 發布:2024-04-27 16:53:08 瀏覽:375
搭建郵箱中繼伺服器 發布:2024-04-27 16:40:42 瀏覽:198
我的世界的神奇寶可夢伺服器 發布:2024-04-27 16:28:28 瀏覽:589
君威高配有哪些配置 發布:2024-04-27 16:27:54 瀏覽:199
安卓九彩蛋如何換顏色 發布:2024-04-27 16:10:36 瀏覽:505
安卓711如何打開隱藏彩蛋 發布:2024-04-27 16:04:53 瀏覽:814
寫一個腳本讓電腦按時自動關機 發布:2024-04-27 16:00:06 瀏覽:930