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
| 165 | * performing the rotation/translation transformation, so for LOLWUT it's |
| 166 | * a good approach. */ |
| 167 | void 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 | } |
no test coverage detected