z0z0z

Untitled

Sep 22nd, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. let r = document.getElementById("rayVisuliser");
  3. let rctx = r.getContext("2d");
  4. let o = document.getElementById("output");
  5. let octx = o.getContext("2d");
  6.  
  7. let collisionArray = initObject(1,200,1,200);
  8.  
  9. let myObjects = {};
  10.  
  11. newObject(myObjects, "mirror", "mirror", "#000000", 1, 100, 100, 1);
  12. newObject(myObjects, "line", "wall", "#010854", 100,100,100,1);
  13. compileObjects(myObjects, collisionArray);
  14.  
  15. visuliseCollisionArray(collisionArray, rctx);
  16.  
  17. function traceRay(angle, position, collisionArray, output) {
  18.  
  19. }
  20.  
  21. function drawLineFromAngle (angle, position) {
  22.    let line = {};
  23.    let renderDistance = 200;
  24.    if (Number(angle) == 90) {
  25.       let counter = 0;
  26.       let x = position.x;
  27.       let y = position.y;
  28.       while (counter <= renderDistance) {
  29.          line = {x,y};
  30.          y++;
  31.          counter++;
  32.       }
  33.       return line;
  34.    } else {
  35.       if (Number(angle) == 270) {
  36.  
  37.  
  38.  
  39.       }
  40.       else {
  41.          let gradient = Math.tan(angle);
  42.  
  43.  
  44.       }
  45.    }
  46. }
  47.  
  48. function newObject(objects, name, type, color, x1, y1, x2, y2) {
  49.    objects[`${name}`] = {};
  50.    objects[`${name}`].x1 = x1;
  51.    objects[`${name}`].y1 = y1;
  52.    objects[`${name}`].x2 = x2;
  53.    objects[`${name}`].y2 = y2;
  54.    objects[`${name}`].type = type;
  55.    objects[`${name}`].color = color;
  56. }
  57.  
  58. function visuliseCollisionArray(collisionArray, ctx) {
  59.    for (x in collisionArray) {
  60.       for (y in collisionArray) {
  61.          if (collisionArray[`${x}`][`${y}`].isOccupied) {
  62.             setPixel(x,y,collisionArray[`${x}`][`${y}`].color, ctx);
  63.          }
  64.       }
  65.    }
  66. }
  67.  
  68. function compileObjects(objects, collisionArray) {
  69.    for (object in objects) {
  70.       objects[`${object}`]
  71.       let line = getLine(objects[`${object}`].x1,objects[`${object}`].y1,objects[`${object}`].x2,objects[`${object}`].y2);
  72.       let angle = getAngle(objects[`${object}`].x1,objects[`${object}`].y1,objects[`${object}`].x2,objects[`${object}`].y2);
  73.       for (point in line) {
  74.          let x = line[`${point}`].x;
  75.          let y = line[`${point}`].y;
  76.          if (collisionArray[`${x}`][`${y}`].isOccupied) {
  77.             console.log(`collisionArray ${x}, ${y} got overwritten`)
  78.          }
  79.          collisionArray[`${x}`][`${y}`].isOccupied = true;
  80.          collisionArray[`${x}`][`${y}`].type = objects[`${object}`].type;
  81.          collisionArray[`${x}`][`${y}`].angle = angle;
  82.          if (objects[`${object}`].type == "mirror") {
  83.             collisionArray[`${x}`][`${y}`].color = "#03fc0f";
  84.          }
  85.          else {
  86.             if (objects[`${object}`].type == "wall") {
  87.                collisionArray[`${x}`][`${y}`].color = objects[`${object}`].color;
  88.             }
  89.             else {
  90.                collisionArray[`${x}`][`${y}`].color = "#000000";
  91.             }
  92.          }
  93.       }
  94.    }
  95. }
  96.  
  97. function drawLine(x1, y1, x2, y2) {
  98.    let x, y, dx, dy, dx1, dy1, px, py, xe, ye, i;
  99.    dx = x2 - x1;
  100.    dy = y2 - y1;
  101.    dx1 = Math.abs(dx);
  102.    dy1 = Math.abs(dy);
  103.    px = 2 * dy1 - dx1;
  104.    py = 2 * dx1 - dy1;
  105.    if (dy1 <= dx1) {
  106.       if (dx >= 0) {
  107.          x = x1;
  108.          y = y1;
  109.          xe = x2;
  110.       } else { // Line is drawn right to left (swap ends)
  111.          x = x2;
  112.          y = y2;
  113.          xe = x1;
  114.       }
  115.       setPixel(x, y);
  116.       for (i = 0; x < xe; i++) {
  117.          x = x + 1;
  118.          if (px < 0) {
  119.             px = px + 2 * dy1;
  120.          } else {
  121.             if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0)) {
  122.                y = y + 1;
  123.             } else {
  124.                y = y - 1;
  125.             }
  126.             px = px + 2 * (dy1 - dx1);
  127.          }
  128.          setPixel(x, y);
  129.       }
  130.    } else { // The line is Y-axis dominant
  131.       if (dy >= 0) {
  132.          x = x1;
  133.          y = y1;
  134.          ye = y2;
  135.       } else { // Line is drawn top to bottom
  136.          x = x2;
  137.          y = y2;
  138.          ye = y1;
  139.       }
  140.       setPixel(x, y); // Draw first pixel
  141.       for (i = 0; y < ye; i++) {
  142.          y = y + 1;
  143.          if (py <= 0) {
  144.             py = py + 2 * dx1;
  145.          } else {
  146.             if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0)) {
  147.                x = x + 1;
  148.             } else {
  149.                x = x - 1;
  150.             }
  151.             py = py + 2 * (dx1 - dy1);
  152.          }
  153.          setPixel(x, y);
  154.       }
  155.    }
  156. }
  157.  
  158. function getLine(x1, y1, x2, y2) {
  159.    let x, y, dx, dy, dx1, dy1, px, py, xe, ye, i;
  160.    let line = {};
  161.    let pxCounter = 1;
  162.    dx = x2 - x1;
  163.    dy = y2 - y1;
  164.    dx1 = Math.abs(dx);
  165.    dy1 = Math.abs(dy);
  166.    px = 2 * dy1 - dx1;
  167.    py = 2 * dx1 - dy1;
  168.    if (dy1 <= dx1) {
  169.       if (dx >= 0) {
  170.          x = x1;
  171.          y = y1;
  172.          xe = x2;
  173.       } else { // Line is drawn right to left (swap ends)
  174.          x = x2;
  175.          y = y2;
  176.          xe = x1;
  177.       }
  178.       line[`${pxCounter}`] = {};
  179.       line[`${pxCounter}`].x = x;
  180.       line[`${pxCounter}`].y = y;
  181.       pxCounter++;
  182.       for (i = 0; x < xe; i++) {
  183.          x = x + 1;
  184.          if (px < 0) {
  185.             px = px + 2 * dy1;
  186.          } else {
  187.             if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0)) {
  188.                y = y + 1;
  189.             } else {
  190.                y = y - 1;
  191.             }
  192.             px = px + 2 * (dy1 - dx1);
  193.          }
  194.          line[`${pxCounter}`] = {};
  195.          line[`${pxCounter}`].x = x;
  196.          line[`${pxCounter}`].y = y;
  197.          pxCounter++;
  198.       }
  199.    } else { // The line is Y-axis dominant
  200.       if (dy >= 0) {
  201.          x = x1;
  202.          y = y1;
  203.          ye = y2;
  204.       } else { // Line is drawn top to bottom
  205.          x = x2;
  206.          y = y2;
  207.          ye = y1;
  208.       }
  209.       line[`${pxCounter}`] = {};
  210.       line[`${pxCounter}`].x = x;
  211.       line[`${pxCounter}`].y = y;
  212.       pxCounter++;
  213.       for (i = 0; y < ye; i++) {
  214.          y = y + 1;
  215.          if (py <= 0) {
  216.             py = py + 2 * dx1;
  217.          } else {
  218.             if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0)) {
  219.                x = x + 1;
  220.             } else {
  221.                x = x - 1;
  222.             }
  223.             py = py + 2 * (dx1 - dy1);
  224.          }
  225.          line[`${pxCounter}`] = {};
  226.          line[`${pxCounter}`].x = x;
  227.          line[`${pxCounter}`].y = y;
  228.          pxCounter++;
  229.       }
  230.    }
  231.    return line;
  232. }
  233.  
  234. function getAngle(x1,y1,x2,y2) {
  235.    return rad2deg(Math.atan(1/Math.abs((x1-x2)/(y1-y2))));
  236. }
  237.  
  238. function rad2deg(radians)
  239. {
  240.    return 180 * radians/Math.PI;
  241. }
  242.  
  243. function setPixel(x,y,color, ctx) {
  244.    ctx.fillStyle = color;
  245.    ctx.fillRect(x - 1,y - 1,1,1);
  246.    ctx.fillStyle = "#000000";
  247. }
  248.  
  249. function initObject(minX,maxX,minY,maxY) {
  250.    let object = {};
  251.    let xCounter = minX;
  252.    let yCounter = minY;
  253.    while(xCounter <= maxX) {
  254.       object[`${xCounter}`] = {};
  255.       yCounter = minY;
  256.       while(yCounter <= maxY) {
  257.          object[`${xCounter}`][`${yCounter}`] = {};
  258.          yCounter++;
  259.       }
  260.       xCounter++;
  261.    }
  262.    return object;
  263. }
  264.  
  265. function getRandomObjectValues(object,objectFor,minX,maxX,minY,maxY,minRand,maxRand) {
  266.    let xCounter = minX;
  267.    let yCounter = minY;
  268.    while(xCounter <= maxX) {
  269.       yCounter = minY;
  270.       while(yCounter <= maxY) {
  271.          object[`${xCounter}`][`${yCounter}`][`${objectFor}`] = getRndInteger(minRand,maxRand);
  272.          yCounter++;
  273.       }
  274.       xCounter++;
  275.    }
  276. }
  277.  
  278. function getRndInteger(min,max) {
  279.    return Math.floor(Math.random() * (max - min + 1) ) + min;
  280. }
Advertisement
Add Comment
Please, Sign In to add comment