Advertisement
Guest User

trngle

a guest
Oct 15th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.49 KB | None | 0 0
  1. <canvas id="my-house" width="800" height="600"></canvas>
  2.  
  3. <style type="text/css">
  4.     #my-house {
  5.         border-style: solid;
  6.         border-width: 5px;
  7.     }
  8. </style>
  9.  
  10. <script type="text/javascript">
  11.  
  12.     function Point(x, y) {
  13.         this.x = x;
  14.         this.y = y;
  15.     }
  16.  
  17.    
  18.  
  19.  
  20.  
  21.     function drawLine(ctx, fx, fy, tx, ty) {
  22.         ctx.moveTo(fx + Math.floor(ctx.canvas.width / 2), ctx.canvas.height - (fy + Math.floor(ctx.canvas.height / 2))  );
  23.         ctx.lineTo(tx + Math.floor(ctx.canvas.width / 2), ctx.canvas.height - (ty + Math.floor(ctx.canvas.height / 2))  );
  24.     }
  25.    
  26.     function len(p1, p2) {
  27.         return Math.sqrt(Math.pow(p2.x - p1.x) + Math.pow(p2.y - p1.y));
  28.     }
  29.    
  30.     function slope(p1, p2) {
  31.         return (p2.y - p1.y) / (p2.x - p1.x);
  32.     }
  33.  
  34.  
  35.     const canvas = document.getElementById('my-house');
  36.     const ctx = canvas.getContext('2d');
  37.     // Set line width
  38.     ctx.lineWidth = 1;
  39.    
  40.     var v1 = new Point(20, 20);
  41.     var v2 = new Point(120, 0);
  42.     var v3 = new Point(70, 80);
  43.  
  44.     // drawLine(ctx, 0, -300, 0, 300);
  45.     // drawLine(ctx, -400, 0, 400, 0);
  46.  
  47.     // performance.now()
  48.     ctx.beginPath();
  49.     // var line1 = len(v1, v2);
  50.     // var line2 = len(v1, v3);
  51.     // var m = slope(v1, v3);
  52.    
  53.     for(var t = 0; t <= 1; t += 0.01) {
  54.         var fx = v1.x + (v2.x - v1.x) * t;
  55.         var fy = v1.y + (v2.y - v1.y) * t;
  56.        
  57.         var gx = v1.x + (v3.x - v1.x) * t;
  58.         var gy = v1.y + (v3.y - v1.y) * t;
  59.        
  60.         fx = Math.round(fx);
  61.         fy = Math.round(fy);
  62.         gx = Math.round(gx);
  63.         gy = Math.round(gy);
  64.        
  65.         drawLine(ctx, fx, fy, gx, gy);
  66.     }
  67.     ctx.stroke();
  68.  
  69. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement