Find the percentage of the figure area covered by own drawing

There is simple shape(red triangle) and complex(black “U shape”).
I draw blue shape by mouse and willing to find: percentage of the figure been covered by drawing(blue color).

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Canvas</title>
    <style>
      canvas {
        border: 1px solid black;
      }
    </style>
  </head>
  <body>
    <canvas id="canvas" width="800" height="600"></canvas>
    <script>
      const canvas = document.getElementById("canvas");
      const ctx = canvas.getContext("2d");
      const radius = 15;

      const random_shape = [
        { x: 20, y: 20, width: 20, height: 100 },
        { x: 60, y: 20, width: 20, height: 100 },
        { x: 20, y: 120, width: 60, height: 20 },
      ];

      const triangle = [
        { x: 200, y: 400 },
        { x: 400, y: 200 },
        { x: 600, y: 400 },
      ];

      let isDrawing = false;
      let lastX = 0;
      let lastY = 0;
      let pixelsInsideFigure = 0;

      function draw_random_shape() {
        for (let i = 0; i < random_shape.length; i++) {
          ctx.fillStyle = "black";
          ctx.fillRect(
            random_shape[i].x,
            random_shape[i].y,
            random_shape[i].width,
            random_shape[i].height
          );
        }
      }

      function draw_triangle() {
        ctx.beginPath();
        ctx.moveTo(triangle[0].x, triangle[0].y);
        for (let i = 1; i < triangle.length; i++) {
          ctx.lineTo(triangle[i].x, triangle[i].y);
        }
        ctx.closePath();
        ctx.fillStyle = "red";
        ctx.fill();
      }

      function handleMouseDown(e) {
        isDrawing = true;
        [lastX, lastY] = [e.offsetX, e.offsetY];
        if (pointInShape({ x: lastX, y: lastY }, random_shape)) {
          pixelsInsideFigure++;
        }
      }

      function handleMouseMove(e) {
        if (!isDrawing) return;
        ctx.strokeStyle = "blue";
        ctx.lineJoin = "round";
        ctx.lineCap = "round";
        ctx.lineWidth = radius;
        ctx.beginPath();
        ctx.moveTo(lastX, lastY);
        ctx.lineTo(e.offsetX, e.offsetY);
        ctx.stroke();
        [lastX, lastY] = [e.offsetX, e.offsetY];
        if (pointInShape({ x: lastX, y: lastY }, random_shape)) {
          pixelsInsideFigure++;
        }
      }

      function handleMouseUp() {
        isDrawing = false;
        calculatePercentage();
        pixelsInsideFigure = 0;
      }

      function clearUserInput() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        draw_triangle();
        draw_random_shape();
      }

      function calculatePercentage() {
        const coveredArea = calculateCoveredArea(
          { x: lastX, y: lastY },
          radius
        );
        const totalArea = Math.PI * Math.pow(radius, 2);
        const coveragePercentage = (coveredArea / totalArea) * 100;

        alert(`Area Coverage Percentage: ${coveragePercentage.toFixed(2)}%`);
        clearUserInput();
      }

      function pointInShape(point, vertices) {
        let inside = false;
        const x = point.x;
        const y = point.y;
        for (let i = 0, j = vertices.length - 1; i < vertices.length; j = i++) {
          const xi = vertices[i].x;
          const yi = vertices[i].y;
          const xj = vertices[j].x;
          const yj = vertices[j].y;
          const intersect =
            yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
          if (intersect) inside = !inside;
        }
        return inside;
      }

      function calculateCoveredArea(point, radius) {
        let coveredArea = 0;
        const centerX = point.x;
        const centerY = point.y;

        for (let x = centerX - radius; x <= centerX + radius; x++) {
          for (let y = centerY - radius; y <= centerY + radius; y++) {
            const distance = Math.sqrt(
              Math.pow(x - centerX, 2) + Math.pow(y - centerY, 2)
            );
            if (distance <= radius) {
              if (pointInShape({ x: x, y: y }, random_shape)) {
                console.log("INSIDE RANDOM SHAPE");
                coveredArea++;
              }
              if (pointInShape({ x: x, y: y }, triangle)) {
                console.log("INSIDE Triangle");
                coveredArea++;
              }
            }
          }
        }

        return coveredArea;
      }

      function calculateArea(vertices) {
        let area = 0;
        for (let i = 0, j = vertices.length - 1; i < vertices.length; j = i++) {
          area +=
            (vertices[j].x + vertices[i].x) * (vertices[j].y - vertices[i].y);
        }
        return Math.abs(area / 2);
      }

      function init() {
        draw_triangle();
        draw_random_shape();

        canvas.addEventListener("mousedown", handleMouseDown);
        canvas.addEventListener("mousemove", handleMouseMove);
        canvas.addEventListener("mouseup", handleMouseUp);
      }

      init();
    </script>
  </body>
</html>

P.S. As reference in Harry Potter 1 I have to lead a wand to fit the shape. To pass the skill you have to properly cover the area.

In provided case I am expecting to get around 60-70%(top left corner).

Searching the internet, asking “bots”, did math adjustments – did not help.
Expecting: proper way to calculate and find the area percentage.

First thing that comes to my mind, you may count total pixels which are showed up after user drawing, then you can compare with target shape pixels count. There is important thing to check counted pixels should be inside boundaries of target shape

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật