简体   繁体   English

如何使用 p5.js 在多边形内绘制矩形

[英]How to draw rectangles inside a polygon using p5.js

I am working on a project for a solar panel installation calculator on roofs.我正在做一个屋顶太阳能电池板安装计算器的项目。 The solar panels are rectangular and have fixed widths and heights.太阳能电池板是矩形的,具有固定的宽度和高度。 But the roofs are dynamic in shape, ie a polygon with multiple sides.但屋顶的形状是动态的,即具有多个边的多边形。 See the attached image for reference.请参阅附图以供参考。

I am trying to find out the maximum number of panels that can be installed on the roofs.我试图找出可以安装在屋顶上的最大面板数量。 I need to place the rectangles (panels) inside the polygon (roof), which you can not overlap and can not touch the boundary.我需要将矩形(面板)放在多边形(屋顶)内,你不能重叠也不能接触边界。

I was trying to achieve this using p5.js.我试图使用 p5.js 来实现这一点。

Here are the basic codes.以下是基本代码。

function setup() {
    createCanvas(800, 500);
    background(220);
    noLoop();
}
  
function draw() {
    fill(237, 34, 93);
    beginShape();
        vertex(50, 50);
        vertex(750, 50);
        vertex(750, 450);
        vertex(500, 450);
        vertex(500, 200);
        vertex(350, 200);
        vertex(350, 450);
        vertex(50, 450);
    endShape(CLOSE);

    var solarPanel = {
        width: 40,
        height: 40
    };
    
    //Algorithm to add the solar panels.
}

Link to p5.js editor - https://editor.p5js.org/abhishekdas/sketches/cVoxihH0n链接到 p5.js 编辑器 - https://editor.p5js.org/abhishekdas/sketches/cVoxihH0n

I am new to p5.js.我是 p5.js 的新手。 Can anyone please help me to find the correct Algorithm?谁能帮我找到正确的算法?

One simple option is to lay the panels in a grid.一个简单的选择是将面板放置在网格中。 Your typical nested for loop should do the trick: remember to add the spacing required between panels when computing the position of each one (this should take care of overlaps).您的典型嵌套 for 循环应该可以解决问题:记住在计算每个面板的 position 时添加面板之间所需的间距(这应该注意重叠)。

Regarding panels laying inside the polygon of the shape you can get started with this point in polygon p5.js example .关于放置在形状多边形内的面板,您可以从多边形 p5.js 示例中的这一点开始。 If you can test one point, you can check 4 points.如果能考1分,就可以考4分。 If you can check 4 points (1 panel), you can check all panels) then simply remove the ones that aren't inside the roof polygon from the grid.如果您可以检查 4 个点(1 个面板),您可以检查所有面板)然后只需从网格中删除不在屋顶多边形内的那些。

For reference here's function:参考这里的 function:

function pointInPoly(verts, pt) {
  let c = false;
  for (let i = 0, j = verts.length - 1; i < verts.length; j = i++) {
    if (((verts[i].y > pt.y) != (verts[j].y > pt.y)) && (pt.x < (verts[j].x - verts[i].x) * (pt.y - verts[i].y) / (verts[j].y - verts[i].y) + verts[i].x)) c = !c;
  }
  return c;
}

...where verts is an array of p5.Vector objects and pt is also a p5.Vector (though a basic object .x, .y properties will also work) ...其中 verts 是p5.Vector对象的数组,pt 也是p5.Vector (尽管基本的 object .x, .y属性也可以使用)

That should be a simple straight forward approach.这应该是一个简单直接的方法。

If you need more complex options you can look into square/rectangle packing algorithms / simulations and optimisations etc. though given the point is to set up solar panels, the real world probably the simpler setup to install and maintain on the long run will outweight potentially space saving layouts that are odd shaped.如果您需要更复杂的选项,您可以查看方形/矩形包装算法/模拟和优化等。尽管重点是设置太阳能电池板,但从长远来看,现实世界可能更简单的安装和维护设置可能会超过形状奇特的节省空间的布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM