Customise Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.

No cookies to display.

[ad_1]

Activity: to develop a perform that effectively locations rectangular blocks in
rectangular 2D container. The situation of the blocks within the container ought to be
as dense as attainable as a way to rationally use the house of the container.

I’m nearly achieved however I’ve some issues with my code gadgets
overlap one another

Right here is my code however I do not need use some libraries:

<!DOCTYPE html>
<html>
<head>
  <title>Block Placement Algorithm</title>
  <fashion>
    .container {
      width: 500px;
      peak: 500px;
      place: relative;
      border: 1px stable #ccc; 
    }
    .block {
      place: absolute;
      border: 1px stable #000;
    }
  </fashion>
</head>
<physique>
  <div class="container" id="container"></div>

  <script>
 perform checkOverlap(newBlock, existingBlocks) {
  for (const block of existingBlocks) {
    if (
      newBlock.proper >= block.left - blockMargin &&
      newBlock.left <= block.proper + blockMargin &&
      newBlock.backside >= block.high - blockMargin &&
      newBlock.high <= block.backside + blockMargin
    ) {
      return true; // Якщо є перекриття з існуючим блоком
    }
  }
  return false; // Якщо немає перекриття з існуючими блоками
}

    perform calculateBlockArea(blockCoordinates) {
      let totalBlockArea = 0;

      for (const block of blockCoordinates) {
        totalBlockArea += block.width * block.peak;
      }

      return totalBlockArea;
    }

    // Generate block coordinates
    const blockCoordinates = [];
    const containerWidth = 500;
    const containerHeight = 500;
    const maxAttempts = 100;
    const blockMargin = 0; 

    for (let i = 0; i < 10; i++) {
      const width = Math.flooring(Math.random() * 100) + 50;
      const peak = Math.flooring(Math.random() * 100) + 50;

      let newBlock;
      let makes an attempt = 0;
      do {
        newBlock = {
          high: Math.flooring(Math.random() * (containerHeight - peak)),
          left: Math.flooring(Math.random() * (containerWidth - width)),
          proper: 0,
          backside: 0,
          initialOrder: i + 1,
          width: width,
          peak: peak
        };

        newBlock.proper = newBlock.left + width;
        newBlock.backside = newBlock.high + peak;

        makes an attempt++;
      } whereas (checkOverlap(newBlock, blockCoordinates) && makes an attempt < maxAttempts);

      blockCoordinates.push(newBlock);
    }

    // Type blocks by initialOrder
    blockCoordinates.kind((a, b) => a.initialOrder - b.initialOrder);

    // Render blocks on the web page
    const container = doc.getElementById('container');

    let currentTop = 0;
    let currentLeft = 0;

    blockCoordinates.forEach(block => {
      const div = doc.createElement('div');
      div.className="block";
      div.fashion.width = block.width + 'px';
      div.fashion.peak = block.peak + 'px';

      // Set place based mostly on the order with margin
      div.fashion.high = currentTop + 'px';
      div.fashion.left = currentLeft + 'px';
      div.textContent = block.initialOrder;

      const randomColor = "#" + ((1 << 24) * Math.random() | 0).toString(16);
      div.fashion.backgroundColor = randomColor;

      container.appendChild(div);

      currentLeft += block.width + blockMargin; 
      if (currentLeft + block.width > containerWidth) {
        currentTop += block.peak + blockMargin; 
        currentLeft = 0; 
      }
    });
  </script>
</physique>
</html>

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *