亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

僅在使用 fill() 時處理草圖拋出 java.lang.AssertionError

僅在使用 fill() 時處理草圖拋出 java.lang.AssertionError

慕娘9325324 2023-03-09 17:09:58
該研究項目是關于使用幾何庫從 .ttf 類型的字體文件中獲取形狀時沿貝塞爾曲線(遞歸多項式形式)處理文本。(它需要數據目錄中的 ttf 文件才能運行。)目前,草圖填充時似乎會拋出錯誤(任何顏色);用于代碼繪制字符形狀且貝塞爾曲線長度小于一定長度的部分。如果填充(); 未使用,草圖似乎功能正常,沒有任何錯誤。目標是使用 fill(); 函數來無誤地填充字符。我試過了; 1) 去掉 beginContour();和結束輪廓();因為我認為它寫得不正確。(我認為這是錯誤的,因為只有當形狀是字母的內側時才應該繪制輪廓,但目前,它不是第一個或最后一個形狀時繪制輪廓)但是即使未使用輪廓函數(使用了填充();),草圖也會拋出錯誤。2)認為與曲線的長度有關,所以嘗試在繪制字母的部分添加if語句。到目前為止,我已經嘗試使用從 void setup(){} 中的初始字體大小和字符串生成的 RGroup 的寬度,以及貝塞爾曲線的長度。if 語句中的條件示例如下;-RGroup形狀時繪制字母' s width is smaller than the length of the curve - 當“縮進”(計算曲線上位置的變量)值小于曲線長度時繪制字母。(本例使草圖僅在字母位于曲線內時才繪制字母,但仍然出現錯誤) - 當“縮進”(計算曲線位置的變量)值小于寬度時繪制字母的RGroup。我看不出問題到底出在哪里,所以我在草圖中分享了整個代碼,但我用“//*******”標記了我認為錯誤發生的地方。本研究基于以下鏈接??梢詮囊韵骆溄硬榭磶缀螏煳臋n。有時在加載草圖時會發生錯誤。大多數情況下,它加載正常,但當您稍微拖動該點時會拋出錯誤。錯誤代碼有時是指曲線的控制點通過鼠標位置更新的點,但由于有時在加載草圖時也會發生錯誤,所以我認為這不是與更新位置有關的問題。
查看完整描述

1 回答

?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

我不認為這會直接回答我的問題,但它確實阻止了在同時使用 fill() 和 P2D 渲染器時發生的錯誤。正如上面 laancelot 所指出的,主要問題確實似乎與堆棧溢出有關。所以我用下面兩種方式解決了這個問題;結論:直接原因是數學公式表達不佳。


1) 在一個類中切換 RPoints。-我不認為這是錯誤發生的直接原因,因為在只重寫代碼的這一部分完成的階段,錯誤仍然存在。但也許這是問題的一部分。我不知道。


2)重寫代碼表達公式的部分,以評估特定點的貝塞爾曲線。- 以前,該公式是通過使用階數為 n 的貝塞爾曲線的顯式定義得出的。因此,必須為 RPoint 點中的每個點計算(更像是制作)公式。正如關于貝塞爾曲線的維基百科頁面上提到的,不推薦這種計算方式。-在修改后的代碼中,用于扭曲文本的公式以多項式形式表示。因此,它能夠在迭代 RPoint 點之前預先計算多項式的系數。這似乎已經解決了問題。


我仍然不確定到底是什么導致了這個問題,為什么它已經解決了,我應該展示代碼的哪一部分來向其他人解釋這個問題,所以我將分享已經重寫的整個代碼。您需要處理、幾何庫和數據文件夾中的 ttf 類型字體文件來測試代碼。牽扯到修改版公式的地方我都標出來了。(還是真的很亂。。。)


//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;   

  }


}



查看完整回答
反對 回復 2023-03-09
  • 1 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號