title |
description |
sections |
tags |
canonical_url |
url_translated |
title_translated |
lang |
Code with comments |
Notes about programming with code snippets and comments. Problem solutions and solution descriptions. |
Problem solutions and solution descriptions |
|
javascript |
online |
canvas |
geometry |
matrix |
algorithms |
implementation |
graphics |
images |
pictures |
square |
cube |
|
/en/ |
/ru/ |
Код с комментариями |
en |
{%- assign articles = "" | split: "" %}
{%- assign articles = articles | push: "Volumetric tetris" %}
{%- capture article_brief %}
General educational game in the broad meaning of this word. In the process of learning programming languages,
it is recommended to write your own version first and then use it for demonstrating and testing another
software or hardware. The three-dimensional interface is written in JavaScript Canvas — the logic of the
game itself is two-dimensional.
{%- endcapture %}
{%- assign articles = articles | push: article_brief %}
{%- assign articles = articles | push: "Spinning spatial cross" %}
{%- capture article_brief %}
We are writing an algorithm for rotating a three-dimensional figure around its center along all three axes
at once. In the previous example, we rotated cube in space — in this example, there are a lot of cubes, the
algorithm will be almost the same, and we will use the same formulas. For clarity, let's take two variants
of a symmetrical volumetric figure in two types of projections — spatial cross and cross-cube — we
consider the difference between them.
{%- endcapture %}
{%- assign articles = articles | push: article_brief %}
{%- assign articles = articles | push: "Spinning cube in space" %}
{%- capture article_brief %}
We consider the difference between parallel and perspective projection. Both are widely used in practice
for various purposes. In the previous example, we rotated square on plane — we pass into three-dimensional
space. Now, to display the rotation of a three-dimensional object on the screen plane, we first need to
create a mathematical model of a three-dimensional object, rotate it by an angle, draw a projection from
it and display already the projection on the screen.
{%- endcapture %}
{%- assign articles = articles | push: article_brief %}
{%- assign articles = articles | push: "Spinning square on plane" %}
{%- capture article_brief %}
Let's write an algorithm in JavaScript to rotate a square by an angle around its center, repeat the high
school program. We will use the Math
class for calculations, and Canvas for displaying the results.
The origin of the coordinates is in the upper left corner, the coordinate axes are directed to the right and
down. The central point for rotations t0
is located in the center of the figure. A square is an array of
four points-vertices. We bypass the array of points, rotate each of them by an angle, then link the points
with lines and draw lines on the canvas. We renew the image at a frequency of 20 frames per second.
{%- endcapture %}
{%- assign articles = articles | push: article_brief %}
{%- include main_page.html articles = articles -%}