Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <Windows.h>
  3. #include <math.h>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <vector>
  7. #include <tchar.h>
  8.  
  9. using namespace std;
  10.  
  11. static int Flag=0;
  12. static vector<int> AllPoints;
  13.  
  14. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  15.  
  16. TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
  17.  
  18. int Round(double x)
  19. {
  20. return (int)(x + 0.5);
  21. }
  22.  
  23. void save(int x,int y)
  24. {
  25. AllPoints.push_back(x);
  26. AllPoints.push_back(y);
  27. }
  28.  
  29. void Extract()
  30. {
  31. ofstream out;
  32. out.open("text.txt");
  33.  
  34. for(int i = 0; i<AllPoints.size(); i+=2)
  35. out << AllPoints[i] << "," << AllPoints[i+1] << ",";
  36.  
  37. out.close();
  38. }
  39.  
  40. //CARTESIN
  41. void DrawLine1(HDC hdc, int x1, int y1, int x2, int y2, COLORREF c)
  42. {
  43. int dx=(x2-x1),dy=(y2-y1);
  44.  
  45. if(abs(dx)>=abs(dy))
  46. {
  47. if(x1 > x2)
  48. {
  49. swap(x1,x2);
  50. swap(y1,y2);
  51. }
  52.  
  53. double slope = (double)dy/dx;
  54.  
  55. for(int x=x1; x<x2; x++)
  56. {
  57. double y = y1 + (x - x1)*slope;
  58. SetPixel(hdc, x, Round(y), c);
  59. save(x,Round(y));
  60. }
  61. }
  62. else
  63. {
  64. if(y1 > y2)
  65. {
  66. swap(x1,x2);
  67. swap(y1,y2);
  68. }
  69.  
  70. double slope = (double)dx/dy;
  71.  
  72. for(int y=y1; y<y2; y++)
  73. {
  74. double x = x1 + (y - y1)*slope;
  75. SetPixel(hdc, Round(x), y, c);
  76. save(Round(x),y);
  77. }
  78. }
  79. }
  80.  
  81. void Draw8Points(HDC hdc, int x, int y, int xc,int yc, COLORREF c)
  82. {
  83. save(xc+x,yc+y);
  84. save(xc-x,yc+y);
  85. save(xc-x,yc-y);
  86. save(xc+x,yc-y);
  87.  
  88. save(xc+y,yc+x);
  89. save(xc-y,yc+x);
  90. save(xc-y,yc-x);
  91. save(xc+y,yc-x);
  92.  
  93. SetPixel(hdc, xc+x, yc+y, c);
  94. SetPixel(hdc, xc-x, yc+y, c);
  95. SetPixel(hdc, xc-x, yc-y, c);
  96. SetPixel(hdc, xc+x, yc-y, c);
  97. SetPixel(hdc, xc+y, yc+x, c);
  98. SetPixel(hdc, xc-y, yc+x, c);
  99. SetPixel(hdc, xc-y, yc-x, c);
  100. SetPixel(hdc, xc+y, yc-x, c);
  101. }
  102. //MIDPOINT CIRCLE
  103. void DrawCircle3(HDC hdc,int xc, int yc, int R, COLORREF c)
  104. {
  105.  
  106.  
  107. int x = R , y = 0;
  108. int d = 1 - R;
  109.  
  110. Draw8Points(hdc,Round(x),Round(y),xc,yc,c);
  111. while(x > y)
  112. {
  113. if(x==0)
  114. cout<< x << " " << y << " " << d<<endl;
  115.  
  116. int d1 = 2*y + 3;
  117. int d2 = 2*(y-x) + 5;
  118.  
  119. if(d<0)
  120. {
  121.  
  122. d+=d1;
  123. y++;
  124. }
  125.  
  126. else
  127. {
  128. d+=d2;
  129. y++;
  130. x--;
  131. }
  132.  
  133.  
  134. Draw8Points(hdc,Round(x),Round(y),xc,yc,c);
  135. }
  136.  
  137.  
  138. }
  139.  
  140.  
  141. void Sorting(vector<int> &arr,string Nums)
  142. {
  143. stringstream ss(Nums);
  144.  
  145. for (int i; ss >> i;) {
  146. arr.push_back(i);
  147. if (ss.peek() == ',')
  148. ss.ignore();
  149. }
  150. }
  151.  
  152. void prev(HDC hdc,vector<int> numbers,COLORREF c)
  153. {
  154. for(int i = 0; i<numbers.size(); i+=2)
  155. {
  156. SetPixel(hdc,numbers[i],numbers[i+1],c);
  157. }
  158. }
  159.  
  160. static int x1, y1,x2,y2,x,y,xc,yc;
  161. LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT mcode, WPARAM wp, LPARAM lp)
  162. {
  163.  
  164. string line;
  165. vector<int> vect;
  166. ifstream in;
  167.  
  168. HDC hdc;
  169. double R;
  170. switch (mcode)
  171. {
  172. case WM_LBUTTONDOWN:
  173. if(Flag == 0)
  174. {
  175. remove( "text.txt" );
  176. Flag = 1;
  177. }
  178.  
  179. xc = LOWORD(lp);
  180. yc = HIWORD(lp);
  181. break;
  182. case WM_LBUTTONUP:
  183. if(Flag == 0)
  184. {
  185. remove( "text.txt" );
  186. Flag = 1;
  187. }
  188. x = LOWORD(lp);
  189. y = HIWORD(lp);
  190.  
  191. hdc = GetDC(hWnd);
  192.  
  193. R = Round(sqrt(pow(x-xc,2.0)+pow(y-yc,2.0)));
  194.  
  195. DrawCircle3(hdc, xc, yc, R, RGB(255,255,255));
  196.  
  197. ReleaseDC(hWnd, hdc);
  198. break;
  199.  
  200. case WM_RBUTTONDOWN:
  201. if(Flag == 0)
  202. {
  203. remove( "text.txt" );
  204. Flag = 1;
  205. }
  206. xc = LOWORD(lp);
  207. yc = HIWORD(lp);
  208. break;
  209.  
  210. case WM_RBUTTONUP:
  211. if(Flag == 0)
  212. {
  213. remove( "text.txt" );
  214. Flag = 1;
  215. }
  216.  
  217. x = LOWORD(lp);
  218. y = HIWORD(lp);
  219.  
  220. hdc = GetDC(hWnd);
  221.  
  222. DrawLine1(hdc, xc, yc, x, y, RGB(255, 255, 255));
  223. ReleaseDC(hWnd, hdc);
  224. break;
  225. case WM_KEYDOWN:
  226. if ( wp == 'L' )
  227. {
  228. in.open("text.txt");
  229. getline(in, line);
  230. Sorting(vect, line);
  231. hdc = GetDC(hWnd);
  232. AllPoints = vect;
  233. prev(hdc,vect,RGB(255,255,255));
  234. in.close();
  235. }
  236.  
  237. if ( wp == 'S' )
  238. {
  239. Extract();
  240. }
  241. break;
  242. case WM_DESTROY:
  243. PostQuitMessage(0);
  244. break;
  245. default:
  246. return DefWindowProc(hWnd, mcode, wp, lp);
  247. }
  248.  
  249. return 0;
  250. }
  251.  
  252. int WINAPI WinMain (HINSTANCE hThisInstance,
  253. HINSTANCE hPrevInstance,
  254. LPSTR lpszArgument,
  255. int nCmdShow)
  256. {
  257. HWND hwnd; /* This is the handle for our window */
  258. MSG messages; /* Here messages to the application are saved */
  259. WNDCLASSEX wincl; /* Data structure for the windowclass */
  260.  
  261. /* The Window structure */
  262. wincl.hInstance = hThisInstance;
  263. wincl.lpszClassName = szClassName;
  264. wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
  265. wincl.style = CS_DBLCLKS; /* Catch double-clicks */
  266. wincl.cbSize = sizeof (WNDCLASSEX);
  267.  
  268. /* Use default icon and mouse-pointer */
  269. wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  270. wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  271. wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  272. wincl.lpszMenuName = NULL; /* No menu */
  273. wincl.cbClsExtra = 0; /* No extra bytes after the window class */
  274. wincl.cbWndExtra = 0; /* structure or the window instance */
  275. /* Use Windows's default colour as the background of the window */
  276. wincl.hbrBackground = (HBRUSH) 10;
  277.  
  278. /* Register the window class, and if it fails quit the program */
  279. if (!RegisterClassEx (&wincl))
  280. return 0;
  281.  
  282. /* The class is registered, let's create the program*/
  283. hwnd = CreateWindowEx (
  284. 0, /* Extended possibilites for variation */
  285. szClassName, /* Classname */
  286. _T("Code::Blocks Template Windows App"), /* Title Text */
  287. WS_OVERLAPPEDWINDOW, /* default window */
  288. CW_USEDEFAULT, /* Windows decides the position */
  289. CW_USEDEFAULT, /* where the window ends up on the screen */
  290. 1000, /* The programs width */
  291. 600, /* and height in pixels */
  292. HWND_DESKTOP, /* The window is a child-window to desktop */
  293. NULL, /* No menu */
  294. hThisInstance, /* Program Instance handler */
  295. NULL /* No Window Creation data */
  296. );
  297.  
  298. /* Make the window visible on the screen */
  299. ShowWindow (hwnd, nCmdShow);
  300.  
  301. /* Run the message loop. It will run until GetMessage() returns 0 */
  302. while (GetMessage (&messages, NULL, 0, 0))
  303. {
  304. /* Translate virtual-key messages into character messages */
  305. TranslateMessage(&messages);
  306. /* Send message to WindowProcedure */
  307. DispatchMessage(&messages);
  308. }
  309.  
  310. /* The program return-value is 0 - The value that PostQuitMessage() gave */
  311. return messages.wParam;
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement