在P5J中绘制平滑线

发布于 2025-01-21 14:46:19 字数 623 浏览 3 评论 0原文

我的功能创建了一个函数,该函数从其第一个参数的坐标中绘制了一条线,并将其绘制为第二个参数的坐标。代码使用鼠标坐标作为该函数的参数运行该函数。但是,我遇到一个问题,即垂直移动鼠标光标会导致行不均匀。

let circles = [];

function setup() {
  createCanvas(600, 600);
}

function draw() {
  lineCreate([pmouseX,pmouseY],[mouseX,mouseY])
  
}

function lineCreate(point1, point2) {
  length = sqrt(pow(point2[0]-point1[0],2) + pow(point2[1]-point1[1],2))
  slope = (point2[1]-point1[1])/(point2[0]-point1[0])
  x = min(point1[0],point2[0])
  endX = max(point1[0],point2[0])
  for (let i = x; i < endX; i++) {
    pointSlope = slope*(i - point1[0]) + point1[1]
    circle(i,pointSlope,2,2)
  }
}

I have a function that I created which draws a line from the coordinates of its first argument to the coordinates of the second argument. The code runs the function using the mouse coordinates as the argument for the function. However, I am having an issue where moving the mouse cursor vertically will cause the line to be uneven.

let circles = [];

function setup() {
  createCanvas(600, 600);
}

function draw() {
  lineCreate([pmouseX,pmouseY],[mouseX,mouseY])
  
}

function lineCreate(point1, point2) {
  length = sqrt(pow(point2[0]-point1[0],2) + pow(point2[1]-point1[1],2))
  slope = (point2[1]-point1[1])/(point2[0]-point1[0])
  x = min(point1[0],point2[0])
  endX = max(point1[0],point2[0])
  for (let i = x; i < endX; i++) {
    pointSlope = slope*(i - point1[0]) + point1[1]
    circle(i,pointSlope,2,2)
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风透绣罗衣 2025-01-28 14:46:19

您需要将点与行连接:

function setup() {
    createCanvas(600, 600);
    strokeWeight(4);
    noFill();
    rect(2, 2, 596, 596);
}

function draw() {
    line(pmouseX, pmouseY, mouseX,mouseY)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>

如果您想用各个点绘制界线,则需要区分沿x轴还是y轴之间的距离较大:

function setup() {
    createCanvas(600, 600);
    strokeWeight(4);
    noFill();
    rect(2, 2, 596, 596);
    fill(0);
}

function draw() {
    lineCreate([pmouseX,pmouseY], [mouseX,mouseY])  
}

function lineCreate(point1, point2) {
    x0 = point1[0];
    x1 = point2[0];
    y0 = point1[1];
    y1 = point2[1];
    if (abs(x1 - x0) > abs(y1 - y0)) {
        if (x0 > x1) {
            let t = x0; x0 = x1; x1 = t;
            t = y0; y0 = y1; y1 = t;
        }
        for (let x = x0; x <= x1; x++) {
            let y = y0 + (y1-y0) * (x-x0) / (x1-x0);
            circle(x, y, 2);
        }
    } else {
        if (y0 > y1) {
            let t = x0; x0 = x1; x1 = t;
            t = y0; y0 = y1; y1 = t;
        }
        for (let y = y0; y <= y1; y++) {
            let x = x0 + (x1-x0) * (y-y0) / (y1-y0);
            circle(x, y, 2);
        }
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>

You need to connect the dots with lines:

function setup() {
    createCanvas(600, 600);
    strokeWeight(4);
    noFill();
    rect(2, 2, 596, 596);
}

function draw() {
    line(pmouseX, pmouseY, mouseX,mouseY)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>

If you want to draw the line with individual points, you need to distinguish whether the distance between the points is larger along the x-axis or the y-axis:

function setup() {
    createCanvas(600, 600);
    strokeWeight(4);
    noFill();
    rect(2, 2, 596, 596);
    fill(0);
}

function draw() {
    lineCreate([pmouseX,pmouseY], [mouseX,mouseY])  
}

function lineCreate(point1, point2) {
    x0 = point1[0];
    x1 = point2[0];
    y0 = point1[1];
    y1 = point2[1];
    if (abs(x1 - x0) > abs(y1 - y0)) {
        if (x0 > x1) {
            let t = x0; x0 = x1; x1 = t;
            t = y0; y0 = y1; y1 = t;
        }
        for (let x = x0; x <= x1; x++) {
            let y = y0 + (y1-y0) * (x-x0) / (x1-x0);
            circle(x, y, 2);
        }
    } else {
        if (y0 > y1) {
            let t = x0; x0 = x1; x1 = t;
            t = y0; y0 = y1; y1 = t;
        }
        for (let y = y0; y <= y1; y++) {
            let x = x0 + (x1-x0) * (y-y0) / (y1-y0);
            circle(x, y, 2);
        }
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文