Advertisement
dabidabidesh

rects

Jul 30th, 2024 (edited)
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.67 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5.   <meta charset="UTF-8">
  6.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.   <title>Rectangle Drawer</title>
  8. </head>
  9.  
  10. <body>
  11.   <form id="rectForm">
  12.     <label for="R1X1">R1X1:</label>
  13.     <input type="number" id="R1X1" name="R1X1"><br>
  14.     <label for="R1Y1">R1Y1:</label>
  15.     <input type="number" id="R1Y1" name="R1Y1"><br>
  16.     <label for="R1X2">R1X2:</label>
  17.     <input type="number" id="R1X2" name="R1X2"><br>
  18.     <label for="R1Y2">R1Y2:</label>
  19.     <input type="number" id="R1Y2" name="R1Y2"><br>
  20.     <label for="R2X1">R2X1:</label>
  21.     <input type="number" id="R2X1" name="R2X1"><br>
  22.     <label for="R2Y1">R2Y1:</label>
  23.     <input type="number" id="R2Y1" name="R2Y1"><br>
  24.     <label for="R2X2">R2X2:</label>
  25.     <input type="number" id="R2X2" name="R2X2"><br>
  26.     <label for="R2Y2">R2Y2:</label>
  27.     <input type="number" id="R2Y2" name="R2Y2"><br>
  28.     <button type="button" onclick="generateCoordinates();drawRectangles()">Draw Rectangles</button>:<input type="text"
  29.      id="result"> | <input type="text" id="result2">
  30.   </form>
  31.   <canvas id="rectCanvas" width="500" height="500" style="border:1px solid #000000;"></canvas>
  32.  
  33.   <script>
  34.     function generateCoordinates() {
  35.       document.getElementById('R1X1').value = Math.floor(Math.random() * 500);
  36.       document.getElementById('R1Y1').value = Math.floor(Math.random() * 500);
  37.       document.getElementById('R1X2').value = Math.floor(Math.random() * 500);
  38.       document.getElementById('R1Y2').value = Math.floor(Math.random() * 500);
  39.       document.getElementById('R2X1').value = Math.floor(Math.random() * 500);
  40.       document.getElementById('R2Y1').value = Math.floor(Math.random() * 500);
  41.       document.getElementById('R2X2').value = Math.floor(Math.random() * 500);
  42.       document.getElementById('R2Y2').value = Math.floor(Math.random() * 500);
  43.  
  44.       if (+R1X1.value >= +R1X2.value || +R1Y1.value >= +R1Y2.value ||
  45.         +R2X1.value >= +R2X2.value || +R2Y1.value >= +R2Y2.value) {
  46.         generateCoordinates();
  47.       }
  48.     }
  49.     function drawRectangles() {
  50.  
  51.       let p = console.log;
  52.  
  53.       const R1X1 = parseInt(document.getElementById('R1X1').value);
  54.       const R1Y1 = parseInt(document.getElementById('R1Y1').value);
  55.       const R1X2 = parseInt(document.getElementById('R1X2').value);
  56.       const R1Y2 = parseInt(document.getElementById('R1Y2').value);
  57.       const R2X1 = parseInt(document.getElementById('R2X1').value);
  58.       const R2Y1 = parseInt(document.getElementById('R2Y1').value);
  59.       const R2X2 = parseInt(document.getElementById('R2X2').value);
  60.       const R2Y2 = parseInt(document.getElementById('R2Y2').value);
  61.  
  62.       const canvas = document.getElementById('rectCanvas');
  63.       const ctx = canvas.getContext('2d');
  64.  
  65.       // Clear the canvas
  66.       ctx.clearRect(0, 0, canvas.width, canvas.height);
  67.  
  68.       // Draw first rectangle
  69.       ctx.beginPath();
  70.       ctx.rect(R1X1, R1Y1, R1X2 - R1X1, R1Y2 - R1Y1);
  71.       ctx.stroke();
  72.  
  73.       // Draw second rectangle
  74.       ctx.beginPath();
  75.       ctx.rect(R2X1, R2Y1, R2X2 - R2X1, R2Y2 - R2Y1);
  76.       ctx.stroke();
  77.  
  78.       if (R1X1 < R2X2 && R1X2 > R2X1 && R1Y1 < R2Y2 && R1Y2 > R2Y1) {
  79.        result.value = "The rectangles intersect";
  80.       } else {
  81.         result.value = "The rectangles do not intersect";
  82.       }
  83.  
  84.       // Check for overlap
  85.       const overlap = !(R1X2 <= R2X1 || R1X1 >= R2X2 || R1Y2 <= R2Y1 || R1Y1 >= R2Y2);
  86.  
  87.       if (overlap) {
  88.         document.getElementById('result2').value = "The rectangles overlap.";
  89.       } else {
  90.         document.getElementById('result2').value = "The rectangles do not overlap.";
  91.       }
  92.     }
  93.   </script>
  94. </body>
  95.  
  96. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement