A card layout - in Perl

TL;DR

Translating A card layout - extended into some Perl code.

Here is a direct translation of the layout in A card layout - extended in some Perl code.

 1 sub generate_grid ($width, $height, $K) {
 2    my $y_unit = $height / (2 + $K + 1 + $K / 2 + 1 + $K + 1 + $K + 2);
 3    my $slot_height = $y_unit * $K;
 4    my $y1 = 2 * $y_unit;
 5    my $y2 = (2 + $K + 1) * $y_unit;
 6    my $led_height = $slot_height / 2;
 7 
 8    my $x_unit = $width / (2 + $K + 1 + $K + 2);
 9    my $slot_width  = $x_unit * $K;
10    my $x10 = 2 * $x_unit;
11    my $x20 = (2 + $K + 1) * $x_unit;
12 
13    my $x_led_unit = $slot_width / ($K + 1 + $K + 1 + $K);
14    my $led_width = $x_led_unit * $K;
15    my $x11 = $x10 + ($K + 1) * $x_led_unit;
16    my $x12 = $x10 + ($K + 1 + $K + 1) * $x_led_unit;
17 
18    return {
19       A => {
20          x => $x10,
21          y => $y1,
22          height => $slot_height,
23          width => $slot_width,
24       },
25       B => {
26          x => $x20,
27          y => $y1,
28          height => $slot_height,
29          width => $slot_width,
30       },
31       C => {
32          x => $x20,
33          y => $y2,
34          height => $slot_height,
35          width => $slot_width,
36       },
37       led1 => {
38          x => $x10,
39          y => $y2,
40          height => $led_height,
41          width => $led_width,
42       },
43       led2 => {
44          x => $x11,
45          y => $y2,
46          height => $led_height,
47          width => $led_width,
48       },
49       led3 => {
50          x => $x12,
51          y => $y2,
52          height => $led_height,
53          width => $led_width,
54       },
55    };
56 }

The returned anonymous hash contains the data for the position (upper-left corner) and size of each slot. Only the upper half of the card is provided, because the other half can be generated by simple rotation.

Note that the actual card dimensions are provided as inputs, as well as factor $K$ (line 1).

The two units are calculated separately for the $X$ axis (line 8) and the $Y$ axis (line 2), as already discussed in A card layout. Then it’s just basic maths.

Note that line 13 calculates a led unit on the $X$ axis, which then allows calculating the positions for the leds.


Comments? Octodon, , GitHub, Reddit, or drop me a line!