The Sunday Blog: Award ceremony of the Summer Contest

And the winner is…

Rob van G. from Utrecht/Netherlands – Congratulations Rob! Your Nextion developer kit, consisting of a  NX4024T032 HMI and a Foca Max adapter is already on its way!

The challenge

Remember, the participants of the Nextion Summer Contest were asked to use all previous Sunday Blog posts of the advanced mathematics series which dealt with calculating and drawing curves, all without external MCU, just in Nextion language. It had been shown how to use a set of sine and cosine approximations to iterate through the coordinates of a circular movement with arbitrary precision. In the harmonic motion episode just one week before the contest started, we even used two (sin/cos) pairs to get two independent circular movements and used the sine  of the first on the x-axis and the cosine of the second on the y-axis to draw Lissajous patterns.

For the contest, you were asked to write code to draw Spirograph patterns. Since the principle of a Spirograph is a geared drawing wheel  with one or more eccentric drawing holes, rotating within an outer geared ring, it’s also just a superposition of two circular movements, the code from the previous week could partly be re-used. The difference is that this time, we needed both, the sine and the cosine components from both iterators and had to add these coordinate-wise up to get the superposition of the two movements, first the wheel rotating in the ring, and then the drawing hole rotation around the center of the wheel. The result is then a beautiful floral pattern.

The solution

Let the inner radius of the outer ring be Ro and the radius of the geared wheel Ri, then it is easy to see that the wheel’s center describes a circle with the radius (Ro – Ri). Since Ri is smaller than Ro, the wheel makes Ro/Ri revolutions while its center describes a full circle. Then, there is the eccentricity. Let the distance of the drawing hole from the wheel’s center be Rd. Then, the drawing hole (and thus our pencil) will draw curls with the radius Rd around the center’s trajectory. That’s basically all:

Xc = (Ro – Ri) * cos (2 * pi * t) and Yc = (Ro – Ri) * sin(2 * pi * t) for the center coordinates of the wheel
Xd = Rd * cos (2 * pi * t * Ro/Ri) and Yc = Rd * sin(2 * pi * t * Ro/Ri) for the hole around the center

X = Xc + Xd and Y = Yc + Yd and you get the required pattern.

The above calculus can still be simplified for the sake of quicker drawing by using trigonometric addition theorems and substitutions as described in this Wikipedia article, but the result on the screen (besides drawing speed) will be identical.

As usual, you’ll find the full hmi project with all resources in the Nextion forums to tinker around with it. Have fun!

Next week, we’ll go back to our Nextion component and customizing topic.