In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you how C++ uses easyx to draw watches and clocks that move in real time. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
This time the task is to use C++ to draw a clock that moves in real time, and is consistent with the time of the current system.
Since we are using C++ language, we need to use this example to improve our understanding of object-oriented programming.
We first need to analyze the requirements, "draw a clock that can move around in real time". According to the demand, we can benefit from two objects, the clock object and the drawing object, so we roughly establish two classes, the Clock class and the Paint class.
The member variables in the Clock class are: the center coordinates x and y of the table, the time of the table (hours, minutes, seconds), the size of the table r (that is, the radius), and the color color of the dial.
There are no other functions in the Clock class, only constructors for initialization.
There are no member variables in the Paint class, only three functions: drawClock_bk, drawClock_scale, and drawClock_sharp.
Among them, it is very simple to draw the dial, and the most difficult thing is to draw the scale function and the needle function.
If you want to draw the scale and the watch hand, you must know how to draw the two coordinates of the scale.
Let's first learn how to get the coordinates (pure mathematical knowledge)
As shown in the figure:
If you want to find the coordinates of a point on the circle (XMague y), using the trigonometric function, if the angle between the line between the point an and the center of the circle o (x0thy0) and the x axis is the radius, then the Abscissa x value of an is x0+cos (c) * the ordinate y of rMagine an is y0-sin (c) * r, so the coordinates of any point on the circle can be obtained. Then we need to draw the scale, that is, we also need another coordinate on the line between the center o and the point an on the circle, so that we can draw the scale. As shown in the figure:
For example, point b is the point on the line between point an and the center of the circle. Suppose that the scale length we need to draw is s, so the distance between an and b is the distance between an and b, and the distance between b and the center of the circle is rmers, so according to the trigonometric function, we can also find that the coordinate of point b is x:x0+cos (c) * (rmurs), y is: y0-sin (c) * (rMuir s).
With the coordinates of an and b, a scale can be drawn, and then all the scales can be drawn according to the actual distribution of the dial (that is, each scale is 5 degrees).
The drawing of the watch hand is similar to the scale: you need to find the point b (the point on the line between the center of the circle and the point on the circle), and then the coordinates of point b can be obtained according to the length and angle of the needle you specify. Then the corresponding pointer can be drawn with b-point coordinates and center coordinates.
This is the most important way to find the coordinates. For the remaining details, please see the following code:
# include # define PI 3.1415using namespace std; class Clock {public: int _ x; int _ y; int _ hour; int _ minute; int _ second; int _ r; COLORREF _ bk_col;public: Clock (int X. This- > _ hour = h; this- > _ minute = m; this- > _ second = s; this- > _ r = r; this- > _ bk_col = bk_color;}}; class Paint {public: void drawclock_bk (Clock c); void drawclock_scale (Clock c); void drawclock_sharp (Clock c);} Void Paint::drawclock_bk (Clock c) {setcolor (RGB); setfillcolor (RGB); fillcircle (c. Clock x) {int x 1; int x 2, y 2; setlinecolor (RGB (255,255,255); for (int a = 1; a tm_min, ptime- > tm_sec, 120, RGB (255,255,255)) Paint p1; p1.drawclock_bk (c); p1.drawclock_scale (c); p1.drawclock_sharp (c); int flag=0; while (true) {Sleep (1000); + c. Received secondd; c. Allocated secondhand% reserved 60; if (c. Broadcast secondhand = 0) {c.roomminuted;} c._minute% = 60 If (c._minute==1) {flag=0; if (c. Details = 0&&flag==0) {c. P1.drawclock_bk hourly examples; flag=1;} c._hour% = 24; p1.drawclock_bk (c); p1.drawclock_scale (c) P1.drawclock_sharp (c);} _ getch (); closegraph (); return 0;}
The running effect of vs2013 is shown in the figure:
These are all the contents of this article entitled "how C++ uses easyx to draw clocks that move in real time". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.