【问题标题】:Cannot convert from void to PVector in Processing无法在处理中从 void 转换为 PVector
【发布时间】:2014-01-08 00:29:15
【问题描述】:

我正在学习处理并尝试使用帕斯卡三角形生成分形。这需要PVectors 的数组。我遇到了一个我不明白的问题——我收到了cannot convert from void to PVector

这是一个摘录,并非每个变量都在摘录中定义,但我认为问题可能在于我不需要透露比这更多的代码 - 显示的更多代码可能只会混淆问题。

arrayCopy(points,old_points);
points = new PVector[int(pow(2, j))];
if (j == 1)
{
  points[0] = new PVector(0,0);
}
if (j == 2)
{
  points[0] = new PVector(1,0);
  points[1] = new PVector(-1,0);
}
else
{
  //j will be 3 for the THIRD term in the series
  int number_of_terms_to_fill = int(pow(j - 1, 2));
  int[] pasc = PascalTriangle(j - 1);
  float real = findReal(pasc, x, y, number_of_terms_to_fill);
  float imagi = findImagi(pasc, x, y, number_of_terms_to_fill);
  PVector v = new PVector(real, imagi);
  for (int k = 0; k < number_of_terms_to_fill; k = k + 2)
  {
    points[k] = old_points[k].add(v); //!!***PROBLEM LINE***!!!
    points[k+1] = old_points[k].sub(v); 
  }
}

我的其他函数,比如findRealfindImagi,我相信是正确的。问题行上的加法应该是两个PVectors之间的加法——合法操作。相反,有些东西是无效的?也许arrayCopy 不是我想要的深拷贝?

不知道发生了什么。

【问题讨论】:

    标签: processing


    【解决方案1】:

    .add() 方法不返回 PVector(它什么也不返回,因此无效)所以你不能这样做

    points[k] = old_points[k].add(v);
    

    我想你想要做的是将 v 添加到 old_points[k] 并将它传递给 points[k] 这不会像那样工作......你必须这样做:

    old_points[k].add(v);
    points[k] = old_points[k];
    old_points[k].sub(v);
    points[k+1] = old_points[k];
    

    为了使你写的内容有效,add 函数应该是这样的:

    PVector add(PVector v) {
      this.x += v.x;
      this.y += v.y;
      this.z += v.z;
      return this;
    }
    

    相反,它看起来像这样:

    void add(PVector v) {
      this.x += v.x;
      this.y += v.y;
      this.z += v.z;
    }
    

    编辑:在下面 Ryan 的评论之后,我在这里提供另一种方式,使用返回 PVector 的静态 .add() 方法...

    points[k] = PVector.add(old_points[k],v);
    points[k+1] = PVector.sub(old_points[k],v);
    

    【讨论】:

    • 其实the referenceReturns void or PVector
    • 我明白了。加法和减法运算意味着可以独立使用。很公平。谢谢!
    • @RyanCarlson 编辑,@PinkElephantsOnParade 欢迎您!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多