javascript - 如何在 p5.js 中的点数组之间绘制正方形?

标签 javascript algorithm geometry processing p5.js

我有一个包含 6 组随机 x、y 坐标的数组。我可以绘制连接点的线,如下所示:

6 points connected by lines

但是,我无法弄清楚如何在线条所在的位置绘制重复的正方形(以实现虚线效果)。现在,我能够得到这个,但我只想沿着这条线画正方形:

enter image description here

我理解我在理论上做错了什么,但在这里的数学真的很挣扎。我如何只包括沿线出现的正方形? TIA。

这是我现在的代码:

let w = 500;
let h = 500;
let riverArr = [];
let distanceArr = [];

function setup() {
    createCanvas(w, h);
}

function draw() {
    background(240);
    generatePoints();

    for (i = 1; i < riverArr.length; i++) {
        let x1 = parseInt(riverArr[i][0]);
        let y1 = parseInt(riverArr[i][1]);

        let x2 = parseInt(riverArr[i - 1][0]);
        let y2 = parseInt(riverArr[i - 1][1]);

        line(x1, y1, x2, y2);

        let d = int(dist(x1, y1, x2, y2));

        ellipse(x1, y1, 5);

        for (j = 0; j <= x1; j++) {
            for (k = 0; k <= y1; k++){
                rect(j, k, 1);
            }
        }
        distanceArr.push(d);
    }
    noLoop();
}

function generatePoints() {
    let finished;
    riverArr.push([0, random(h)]);
    for (i = 0; i < 4; i++) {
        finished == false;
        riverArr.push([random(w), random(h)]);
        if (i > 0 && riverArr[i][0] <= riverArr[i - 1][0]) {
            console.log(riverArr[i][0], riverArr[i - 1][0]);
            console.log('Bad path');
        }
        finished == true;
    }
    riverArr.push([w, random(h)]);
}

最佳答案

像这样的东西?

let points = [];
const rectSize = 10;

function setup() {
    createCanvas(400, 400);
    // add random points to a list
    for (let i = 0; i < 4; i++)
        points.push(createVector(random(0, width), random(0, height)));

    rectMode(CENTER);
    fill(0);
    noStroke();
}

function draw() {
    background(255);
    // from the first point to the one before last
    for (let i = 0; i < points.length - 1; i++) {
        // get a reference to the current and next point
        const current = points[i], next = points[i + 1];
        // calculate how many rectangles to draw in between two points
        // 1. calculate the distance between the current and next point
        // 2. divide the distance by twice the rectangle size (to achieve an equal gap between drawn and not drawn rectangles
        // 3. round down (floor) the divided distance
        const count = floor(dist(current.x, current.y, next.x, next.y) / (rectSize * 2));
        // compute direction of movement vector from one point to the next
        // 1. subtract current vector from next vector (e.g. each componeny (x,y))
        // 2. divide direction vector by how many rectangles
        // 3. store result in a p5.js Vector object (e.g. if count = 5, adding dir 5 times to current point gets us to the next point
        const dir = createVector((next.x - current.x) / count, (next.y - current.y) / count);
        // for each inbetween rectangles
        for (let j = 0; j < count; j++)
        // draw it by adding to the current position the direction towards the next position multiplied for each rectangle
            rect(current.x + dir.x * j, current.y + dir.y * j, rectSize, rectSize);
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.0.0/p5.min.js"></script>


它没有使用您的逻辑,但我认为调整它不会太难。

我也刚刚注意到,在您的 generatePoints()方法,你说的是 finished == true/false .不知道是什么目的finished服务,但您可能应该更改 ===分配,而不是比较。

关于javascript - 如何在 p5.js 中的点数组之间绘制正方形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61556768/

相关文章:

opengl - OpenGL立方体贴图中的面约定

algorithm - 判断线段的方向是顺时针还是逆时针

java - 生成数字等于一组数字的最坏情况复杂度

algorithm - 这3个类轮是什么排序算法?

c++ - 计算第 n 个不包含给定数字的数字的程序

javascript - AJAX调用在html div中显示api键和值

c++ - 旋转一个 vector 以达到与另一个 vector 的正交性

javascript - 如何在流式传输轨道时处理 SoundCloud 的 JavaScript API 404 错误

javascript - Canvas toDataURL() 函数完成后执行函数

javascript - GWT:弹出窗口阻止程序阻止 RPC 后打开窗口