Advertisement
JoelSjogren

Frame multiplication

May 20th, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 8.40 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Frame multiplication</title>
  5. </head>
  6. <body>
  7.  
  8. <h1>Frame multiplication</h1>
  9. Frames are boolean-valued matrices. They can be implemented densely or sparsely. This implementation is dense.
  10. <p>
  11.  
  12. <table>
  13. <tr>
  14.     <td valign="top">
  15.         <canvas id="matrix0" width="300" height="300" style="border:1px solid #c3c3c3;" onclick="click(0)">
  16.     Your browser does not support the HTML5 canvas tag.</canvas><br>
  17.     </td>
  18.     <td valign="center">&times;</td>
  19.     <td valign="top">
  20.         <canvas id="matrix1" width="300" height="300" style="border:1px solid #c3c3c3;" onclick="click(1)">
  21.     Your browser does not support the HTML5 canvas tag.</canvas><br>
  22.     </td>
  23.     <td valign="center">&equals;</td>
  24.     <td valign="top">
  25.         <canvas id="matrix2" width="300" height="300" style="border:1px solid #c3c3c3;"></canvas>
  26.     </td>
  27. </tr>
  28. <tr>
  29.     <td valign="top">
  30.         <button onclick="make('empty', 0)">empty</button>
  31.         <button onclick="make('diagonal', 0)">diagonal</button>
  32.         <button onclick="make('full', 0)">full</button>
  33.         <button onclick="make('random', 0)">random</button>
  34.         <button onclick="make('opposite', 0)">opposite</button>
  35.         <button onclick="make('predicate', 0)">predicate</button>
  36.         <button onclick="make('transpose', 0)">transpose</button>
  37.         <form action="">
  38.             <br>
  39.             Number of random pixels:<br>
  40.             <input type="number" id="random0" value="10"><br><br>
  41.             Custom predicate:<br>
  42.             <input type="text" id="predicate0" value="(i+j)%3 == 1">
  43.         </form>
  44.     </td>
  45.     <td></td>
  46.     <td valign="top">
  47.         <button onclick="make('empty', 1)">empty</button>
  48.         <button onclick="make('diagonal', 1)">diagonal</button>
  49.         <button onclick="make('full', 1)">full</button>
  50.         <button onclick="make('random', 1)">random</button>
  51.         <button onclick="make('opposite', 1)">opposite</button>
  52.         <button onclick="make('predicate', 1)">predicate</button>
  53.         <button onclick="make('transpose', 1)">transpose</button>
  54.         <form action="">
  55.             <br>
  56.             Number of random pixels:<br>
  57.             <input type="number" id="random1" value="10"><br><br>
  58.             Custom predicate:<br>
  59.             <input type="text" id="predicate1" value="(i+j)%3 == 1">
  60.         </form>
  61.     </td>
  62.     <td></td>
  63.     <td valign="top">
  64.         <button onclick="make('transpose', 2)">transpose</button>
  65.         <form action="">
  66.             <br>
  67.             Kind of addition:
  68.             <input type="radio" name="addition" value="OR" checked="checked" onclick="changeAddition('OR')"> OR
  69.             <input type="radio" name="addition" value="XOR" onclick="changeAddition('XOR')"> XOR
  70.         </form>
  71.     </td>
  72. </tr>
  73. </table>
  74.  
  75. <script>
  76.  
  77. var n = 10;
  78. var resn = 300;
  79.  
  80. function Frame() {
  81.     this.data = [];  // n*n array
  82.    
  83.     for (i = 0; i < n; i++) {
  84.        this.data[i] = [];
  85.        for (j = 0; j < n; j++) {
  86.            this.data[i][j] = false;
  87.        }
  88.    }
  89.    
  90.    this.dot = function(frame) {
  91.        new_frame = new Frame();
  92.        for (i = 0; i < n; i++) {
  93.            for (j = 0; j < n; j++) {
  94.                new_frame.data[i][j] = false;
  95.                for (k = 0; k < n; k++) {
  96.                    if (this.data[i][k] && frame.data[k][j]) {
  97.                        new_frame.data[i][j] = true;
  98.                    }
  99.                }
  100.            }
  101.        }
  102.        return new_frame;
  103.    }
  104.    
  105.    this.altdot = function(frame) {
  106.        new_frame = new Frame();
  107.        for (i = 0; i < n; i++) {
  108.            for (j = 0; j < n; j++) {
  109.                new_frame.data[i][j] = false;
  110.                for (k = 0; k < n; k++) {
  111.                    if (this.data[i][k] && frame.data[k][j]) {
  112.                        new_frame.data[i][j] = !new_frame.data[i][j];
  113.                    }
  114.                }
  115.            }
  116.        }
  117.        return new_frame;
  118.    }
  119.    
  120.    this.draw = function(ctx) {
  121.        ctx.clearRect(0, 0, resn, resn);
  122.        ctx.fillStyle = "#000000";
  123.        ctx.strokeStyle = "#eeeeee";
  124.        ctx.lineWidth = 1;
  125.        for (i = 1; i < n; i++) {
  126.            ctx.moveTo(resn*i/n, 0);
  127.            ctx.lineTo(resn*i/n, resn);
  128.            ctx.stroke();
  129.            ctx.moveTo(0, resn*i/n);
  130.            ctx.lineTo(resn, resn*i/n);
  131.            ctx.stroke();
  132.        }
  133.        for (i = 0; i < n; i++) {
  134.            for (j = 0; j < n; j++) {
  135.                if (this.data[i][j]) {
  136.                    ctx.fillRect(resn*j/n, resn*i/n, resn/n, resn/n);
  137.                }
  138.            }
  139.        }
  140.    }
  141.    
  142.    this.makeEmpty = function() {
  143.        for (i = 0; i < n; i++) {
  144.            for (j = 0; j < n; j++) {
  145.                this.data[i][j] = false;
  146.            }
  147.        }
  148.    }
  149.    
  150.    this.makeDiagonal = function() {
  151.        for (i = 0; i < n; i++) {
  152.            for (j = 0; j < n; j++) {
  153.                this.data[i][j] = i == j;
  154.            }
  155.        }
  156.    }
  157.    
  158.    this.makeFull = function() {
  159.        for (i = 0; i < n; i++) {
  160.            for (j = 0; j < n; j++) {
  161.                this.data[i][j] = true;
  162.            }
  163.        }
  164.    }
  165.    
  166.    this.makeRandom = function(many) {
  167.        this.makeEmpty();
  168.        for (k = 0; k < many && k < n*n; k++) {  // TODO
  169.            do {
  170.                i = Math.floor((Math.random() * n));
  171.                j = Math.floor((Math.random() * n));
  172.            } while (this.data[i][j]);
  173.            this.data[i][j] = true;
  174.        }
  175.    }
  176.    
  177.    this.makeOpposite = function() {
  178.        for (i = 0; i < n; i++) {
  179.            for (j = 0; j < n; j++) {
  180.                this.data[i][j] = !this.data[i][j];
  181.            }
  182.        }
  183.    }
  184.    
  185.    this.makePredicate = function(pred) {
  186.        for (i = 0; i < n; i++) {
  187.            for (j = 0; j < n; j++) {
  188.                this.data[i][j] = eval(pred);
  189.            }
  190.        }
  191.    }
  192.    
  193.    this.makeTranspose = function(pred) {
  194.        new_data = [];
  195.        for (i = 0; i < n; i++) {
  196.            new_data[i] = [];
  197.            for (j = 0; j < n; j++) {
  198.                new_data[i][j] = this.data[j][i];
  199.            }
  200.        }
  201.        this.data = new_data;
  202.    }
  203. }
  204.  
  205. var c = [document.getElementById("matrix0"),
  206.         document.getElementById("matrix1"),
  207.         document.getElementById("matrix2")];
  208. var ctx = [c[0].getContext("2d"),
  209.           c[1].getContext("2d"),
  210.           c[2].getContext("2d")];
  211.  
  212. frames = [new Frame, new Frame, new Frame];
  213. make('empty', 0);
  214. make('diagonal', 1);
  215. recalculate();
  216.  
  217. c[0].addEventListener('click', function(event) { click(0, event); }, false);
  218. c[1].addEventListener('click', function(event) { click(1, event); }, false);
  219.  
  220. function make(what, i) {
  221.    if (what === 'empty') frames[i].makeEmpty();
  222.    if (what === 'diagonal') frames[i].makeDiagonal();
  223.    if (what === 'full') frames[i].makeFull();
  224.    if (what === 'random') {
  225.        var many = document.getElementById("random"+i).value;
  226.        frames[i].makeRandom(many);
  227.    }
  228.    if (what === 'opposite') frames[i].makeOpposite();
  229.    if (what === 'predicate') {
  230.        var pred = document.getElementById("predicate"+i).value;
  231.        frames[i].makePredicate(pred);
  232.    }
  233.    if (what === 'transpose') {
  234.        if (i == 0 || i == 1) {
  235.            frames[i].makeTranspose();
  236.        } else { // i == 2
  237.            frames[0].makeTranspose();
  238.            frames[1].makeTranspose();
  239.            [frames[0], frames[1]] = [frames[1], frames[0]];
  240.        }
  241.    }
  242.  
  243.    recalculate();
  244. }
  245.  
  246. function click(i, event) {
  247.    if (event.isTrusted) {
  248.        var rect = c[i].getBoundingClientRect();
  249.        var x = event.clientX - rect.left;
  250.        var y = event.clientY - rect.top;
  251.        var j = Math.max(0, Math.min(n-1, Math.floor(y * n / resn)));
  252.        var k = Math.max(0, Math.min(n-1, Math.floor(x * n / resn)));
  253.        frames[i].data[j][k] = !frames[i].data[j][k];
  254.        frames[i].draw(ctx[0]);
  255.        recalculate();
  256.    }
  257. }
  258.  
  259. var addition = 'OR';
  260. function changeAddition(newAddition) {
  261.    addition = newAddition;
  262.    recalculate();
  263. }
  264.  
  265. function recalculate() {
  266.    if (addition === 'OR') {
  267.        frames[2] = frames[0].dot(frames[1]);
  268.    } else { // addition === 'XOR'
  269.        frames[2] = frames[0].altdot(frames[1]);
  270.    }
  271.  
  272.    frames[0].draw(ctx[0]);
  273.    frames[1].draw(ctx[1]);
  274.    frames[2].draw(ctx[2]);
  275. }
  276.  
  277. </script>
  278.  
  279. </body>
  280. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement