怎么用C语言设计画图程序

怎么用C语言设计画图程序,第1张

分类: 电脑/网络 >> 程序设计 >> 其他编程语言

问题描述:

请给给出一个简单的例子

解析:

使用图形函数时要确保有显示器图形驱动程序BGI,同时将集成开发环境options/Linker中的Graphics lib选为on,只有这样才能保证正确使用图形函数。

#include <stdioh>

#include <graphicsh>

main()

{

int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"") ;

line(30,50,500,50);/画一条直线/

circle(200,200,20); /画圆/

getch();

cleardevice();

closegraph();

}

呵呵,哥们,我们也刚学完这些,以下代码给你,要的基本符合,不过你得要在加2个资源(在insert的resource中)“鼠标的(cursor)和菜单栏的(menu)”,不加是不行的,加了后该修改的你能看的懂的,实在不行,我就直接发文件给你:

#include <windowsh>

#include "resourceh"

#include <stringh>

#include <stdioh>

#include <mathh>

HINSTANCE g_hInstance;

int g_nToolID = 0;

struct tagLine

{

POINT ptB;

POINT ptE;

bool bIsDelete;

};

tagLine g_AllLines[100];//直线数组

int g_nLineNumber = 0; //实际直线数

struct tagCircle

{

POINT ptCenter;

int nRadius;

bool bIsDelete;

};

tagCircle g_AllCircles[100];//整圆数组

int g_nCircleNumber = 0; // 实际保存的圆个数

POINT pt1 = {200,200};

POINT pt2 = {200,200};

POINT pt3 = {0,0};

int g_nState = 0;

//功能:画一线直线段

void DrawLine(HDC hDC,POINT ptB,POINT ptE)

{

::MoveToEx(hDC,ptBx,ptBy,NULL);

::LineTo(hDC,ptEx,ptEy);

}

//功能:已知两点求距离

int Distance(POINT pt1,POINT pt2)

{

int nR = 0;

nR=sqrt((pt1x - pt2x)(pt1x - pt2x) +(pt1y - pt2y)(pt1y- pt2y));

return nR;

}

void DrawCircle(HDC hDC,POINT ptC,int nR)

{

::Ellipse(hDC,ptCx - nR,ptCy - nR,ptCx + nR,ptCy + nR);

}

void OnMouseMove(POINT ptPos,HWND hWnd)

{

if(g_nToolID == ID_CIRCLE)

{

if(g_nState == 1)

{

HDC hDC = ::GetDC(hWnd);

//取得系统预定义一个“空心刷子”

HBRUSH hBrush = (HBRUSH)::GetStockObject(NULL_BRUSH);

::SelectObject(hDC,hBrush);

::SetROP2(hDC,R2_NOTXORPEN);

DrawLine(hDC,pt1,pt2);

int nR = Distance(pt1,pt2);

DrawCircle(hDC,pt1,nR);

pt3 = ptPos;

nR = Distance(pt1,pt3);

DrawCircle(hDC,pt1,nR);

DrawLine(hDC,pt1,pt3);

pt2 = pt3;

::ReleaseDC(hWnd,hDC);

}

}else if(g_nToolID == ID_LINE)

{

if(g_nState == 1)

{

HDC hDC = ::GetDC(hWnd);

pt3=ptPos;

//用背景色,绘制上一次的位置==>擦除

::SetROP2(hDC,R2_NOTXORPEN);

::DrawLine(hDC,pt1,pt2);

::DrawLine(hDC,pt1,pt3);

pt2 = pt3;

::ReleaseDC(hWnd,hDC);

}

}

}

void OnLButtonUp(POINT ptPos)

{

if(g_nToolID == ID_CIRCLE)

{

if(g_nState == 1)

{

tagCircle aCir;

aCirptCenter = pt1;

aCirnRadius = Distance(pt1,ptPos);

aCirbIsDelete = false;

g_AllCircles[g_nCircleNumber] = aCir;

g_nCircleNumber = g_nCircleNumber + 1;

g_nState = 0;

}

}else if(g_nToolID == ID_LINE)

{

if(g_nState == 1)

{

g_nState = 0;

tagLine aLine;

aLineptB = pt1;

aLineptE = pt3;

aLinebIsDelete = false;

g_AllLines[g_nLineNumber] = aLine;

g_nLineNumber = g_nLineNumber + 1;

}

}

}

//功能:判断点是否在直线段上

bool PtOnCircle(POINT pt,POINT ptC,int nR)

{

int nL = Distance(pt,ptC);

if( fabs(nL - nR) < 5 )

{

return true;

}else

{

return false;

}

}

//功能:判断点是否在直线段上

bool PtOnLine(POINT pt,POINT ptB,POINT ptE)

{

int nL1 = Distance(pt,ptB);

int nL2 = Distance(pt,ptE);

int nL3 = Distance(ptB,ptE);

if( fabs(( nL1 + nL2 ) - nL3) < 5 )

{

return true;

}else

{

return false;

}

}

void OnLButtonDown(HWND hWnd,POINT ptPos)

{

if(g_nToolID == ID_CIRCLE)

{

if(g_nState == 0)

{

g_nState = 1;

pt2 = pt1 = ptPos;

}

}else if(g_nToolID == ID_LINE)

{

if(g_nState == 0)

{

g_nState = 1;

pt2 = pt1 = ptPos;

}

}else if(g_nToolID == ID_DELETE)

{

//判断当前鼠标点是否在某一个直线段上

for(int nI = 0; nI < g_nLineNumber; nI = nI + 1)

{

tagLine pLine = &g_AllLines[nI];

if(PtOnLine(ptPos,(pLine)ptB,(pLine)ptE))

{

char cTxt[100];

sprintf(cTxt,"nI = %d\n",nI);

::OutputDebugString(cTxt);

(pLine)bIsDelete = true;

break;

}

}

//判断当前鼠标点是否在某一个圆上

for(nI = 0; nI < g_nCircleNumber; nI = nI + 1)

{

tagCircle pCircle = &g_AllCircles[nI];

if(PtOnCircle(ptPos,(pCircle)ptCenter,(pCircle)nRadius))

{

(pCircle)bIsDelete = true;

break;

}

}

::InvalidateRect(hWnd,NULL,TRUE);

}

}

void OnPaint(HDC hDC)

{

// 1重绘圆

//取得系统预定义一个“空心刷子”

HBRUSH hBrush = (HBRUSH)::GetStockObject(NULL_BRUSH);

::SelectObject(hDC,hBrush);

for(int nI = 0; nI < g_nCircleNumber; nI++)

{

tagCircle aCir;

aCir = g_AllCircles[nI];

if(aCirbIsDelete == false)

{

DrawCircle(hDC,aCirptCenter,aCirnRadius);

}

}

// 2重绘直线段

for(nI = 0; nI < g_nLineNumber; nI = nI + 1)

{

tagLine aLine = g_AllLines[nI];

if(aLinebIsDelete == false)

{

::DrawLine(hDC,aLineptB,aLineptE);

}

}

}

void OnCommand(HWND hWnd,int nCmdID)

{

switch(nCmdID)

{

case ID_EXIT:

::DestroyWindow(hWnd);

break;

case ID_LINE:

g_nToolID = ID_LINE;

break;

case ID_CIRCLE:

g_nToolID = ID_CIRCLE;

break;

case ID_DELETE:

g_nToolID = ID_DELETE;

break;

}

}

// 窗口回调函数

int WndProc(HWND hWnd,

int message,

int wParam,

int lParam)

{

switch (message)

{

case WM_SETCURSOR:

{

HCURSOR hCur1 = ::LoadCursor(g_hInstance,"pen_cursor");

HCURSOR hCur2 = ::LoadCursor(g_hInstance,"hand_cursor");

HCURSOR hCur3 = ::LoadCursor(g_hInstance,"point_cursor");

HCURSOR hCur4 = ::LoadCursor(g_hInstance,"arrow_cursor");

switch(g_nToolID)

{

case ID_LINE:

::SetCursor(hCur1);

break;

case ID_CIRCLE:

::SetCursor(hCur2);

break;

case ID_DELETE:

::SetCursor(hCur3);

break;

default:

::SetCursor(hCur1);

}

break;

}

case WM_COMMAND:

{

int nID = LOWORD(wParam);

OnCommand(hWnd,nID);

break;

}

case WM_LBUTTONDOWN:

{

POINT ptPos;

ptPosx = LOWORD(lParam);

ptPosy = HIWORD(lParam);

OnLButtonDown(hWnd,ptPos);

}

break;

case WM_LBUTTONUP:

{

POINT ptPos;

ptPosx = LOWORD(lParam);

ptPosy = HIWORD(lParam);

OnLButtonUp(ptPos);

}

break;

case WM_MOUSEMOVE:

{

POINT ptPos;

ptPosx = LOWORD(lParam);

ptPosy = HIWORD(lParam);

OnMouseMove(ptPos,hWnd);

}

break;

case WM_PAINT:

{

PAINTSTRUCT ps;

HDC hDC = ::BeginPaint(hWnd,&ps);

OnPaint(hDC);

::EndPaint(hWnd,&ps);

break;

}

case WM_DESTROY: //窗口关闭时发送

::PostQuitMessage(0); //在消息队列的尾部添加一个0消息,以示,结束程序

break;

default:

return ::DefWindowProc(hWnd, message, wParam, lParam);

}

return 0;

}

int WinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance,

char lpCmdLine,

int nCmdShow)

{

g_hInstance = hInstance;

WNDCLASSEX wcex;

wcexcbSize = sizeof(WNDCLASSEX);

wcexstyle = CS_HREDRAW | CS_VREDRAW;

wcexlpfnWndProc = (WNDPROC)WndProc;

wcexcbClsExtra = 0;

wcexcbWndExtra = 0;

wcexhInstance = hInstance;

wcexhIcon = NULL;

wcexhCursor = LoadCursor(NULL, IDC_ARROW);

wcexhbrBackground = (HBRUSH)(COLOR_WINDOW+1);

wcexlpszMenuName = "menu1";

wcexlpszClassName = "ABCDE";

wcexhIconSm = NULL;

RegisterClassEx(&wcex);

HWND hWnd = CreateWindow("ABCDE",

"WanJie",

WS_OVERLAPPEDWINDOW,

50, 50, //(x,y)

500, 500, // (w,h)

NULL,

NULL,

hInstance,

NULL);

ShowWindow(hWnd,SW_SHOW);

MSG msg;

while (GetMessage(&msg, NULL, 0, 0))

{

DispatchMessage(&msg);

}

return 1;

}

function

fupin(w,c,l,r)

d1=4lc^4r;

d2=w^2;

d3=w^3;

d4=(2cr)(-2lc+1/d2);

d5=(-4lc)/w+1/d3;

d6=sqrt(d4^2+d5^2);

p=d1/d6;

plot(w,p);

试试吧,你写一个函数,把里面的参数提出来,这样你在计算其他曲线的时候,就改相应的值就好了!不过我画出来感觉不太像,你按照这个思路试试!

以上就是关于怎么用C语言设计画图程序全部的内容,包括:怎么用C语言设计画图程序、帮我写个画图程序,用win32 API,实现画圆,画矩形等,用到控件 ,鼠标消息处理等。、matlab编写程序画图,做成一个时间-路程的曲线图等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9541526.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存