当前位置: 首页 > 知识库问答 >
问题:

仅当使用fill()时处理抛出java.lang.AssertionError的草图

明宜年
2023-03-14

本研究基于以下环节。geomerative库文档可以从以下链接中看到。

//build a bezier curve using the recursive polynomial form
//number of control points(num+1 points)
int num = 4;
//arraylist to store the picked values
ArrayList<Point> pt;
float nfac;

import geomerative.*;

RFont fnt;
RGroup rg;
RPoint [][]rp;

String str = "(O_o)/ Oooh";

FloatList X;
FloatList Y;
FloatList SUM;

void setup(){
  size(1000,1000,P2D);
  RG.init(this);
  pt = new ArrayList<Point>();
  //pick a number of points with random positions and store x,y values in them
  for(int i=0; i<=num; i++){
    float x = random(0,width);
    float y = random(0,height);
    pt.add(new Point(x,y));
  }

  fnt = new RFont("Zapfino.ttf",100);
  //RCommand.setSegmentAngle(random(0,HALF_PI)); 
  //RCommand.setSegmentator(RCommand.ADAPTATIVE);
  RCommand.setSegmentLength(3); 
  RCommand.setSegmentator(RCommand.UNIFORMLENGTH);

  rg = fnt.toGroup(str);
  rp = rg.getPointsInPaths();

  X = new FloatList();
  Y = new FloatList();

  SUM = new FloatList();
}

void draw(){
  background(255);
  noFill();
  strokeCap(ROUND);
  strokeWeight(2);
  //draw points
  beginShape();
  for(int i=0; i<=num; i++){
    vertex(pt.get(i).x,pt.get(i).y);
  }
  endShape();

  for(int i=0; i<=num; i++){
    ellipse(pt.get(i).x,pt.get(i).y,10,10);
  }


  //draw curve
  nfac = 1;
  for(int i=0; i<num; i++){
    nfac *= (i+1);
  }

  strokeWeight(2);
  beginShape();
  int Bzindex = 0;
  for(float t=0; t<=1; t+=0.01){
    float x = 0;
    float y = 0;
    Bzindex++;
    for(int i=0; i<=num; i++){

      float coef = 1;
      float kfac = 1;
      float k_nfac = 1;
      for(int k=i; k>0; k--){
        kfac *= k;
      }
      for(int k=(num-i); k>0; k--){
        k_nfac *= k;
      }

      coef = nfac/(kfac*k_nfac);

      x += coef*(pow((1-t),num-i)*pow(t,i)*pt.get(i).x);
      y += coef*(pow((1-t),num-i)*pow(t,i)*pt.get(i).y);

    }
    vertex(x,y);
    X.set(Bzindex,x);
    Y.set(Bzindex,y);
  }
  endShape();




  //get arclength by pulling points from a floatlist
  int numberOfDivisions = X.size()-2;
  int maxPoint = numberOfDivisions+1;

  float sum = 0;

  float prevPointX = X.get(0);
  float prevPointY = Y.get(0);

  for(int i=1; i<=maxPoint; i++){
    float pointX = X.get(i);
    float pointY = Y.get(i);
    sum += dist(pointX,pointY,prevPointX,prevPointY);
    SUM.set(i-1,sum);
    prevPointX = pointX;
    prevPointY = pointY;
  }
  //

  //*******************************************************
  noFill();
  //creates error only when using fill();
  fill(0,255,0);
  stroke(0);
  //noStroke();
  strokeWeight(4);
  float indent = SUM.get(0);
  beginShape();
  for(int i=0; i<rp.length; i++){

    if(i>0){
      beginContour();
    }

    for(int j=0; j<rp[i].length; j++){
      float t = 0;

      indent = rp[i][j].x+SUM.get(0);

      //

      float targetArcLength = indent;

        int index = IndexOfLargestValueSmallerThan(targetArcLength);

        if(SUM.get(index)==targetArcLength){
          t = index/(SUM.size()-1);
        }else{
          float lengthBefore = SUM.get(index);
          float lengthAfter = SUM.get(index+1);
          float segmentLength = lengthAfter - lengthBefore;
          float segmentFraction = (targetArcLength - lengthBefore)/segmentLength;
          t = (index+segmentFraction)/(SUM.size()-1);
        }

        float x = 0;
        float y = 0;
        float vx = 0;
        float vy = 0;

          for(int l=0; l<=num; l++){

            float coef = 1;
            float kfac = 1;
            float k_nfac = 1;
            for(int k=l; k>0; k--){
              kfac *= k;
            }
            for(int k=(num-l); k>0; k--){
              k_nfac *= k;
            }

            coef = nfac/(kfac*k_nfac);

            x += coef*(pow((1-t),num-l)*pow(t,l)*pt.get(l).x);
            y += coef*(pow((1-t),num-l)*pow(t,l)*pt.get(l).y);

            float v = (pow(1-t,num-l)*l*pow(t,l-1))+((num-l)*pow(1-t,num-l-1)*(-1)*pow(t,l));
            vx += coef*pt.get(l).x*(v);
            vy += coef*pt.get(l).y*(v);
         }

        PVector P = new PVector(x,rp[i][j].y+y);

        PVector ldir = new PVector(P.x-x,P.y-y);


        PVector dir = new PVector(vy,-vx); 
        //
        ldir.rotate(dir.heading()+PI/2);

        vertex(x+ldir.x,y+ldir.y);
      }
      if(i<rp.length&&i>0){
        endContour();
      }

    }
    endShape();
    //**************************************************************
}

int IndexOfLargestValueSmallerThan(float _targetArcLength){
  int index = 0;
  for(int i=0; i<SUM.size()-1; i++){
    if(SUM.get(i)<=_targetArcLength){
      index = i;
    }
  }
  return index;
}

void mouseDragged(){
  int which = -1;
  if((mouseX<width)&&(mouseX>0)&&(mouseY<height)&&(mouseY>0)){
  for(int i=0; i<=num; i++){
    if(dist(mouseX,mouseY,pt.get(i).x,pt.get(i).y)<50){
      which = i;
      pt.get(which).update(mouseX,mouseY);
    }
  }
  }
}

class Point{
  float x,y;

  Point(float _x, float _y){
    x = _x;
    y = _y;
  }

  void update(float _newx, float _newy){
    x = _newx;
    y = _newy;
  }

}

有时错误发生在加载草图时。大多数情况下,它加载正常,但当你稍微拖动点时,它会抛出一个错误。错误代码有时是指曲线的控制点被鼠标位置更新的点,但是因为错误经常发生在草图加载的时候,我认为这不是一个与更新的位置有关的问题。

错误代码如下;

a.lang.AssertionError
    at processing.opengl.PSurfaceJOGL$2.run(PSurfaceJOGL.java:412)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.AssertionError
    at jogamp.opengl.glu.tessellator.Sweep.AddRightEdges(Sweep.java:358)
    at jogamp.opengl.glu.tessellator.Sweep.SweepEvent(Sweep.java:1105)
    at jogamp.opengl.glu.tessellator.Sweep.__gl_computeInterior(Sweep.java:1339)
    at jogamp.opengl.glu.tessellator.GLUtessellatorImpl.gluTessEndPolygon(GLUtessellatorImpl.java:526)
    at com.jogamp.opengl.glu.GLU.gluTessEndPolygon(GLU.java:896)
    at processing.opengl.PJOGL$Tessellator.endPolygon(PJOGL.java:641)
    at processing.opengl.PGraphicsOpenGL$Tessellator.tessellatePolygon(PGraphicsOpenGL.java:12621)
    at processing.opengl.PGraphicsOpenGL.tessellate(PGraphicsOpenGL.java:2255)
    at processing.opengl.PGraphicsOpenGL.endShape(PGraphicsOpenGL.java:1965)
    at processing.core.PGraphics.endShape(PGraphics.java:1707)
    at processing.core.PApplet.endShape(PApplet.java:11641)
    at bezier_polynomial_recursive_text_03.draw(bezier_polynomial_recursive_text_03.java:218)
    at processing.core.PApplet.handleDraw(PApplet.java:2475)
    at processing.opengl.PSurfaceJOGL$DrawListener.display(PSurfaceJOGL.java:866)
    at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:692)
    at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:674)
RuntimeException: java.lang.AssertionError
    at jogamp.opengl.GLAutoDrawableBase$2.run(GLAutoDrawableBase.java:443)
    at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1293)
    at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:1147)
    at com.jogamp.newt.opengl.GLWindow.display(GLWindow.java:759)
    at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:81)
    at com.jogamp.opengl.util.AnimatorBase.display(AnimatorBase.java:452)
    at com.jogamp.opengl.util.FPSAnimator$MainTask.run(FPSAnimator.java:178)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)

共有1个答案

阎渝
2023-03-14

我不认为这会直接回答我的问题,但它确实阻止了在使用fill()和P2D渲染器时出现的错误。正如laancelot在上面指出的,主要问题似乎确实与堆栈溢出有关。所以我用下面写的两种方法来处理这个问题;结论:直接原因是数学公式表达不好。

1)在类中切换RPoints。-我不认为这是发生错误的直接原因,因为在只完成重写代码这一部分的阶段,错误仍然存在。但也许这是问题的一部分。我不确定。

2)重写代码表达公式的部分,求取特定点的bezier曲线。--以前,该公式是利用n次bezier曲线的显式定义得出的。因此,公式必须为RPoint点中的每个点计算(更像made)。正如维基百科关于贝塞尔曲线的页面上提到的,这种计算方式是不推荐的。-在修订后的代码中,用于扭曲文本的公式以多项式形式表示。因此,它能够在RPoint点迭代之前预先计算多项式的系数。这似乎解决了问题。

//n number of points
int num = 4;
//arraylist to store the picked values
ArrayList<cntrlPoint> pt;

//import the geomerative library
import geomerative.*;



//string
String str = "(O_o)/ Oooh";

FloatList X;
FloatList Y;
FloatList SUM;

RClass rc;

void setup() {
  size(1000, 1000, P2D);
  pt = new ArrayList<cntrlPoint>();
  //pick a number of points with random positions
  for (int i=0; i<=num; i++) {
    float x = random(0, width);
    float y = random(0, height);
    pt.add(new cntrlPoint(x, y));
  }

  RG.init(this);
  rc = new RClass();   

  X = new FloatList();
  Y = new FloatList();  
  SUM = new FloatList();
}

void draw() {
  background(255);
  noFill();
  strokeWeight(2);
  drwCntrlPoints();
  drwCurve();
  gtArcLength();
  fill(0,255,0);
  rc.crtPoly(pt);
  rc.drwText();
}

void drwCntrlPoints() {
  //draw points
  beginShape();
  for (int i=0; i<=num; i++) {
    vertex(pt.get(i).x, pt.get(i).y);
  }
  endShape();

  for (int i=0; i<=num; i++) {
    ellipse(pt.get(i).x, pt.get(i).y, 10, 10);
  }
}

void drwCurve() {
  //draw curve
  float curveDetail = 0.01;
  float nfac = 1;
  for (int i=0; i<num; i++) {
    nfac *= (i+1);
  }
  int arcIndex = 0;
  strokeWeight(2);
  beginShape();
  for (float t=0; t<=1; t+=curveDetail) {
    float x = 0;
    float y = 0;
    arcIndex++;
    for (int i=0; i<=num; i++) {

      float coef = 1;
      float kfac = 1;
      float k_nfac = 1;
      for (int k=i; k>0; k--) {
        kfac *= k;
      }
      for (int k=(num-i); k>0; k--) {
        k_nfac *= k;
      }

      coef = nfac/(kfac*k_nfac);

      x += coef*(pow((1-t), num-i)*pow(t, i)*pt.get(i).x);
      y += coef*(pow((1-t), num-i)*pow(t, i)*pt.get(i).y);
    }
    vertex(x, y);
    X.set(arcIndex, x);
    Y.set(arcIndex, y);
  }
  endShape();
}

void gtArcLength() {
  //get arclength by pulling points from a floatlist
  int numberOfDivisions = X.size()-2;
  int maxPoint = numberOfDivisions+1;

  float sum = 0;

  float prevPointX = X.get(0);
  float prevPointY = Y.get(0);

  for (int i=1; i<=maxPoint; i++) {
    float pointX = X.get(i);
    float pointY = Y.get(i);
    sum += dist(pointX, pointY, prevPointX, prevPointY);
    SUM.set(i-1, sum);
    prevPointX = pointX;
    prevPointY = pointY;
  }
}

//*******factorial
int fact(int fa){
  if(fa==1){
    return 1;
  }
  if(fa==0){
    return 1;
  }
  else{
    return fa*fact(fa-1);
  }
}
//********************

int IndexOfLargestValueSmallerThan(float _targetArcLength) {
  int index = 0;
  for (int i=0; i<SUM.size()-1; i++) {
    if (SUM.get(i)<=_targetArcLength) {
      index = i;
    }
  }
  return index;
}

void mouseDragged() {
  int which = -1;
  if ((mouseX<width)&&(mouseX>0)&&(mouseY<height)&&(mouseY>0)) {
    for (int i=0; i<=num; i++) {
      if (dist(mouseX, mouseY, pt.get(i).x, pt.get(i).y)<80) {
        which = i;
      }
    }
    if (which>-1) {
      pt.get(which).update(mouseX, mouseY);
    }
  }
}

class RClass {
  //get ttf file
  //create rfont
  RFont fnt;
  //turn rfont to rgroup to get points
  RGroup rg;
  //going to get point in path, so that the characters in the string can be seperated
  RPoint [][]rp;

  //floatlist to store coefficients
  FloatList Cx;
  FloatList Cy;

  RClass() {
    fnt = new RFont("Zapfino.ttf", 100);
    rg = fnt.toGroup(str);
    rp = rg.getPointsInPaths();

    //RCommand.setSegmentAngle(random(0,HALF_PI)); 
    //RCommand.setSegmentator(RCommand.ADAPTATIVE);
    RCommand.setSegmentLength(3); 
    RCommand.setSegmentator(RCommand.UNIFORMLENGTH);

    Cx = new FloatList();
    Cy = new FloatList();
  }

  //**********************************here
  void crtPoly(ArrayList<cntrlPoint> _pt){
    float ptsize = _pt.size();
    for(int j=0; j<ptsize; j++){
      float coefx = 0;
      float coefy = 0;
      float pi = 1;
      float sigx = 0;
      float sigy = 0;
      for(int m=0; m<=j-1; m++){
        pi *= (ptsize-1-m);
      }
      for(int i=0; i<=j; i++){
        sigx += (pow(-1,i+j)*pt.get(i).x)/(fact(i)*fact(j-i));
        sigy += (pow(-1,i+j)*pt.get(i).y)/(fact(i)*fact(j-i));
      }
      coefx = pi*sigx;
      coefy = pi*sigy;
      Cx.set(j,coefx);
      Cy.set(j,coefy);
    }
  }
  //**************************************

  void drwText() {
    float indent = SUM.get(0);  

    beginShape();       
    for (int i=0; i<rp.length; i++) {
      if(i>0){
        beginContour();
      }
      for (int j=0; j<rp[i].length; j++) {

        float t = 0;

        indent = rp[i][j].x+SUM.get(0);

        float targetArcLength = indent;

        int index = IndexOfLargestValueSmallerThan(targetArcLength);

        if (SUM.get(index)==targetArcLength) {
          t = index/(SUM.size()-1);
        } else {
          float lengthBefore = SUM.get(index);
          float lengthAfter = SUM.get(index+1);
          float segmentLength = lengthAfter - lengthBefore;
          float segmentFraction = (targetArcLength - lengthBefore)/segmentLength;
          t = (index+segmentFraction)/(SUM.size()-1);
        }

        //***************************here
        float x = 0;
        float y = 0;
        float vx = 0;
        float vy = 0;

        for(int l=0; l<=num; l++){
          x += Cx.get(l)*pow(t,l);
          y += Cy.get(l)*pow(t,l);
        }

        for(int l=1; l<=num; l++){
          vx += l*Cx.get(l)*pow(t,l-1);
          vy += l*Cy.get(l)*pow(t,l-1);
        }
        //**************************************

        PVector P = new PVector(x, rp[i][j].y+y);

        PVector ldir = new PVector(P.x-x, P.y-y);


        PVector dir = new PVector(vy, -vx); 
        //
        ldir.rotate(dir.heading()+PI/2);

        vertex(x+ldir.x, y+ldir.y);
      }
      if(i>0&&i<rp.length){
        endContour();
      }
    }
    endShape();
  }
}

class cntrlPoint{
  float x,y;

  cntrlPoint(float _x, float _y){
    x = _x;
    y = _y;
  }

  void update(float _newx, float _newy){    
    x = _newx;
    y = _newy;   
  }

}
 类似资料:
  • 这是如何使用公共类frome的一个后续步骤。其他处理选项卡中的java文件?;使用来自的Usage类中的示例。java文件-有完整的文档吗?-处理2。x和3。x论坛,我有这个: /tmp/Sketch/Foo.java 这个例子运行得很好,但是如果我取消注释import peasy。组织 行,则编译失败: 当然,我确实在下安装了PeasyCam,如果我导入peasy.*它工作得很好 来自草图。 我

  • 我使用的是陀螺仪/加速计传感器(https://www.sparkfun.com/products/13284)用Arduino来记录旋转。我总共使用了八个这样的传感器。我还使用多路复用器来帮助多个传感器(https://learn.adafruit.com/adafruit-tca9548a-1-to-8-i2c-multiplexer-breakout/overview).在我的Arduino

  • 我试着把加工草图与多个。pde文件(标签或类)在我的网站上,但它似乎不起作用。你应该能够点击,使球出现在屏幕上反弹。然而,似乎并没有第二次出现。pde文件,因此,草图不能完全工作。 http://willhay.io/processing/一个普通的单文件草图效果很好,我认为这与这个草图有一个球有关。应该出现的球的pde类。

  • 我目前正在研究一种数据可视化技术,它从电影中获取信息,实时播放并同时创建。为此,我想有两个独立的草图窗口。一个窗口应显示实时播放的电影,另一个窗口应显示正在进行的可视化。 我似乎不知道如何轻松地添加另一个草图窗口,并尝试了一些在处理3中不再工作的示例。 然后我偶然发现了这个例子:https://gist.github.com/atduskgreg/666e46c8408e2a33b09a 尽管我可

  • 下面的代码旨在将蓝色背景和三个移动形状(圆形、正方形、三角形)绘制到屏幕上。然而,代码是用面向对象编程编写的,我的草图在处理IDE的Java模式下工作得很好,但在OpenProcessing中却不能工作,这是不对的。代码编译得很好。我想我在OOP中做了一些不正确的事情。谁能帮帮我吗?谢了!

  • 我正在调试一个HiveProcessor,它遵循官方的PutHiveStreaming处理器,但它写入的是Hive2.x,而不是3.x。该流在Nifi cluster 1.7.1中运行。尽管发生此异常,但数据仍被写入配置单元。 例外情况是: 有人做过类似的事吗?或者有没有更简单的方法来调试自定义处理器?