Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
2ead41e0
Commit
2ead41e0
authored
Sep 12, 2018
by
antirez
Browse files
LOLWUT: draw rotated squares using trivial trigonometry.
parent
a974531d
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/lolwut.c
View file @
2ead41e0
...
...
@@ -34,6 +34,7 @@
*/
#include "server.h"
#include <math.h>
/* This function represents our canvas. Drawing functions will take a pointer
* to a canvas to write to it. Later the canvas can be rendered to a string
...
...
@@ -124,6 +125,47 @@ void lwDrawLine(lwCanvas *canvas, int x1, int y1, int x2, int y2, int color) {
}
}
/* 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 three points incrementing K by PI/2 (90 degrees), we'll have the
* points of the square. In order to rotate the square, we just start with
* k = PI/4 + rotation_angle, and we are done.
*
* Of course the vanilla equations above will descrive the square inside a
* circle of radius 1, so in order to draw larger squares we'll have to
* multiply the obtained coordinates, and then translate them. However this
* is much simpler than implementing the abstract concept of 2D shape and then
* performing the rotation/translation transformation, so for LOLWUT it's
* a good approach. */
void
lwDrawSquare
(
lwCanvas
*
canvas
,
int
x
,
int
y
,
float
size
,
float
angle
)
{
int
px
[
4
],
py
[
4
];
/* Adjust the desired size according to the fact that the square inscribed
* into a circle of radius 1 has the side of length SQRT(2). This way
* size becomes a simple multiplication factor we can use with our
* coordinates to magnify them. */
size
/=
1
.
4142
;
/* Compute the four points. */
float
k
=
M_PI
/
4
+
angle
;
for
(
int
j
=
0
;
j
<
4
;
j
++
)
{
px
[
j
]
=
sin
(
k
)
*
size
+
x
;
py
[
j
]
=
cos
(
k
)
*
size
+
y
;
k
+=
M_PI
/
2
;
}
/* Draw the square. */
for
(
int
j
=
0
;
j
<
4
;
j
++
)
lwDrawLine
(
canvas
,
px
[
j
],
py
[
j
],
px
[(
j
+
1
)
%
4
],
py
[(
j
+
1
)
%
4
],
1
);
}
/* Converts the canvas to an SDS string representing the UTF8 characters to
* print to the terminal in order to obtain a graphical representaiton of the
* logical canvas. The actual returned string will require a terminal that is
...
...
@@ -159,6 +201,8 @@ int main(void) {
lwDrawPixel
(
c
,
i
,
i
,
1
);
}
lwDrawLine
(
c
,
10
,
10
,
60
,
30
,
1
);
lwDrawSquare
(
c
,
40
,
40
,
40
,
0
.
5
);
lwDrawSquare
(
c
,
50
,
40
,
10
,
1
);
sds
rendered
=
lwRenderCanvas
(
c
);
printf
(
"%s
\n
"
,
rendered
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment