href = "http://www.actiprosoftware.com/app_themes/standard/standard.css" text = "text / css" rel = "stylesheet" /> Recently watching the programming book, see one of the sentences: at 80 In the era, if you can draw rounds, then you are really smart.
This sentence makes me jump, but what should I painter?
The first idea is to draw a straight line in the direction of the screen horizontal, then you can draw a solid circle, below is the key code section:
Void UselineDrawround ()
{
INT IR, IXO, IYO, IX1, IX2, IY1, IY2, IX;
HDC GDC = Getdc (g_hwnd);
HPEN HPEN, OLD_HPEN;
IXO = 50;
IYO = 50;
Ir = 50;
IY1 = IYO - IR;
IY2 = IYO IR;
HPEN = Createpen (ps_dash, 1, rgb (0, 0, 255));
Old_hpen = (hpen) SelectObject (GDC, HPEN);
For (int y = iy1; y <= iy2; y )
{
IX = (int) SQRT (POW (IR, 2) - POW (Y-IYO, 2));
IX1 = IXO - IX;
IX2 = IXO IX;
MoveToex (GDC, IX1, Y, NULL);
LINETO (GDC, IX2, Y);
}
DeleteObject (HPEN);
SelectObject (GDC, OLD_HPEN);
ReleaseDC (G_HWND, GDC);
}
Ok, now you can draw a solid round, then if I want to draw a hollow?
Immediately think of drawing, the code is as follows:
Void USPixeldrawround ()
{
INT IR, IXO, IYO, IX1, IX2, IY1, IY2, IX;
HDC GDC = Getdc (g_hwnd);
IXO = 150;
IYO = 150;
Ir = 50;
IY1 = IYO - IR;
IY2 = IYO IR;
For (int y = iy1; y <= iy2; y )
{
IX = (int) SQRT (POW (IR, 2) - POW (Y-IYO, 2));
IX1 = IXO - IX;
IX2 = IXO IX;
Setpixel (GDC, IX1, Y, RGB (255, 0, 0));
Setpixel (GDC, IX2, Y, RGB (255, 0, 0));
}
ReleaseDC (G_HWND, GDC);
}
If you have run the code above, you will find some points that there is no painting, because the calculated x, y is the int type, the fractional part is went, it is the accuracy problem. Is there any way to improve this algorithm? it should be OK. We can imitate the polygon, regard the circle as a positive N-shaped shape, and when this N tends to be infinite, this round will look very perfect, at least this can solve the problem between point and points. .
Void Uselinedrawround2 ()
{
INT IR, IXO, IYO, IX1, IX2, IY1, IY2, IX;
HDC GDC = Getdc (g_hwnd); HPEN HPEN, OLD_HPEN;
Static int SX = 0;
Static int Sy = 0;
IXO = 250;
IYO = 250;
Ir = 50;
IY1 = IYO - IR;
IY2 = IYO IR;
HPEN = CREATEPEN (PS_SOLID, 1, RGB (0, 0, 255));
Old_hpen = (hpen) SelectObject (GDC, HPEN);
For (int y = iy1; y <= iy2; y )
{
IX = (int) SQRT (POW (IR, 2) - POW (Y-IYO, 2));
IX1 = IXO - IX;
IX2 = IXO IX;
IF (y == iy1) // (SX == 0 && Sy == 0)
{
MoveToex (GDC, IX1, Y, NULL);
}
Else
{
LINETO (GDC, SX, SY);
}
SX = IX1;
Sy = Y;
}
FOR (Y = IY2; Y> = IY1; Y -)
{
IX = (int) SQRT (POW (IR, 2) - POW (Y-IYO, 2));
IX1 = IXO - IX;
IX2 = IXO IX;
LINETO (GDC, IX2, Y);
}
DeleteObject (HPEN);
SelectObject (GDC, OLD_HPEN);
ReleaseDC (G_HWND, GDC);
}
All code is as follows:
#define Win32_Lean_and_mean
#include
#include
#include
#include
#include
#include
#define key_down (VK_CODE) ((GetasyncKeyState (VK_CODE) & 0x8000)? 1: 0)
Hinstance hinstance_app;
HWND G_HWND;
Void UselineDrawround ()
{
INT IR, IXO, IYO, IX1, IX2, IY1, IY2, IX;
HDC GDC = Getdc (g_hwnd);
HPEN HPEN, OLD_HPEN;
IXO = 50;
IYO = 50;
Ir = 50;
IY1 = IYO - IR;
IY2 = IYO IR;
HPEN = Createpen (ps_dash, 1, rgb (0, 0, 255));
Old_hpen = (hpen) SelectObject (GDC, HPEN);
For (int y = iy1; y <= iy2; y )
{
IX = (int) SQRT (POW (IR, 2) - POW (Y-IYO, 2));
IX1 = IXO - IX;
IX2 = IXO IX;
MoveToex (GDC, IX1, Y, NULL);
LINETO (GDC, IX2, Y);
}
DeleteObject (HPEN);
SelectObject (GDC, OLD_HPEN);
ReleaseDC (G_HWND, GDC);
Void USPixeldrawround ()
{
INT IR, IXO, IYO, IX1, IX2, IY1, IY2, IX;
HDC GDC = Getdc (g_hwnd);
IXO = 150;
IYO = 150;
Ir = 50;
IY1 = IYO - IR;
IY2 = IYO IR;
For (int y = iy1; y <= iy2; y )
{
IX = (int) SQRT (POW (IR, 2) - POW (Y-IYO, 2));
IX1 = IXO - IX;
IX2 = IXO IX;
Setpixel (GDC, IX1, Y, RGB (255, 0, 0));
Setpixel (GDC, IX2, Y, RGB (255, 0, 0));
}
ReleaseDC (G_HWND, GDC);
}
Void Uselinedrawround2 ()
{
INT IR, IXO, IYO, IX1, IX2, IY1, IY2, IX;
HDC GDC = Getdc (g_hwnd);
HPEN HPEN, OLD_HPEN;
Static int SX = 0;
Static int Sy = 0;
IXO = 250;
IYO = 250;
Ir = 50;
IY1 = IYO - IR;
IY2 = IYO IR;
HPEN = CREATEPEN (PS_SOLID, 1, RGB (0, 0, 255));
Old_hpen = (hpen) SelectObject (GDC, HPEN);
For (int y = iy1; y <= iy2; y )
{
IX = (int) SQRT (POW (IR, 2) - POW (Y-IYO, 2));
IX1 = IXO - IX;
IX2 = IXO IX;
IF (y == iy1) // (SX == 0 && Sy == 0)
{
MoveToex (GDC, IX1, Y, NULL);
}
Else
{
LINETO (GDC, SX, SY);
}
SX = IX1;
Sy = Y;
}
FOR (Y = IY2; Y> = IY1; Y -)
{
IX = (int) SQRT (POW (IR, 2) - POW (Y-IYO, 2));
IX1 = IXO - IX;
IX2 = IXO IX;
LINETO (GDC, IX2, Y);
}
DeleteObject (HPEN);
SelectObject (GDC, OLD_HPEN);
ReleaseDC (G_HWND, GDC);
}
LResult Callback WinProc (HWND HWND, UINT MSG, WPARAM WPARAM, LPARAM LPARAM)
{
Paintstruct PS;
HDC HDC;
Switch (msg)
{
Case WM_CREATE:
Return 0;
Break;
Case WM_Paint:
HDC = BeginPaint (HWND, & PS);
Uselinedrawround ();
Usepixeldrawround ();
Uselinedrawround2 ();
Endpaint (hwnd, & ps);
Return 0;
Break;
Case WM_DESTROY:
PostquitMessage (0);
Return 0;
Break;
DEFAULT:
Break;
}
Return DefWindowProc (HWND, MSG, WPARAM, LPARAM);
}
Int WinApi WinMain (Hinstance Hinstance, Hinstance Hprevinstance, LPSTR LPCMDLINE, INT NSHOWCMD)
{
Hwnd hwnd;
WNDCLASSEX WINCLASS;
MSG msg;
Hinstance_app = hinstance;
WinClass.cbclsextra = 0;
Winclass.cbsize = sizeof (wndclassex);
WinClass.cbwndextra = 0;
WinClass.hbrbackground = (hbrush) getStockBrush (Black_brush);
Winclass.hcursor = loadingcursor (Hinstance, makeintResource (CURSOR_CROSSHAIR);
Winclass.hicon = loading ;; NULL, IDI_APPLICATION;
Winclass.hiconsm = loadicon (Hinstance, MakeintResource (ICON_T3DX));
Winclass.hinstance = Hinstance;
WinClass.lpfnWndProc = WinProc;
WinClass.lpszclassName = "Winclass1";
WinClass.lpsz GeneNuname = NULL;
WinClass.Style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC | CS_DBLCLKS;
IF (! RegisterClassex (& WinClass))
{
Return 0;
}
HWnd = CREATEWINDOWEX (WS_EX_TOPMOST,
"Winclass1",
"My First Window",
WS_POPUP / * WS_OVERLAPPEDWINDOW * / | WS_VISIBLE,
0,
0,
GetSystemMetrics (SM_CXSCREEN),
GetSystemMetrics (SM_CYSCREEN),
NULL,
NULL,
Hinstance,
NULL);
IF (hwnd == null)
{
Return 0;
}
g_hwnd = hwnd;
/ *
While (GetMessage (& MSG, NULL, 0, 0))
{
TranslateMessage (& MSG);
DispatchMessage (& MSG);
IF (key_down (vk_escape))
{
Postmessage (hwnd, wm_destroy, 0, 0);
}
}
* /
While (True)
{
IF (PEEKMESSAGE (& MSG, NULL, 0, 0, PM_REMOVE))
{
IF (msg.Message == wm_quit)
{
Break;
}
TranslateMessage (& MSG);
DispatchMessage (& MSG);
IF (key_down (vk_escape))
{
Postmessage (hwnd, wm_destroy, 0, 0);
}
}
}
Return msg.wparam;
}