MCPcopy Create free account
hub / github.com/F-Stack/f-stack / lwDrawSquare

Function lwDrawSquare

app/redis-6.2.6/src/lolwut.c:167–188  ·  view source on GitHub ↗

Draw a square centered at the specified x,y coordinates, with the specified * rotation angle and size. In order to write a rotated square, we use the * trivial fact that the parametric equation: * * x = sin(k) * y = cos(k) * * Describes a circle for values going from 0 to 2*PI. So basically if we start * at 45 degrees, that is k = PI/4, with the first point, and then we find * the other

Source from the content-addressed store, hash-verified

165 * performing the rotation/translation transformation, so for LOLWUT it's
166 * a good approach. */
167void lwDrawSquare(lwCanvas *canvas, int x, int y, float size, float angle, int color) {
168 int px[4], py[4];
169
170 /* Adjust the desired size according to the fact that the square inscribed
171 * into a circle of radius 1 has the side of length SQRT(2). This way
172 * size becomes a simple multiplication factor we can use with our
173 * coordinates to magnify them. */
174 size /= 1.4142135623;
175 size = round(size);
176
177 /* Compute the four points. */
178 float k = M_PI/4 + angle;
179 for (int j = 0; j < 4; j++) {
180 px[j] = round(sin(k) * size + x);
181 py[j] = round(cos(k) * size + y);
182 k += M_PI/2;
183 }
184
185 /* Draw the square. */
186 for (int j = 0; j < 4; j++)
187 lwDrawLine(canvas,px[j],py[j],px[(j+1)%4],py[(j+1)%4],color);
188}

Callers 1

lwDrawSchotterFunction · 0.85

Calls 1

lwDrawLineFunction · 0.85

Tested by

no test coverage detected