You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Processing绘制满足到两点距离乘积p<c²/4的点集无输出问题排查

问题

我需要在水平线上设定距离为c的两点,绘制所有到这两点距离乘积等于p < c²/4的点集。用Processing实现时,输出仅显示连线但无目标点;但当设置p = c²/4时,代码能正常在连线中点显示红点。

失效代码(p < c²/4时)

void setup() {
  size(800, 600);
  background(255);
  stroke(0);
  float c = 700;
  float p = (c * c) / 4;
  float x1 = (width - c) / 2;
  float x2 = x1 + c;
  float y = height / 2;
  fill(0, 0, 255);
  stroke(0, 0, 255);
  ellipse(x1, y, 10, 10);
  ellipse(x2, y, 10, 10);
  line(x1, y, x2, height / 2);
  strokeWeight(4);
  float threshold = 30.0; // Increased threshold for better visibility

  for (float x = x1; x <= x2; x += 1) {
    float d1 = dist(x, y, x1, y);
    float d2 = dist(x, y, x2, y);

    // Calculate the product of distances
    float product = d1 * d2;

    // Check if the product is equal to p and p < c^2/4
    if (abs(product - p) < threshold && p < (c * c) / 4) {
      stroke(255, 0, 0);
      point(x, y);
    }
  }
}

void draw() {
  
}

正常代码(p = c²/4时)

void setup() {
  size(800, 600);
  background(255);
  stroke(0);
  float c = 700;
  float p = (c * c)/4;
  float x1 = (width - c) / 2;
  float x2 = x1 + c;
  float y = height / 2;
  fill(0, 0, 255);
  stroke(0, 0, 255);
  ellipse(x1, y, 10, 10);
  ellipse(x2, y, 10, 10);
  line(x1, y, x2, height / 2);
  float threshold = 30.0; // Increased threshold for better visibility

  for (float x = x1; x <= x2; x += 1) {
    float d1 = dist(x, y, x1, y);
    float d2 = dist(x, y, x2, y);
    
    // Calculate the product of distances
    float product = d1 * d2;
    
    // Check if the product is within a certain range of p
    if (abs(product - p) < threshold) {
      stroke(255, 0, 0); 
      ellipse(x, y, 5, 5);
    }
  }
}

void draw() {
}
问题原因
  1. 参数赋值矛盾:失效代码中p被赋值为c²/4,但判断条件又要求p < c²/4,这个逻辑永远为假,因此不会触发任何点的绘制。
  2. 遍历范围错误:你只遍历了两点连线上的x值,且固定了y坐标。但当p < c²/4时,满足条件的点构成卡西尼卵形线,这类点不在两点的水平连线上,需要遍历整个画布的所有坐标。
  3. 冗余条件判断:p < c²/4是你设定的参数前提,不需要在循环的判断逻辑中重复校验,应直接将p设为符合要求的值。
修复方案

调整p的赋值,扩展遍历范围到整个画布,并移除冗余判断:

void setup() {
  size(800, 600);
  background(255);
  stroke(0);
  float c = 700;
  // 直接设置p为小于c²/4的值,比如c²/5
  float p = (c * c) / 5;
  float x1 = (width - c) / 2;
  float x2 = x1 + c;
  float centerY = height / 2;
  
  // 绘制两个固定点
  fill(0, 0, 255);
  stroke(0, 0, 255);
  ellipse(x1, centerY, 10, 10);
  ellipse(x2, centerY, 10, 10);
  line(x1, centerY, x2, centerY);
  
  strokeWeight(2);
  float threshold = 50.0; // 根据显示效果调整阈值

  // 遍历整个画布的所有像素点
  for (float x = 0; x < width; x += 1) {
    for (float y = 0; y < height; y += 1) {
      float d1 = dist(x, y, x1, centerY);
      float d2 = dist(x, y, x2, centerY);
      float product = d1 * d2;
      
      // 判断距离乘积是否接近p
      if (abs(product - p) < threshold) {
        stroke(255, 0, 0);
        point(x, y);
      }
    }
  }
}

void draw() {
}
修复说明
  • 修正p的赋值为明确小于c²/4的值,避免逻辑矛盾
  • 改为双重循环遍历整个画布的x和y坐标,覆盖所有可能的点
  • 移除冗余的p < c²/4判断,简化逻辑
  • 调整阈值和笔触大小,让卵形线显示更清晰

内容的提问来源于stack exchange,提问作者Anon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 21:45:05