猿问

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

该研究项目是关于使用几何库从 .ttf 类型的字体文件中获取形状时沿贝塞尔曲线(递归多项式形式)处理文本。(它需要数据目录中的 ttf 文件才能运行。)目前,草图填充时似乎会抛出错误(任何颜色);用于代码绘制字符形状且贝塞尔曲线长度小于一定长度的部分。如果填充(); 未使用,草图似乎功能正常,没有任何错误。目标是使用 fill(); 函数来无误地填充字符。

我试过了; 1) 去掉 beginContour();和结束轮廓();因为我认为它写得不正确。(我认为这是错误的,因为只有当形状是字母的内侧时才应该绘制轮廓,但目前,它不是第一个或最后一个形状时绘制轮廓)但是即使未使用轮廓函数(使用了填充();),草图也会抛出错误。2)认为与曲线的长度有关,所以尝试在绘制字母的部分添加if语句。到目前为止,我已经尝试使用从 void setup(){} 中的初始字体大小和字符串生成的 RGroup 的宽度,以及贝塞尔曲线的长度。if 语句中的条件示例如下;-RGroup形状时绘制字母' s width is smaller than the length of the curve - 当“缩进”(计算曲线上位置的变量)值小于曲线长度时绘制字母。(本例使草图仅在字母位于曲线内时才绘制字母,但仍然出现错误) - 当“缩进”(计算曲线位置的变量)值小于宽度时绘制字母的RGroup。

我看不出问题到底出在哪里,所以我在草图中分享了整个代码,但我用“//*******”标记了我认为错误发生的地方。

本研究基于以下链接可以从以下链接查看几何库文档。

有时在加载草图时会发生错误。大多数情况下,它加载正常,但当您稍微拖动该点时会抛出错误。错误代码有时是指曲线的控制点通过鼠标位置更新的点,但由于有时在加载草图时也会发生错误,所以我认为这不是与更新位置有关的问题。



慕娘9325324
浏览 153回答 1
1回答

慕桂英546537

我不认为这会直接回答我的问题,但它确实阻止了在同时使用 fill() 和 P2D 渲染器时发生的错误。正如上面 laancelot 所指出的,主要问题确实似乎与堆栈溢出有关。所以我用下面两种方式解决了这个问题;结论:直接原因是数学公式表达不佳。1) 在一个类中切换 RPoints。-我不认为这是错误发生的直接原因,因为在只重写代码的这一部分完成的阶段,错误仍然存在。但也许这是问题的一部分。我不知道。2)重写代码表达公式的部分,以评估特定点的贝塞尔曲线。- 以前,该公式是通过使用阶数为 n 的贝塞尔曲线的显式定义得出的。因此,必须为 RPoint 点中的每个点计算(更像是制作)公式。正如关于贝塞尔曲线的维基百科页面上提到的,不推荐这种计算方式。-在修改后的代码中,用于扭曲文本的公式以多项式形式表示。因此,它能够在迭代 RPoint 点之前预先计算多项式的系数。这似乎已经解决了问题。我仍然不确定到底是什么导致了这个问题,为什么它已经解决了,我应该展示代码的哪一部分来向其他人解释这个问题,所以我将分享已经重写的整个代码。您需要处理、几何库和数据文件夹中的 ttf 类型字体文件来测试代码。牵扯到修改版公式的地方我都标出来了。(还是真的很乱。。。)//n number of pointsint num = 4;//arraylist to store the picked valuesArrayList<cntrlPoint> pt;//import the geomerative libraryimport geomerative.*;//stringString str = "(O_o)/ Oooh";FloatList X;FloatList Y;FloatList SUM;RClass rc;void setup() {&nbsp; size(1000, 1000, P2D);&nbsp; pt = new ArrayList<cntrlPoint>();&nbsp; //pick a number of points with random positions&nbsp; for (int i=0; i<=num; i++) {&nbsp; &nbsp; float x = random(0, width);&nbsp; &nbsp; float y = random(0, height);&nbsp; &nbsp; pt.add(new cntrlPoint(x, y));&nbsp; }&nbsp; RG.init(this);&nbsp; rc = new RClass();&nbsp; &nbsp;&nbsp; X = new FloatList();&nbsp; Y = new FloatList();&nbsp;&nbsp;&nbsp; SUM = new FloatList();}void draw() {&nbsp; background(255);&nbsp; noFill();&nbsp; strokeWeight(2);&nbsp; drwCntrlPoints();&nbsp; drwCurve();&nbsp; gtArcLength();&nbsp; fill(0,255,0);&nbsp; rc.crtPoly(pt);&nbsp; rc.drwText();}void drwCntrlPoints() {&nbsp; //draw points&nbsp; beginShape();&nbsp; for (int i=0; i<=num; i++) {&nbsp; &nbsp; vertex(pt.get(i).x, pt.get(i).y);&nbsp; }&nbsp; endShape();&nbsp; for (int i=0; i<=num; i++) {&nbsp; &nbsp; ellipse(pt.get(i).x, pt.get(i).y, 10, 10);&nbsp; }}void drwCurve() {&nbsp; //draw curve&nbsp; float curveDetail = 0.01;&nbsp; float nfac = 1;&nbsp; for (int i=0; i<num; i++) {&nbsp; &nbsp; nfac *= (i+1);&nbsp; }&nbsp; int arcIndex = 0;&nbsp; strokeWeight(2);&nbsp; beginShape();&nbsp; for (float t=0; t<=1; t+=curveDetail) {&nbsp; &nbsp; float x = 0;&nbsp; &nbsp; float y = 0;&nbsp; &nbsp; arcIndex++;&nbsp; &nbsp; for (int i=0; i<=num; i++) {&nbsp; &nbsp; &nbsp; float coef = 1;&nbsp; &nbsp; &nbsp; float kfac = 1;&nbsp; &nbsp; &nbsp; float k_nfac = 1;&nbsp; &nbsp; &nbsp; for (int k=i; k>0; k--) {&nbsp; &nbsp; &nbsp; &nbsp; kfac *= k;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; for (int k=(num-i); k>0; k--) {&nbsp; &nbsp; &nbsp; &nbsp; k_nfac *= k;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; coef = nfac/(kfac*k_nfac);&nbsp; &nbsp; &nbsp; x += coef*(pow((1-t), num-i)*pow(t, i)*pt.get(i).x);&nbsp; &nbsp; &nbsp; y += coef*(pow((1-t), num-i)*pow(t, i)*pt.get(i).y);&nbsp; &nbsp; }&nbsp; &nbsp; vertex(x, y);&nbsp; &nbsp; X.set(arcIndex, x);&nbsp; &nbsp; Y.set(arcIndex, y);&nbsp; }&nbsp; endShape();}void gtArcLength() {&nbsp; //get arclength by pulling points from a floatlist&nbsp; int numberOfDivisions = X.size()-2;&nbsp; int maxPoint = numberOfDivisions+1;&nbsp; float sum = 0;&nbsp; float prevPointX = X.get(0);&nbsp; float prevPointY = Y.get(0);&nbsp; for (int i=1; i<=maxPoint; i++) {&nbsp; &nbsp; float pointX = X.get(i);&nbsp; &nbsp; float pointY = Y.get(i);&nbsp; &nbsp; sum += dist(pointX, pointY, prevPointX, prevPointY);&nbsp; &nbsp; SUM.set(i-1, sum);&nbsp; &nbsp; prevPointX = pointX;&nbsp; &nbsp; prevPointY = pointY;&nbsp; }}//*******factorialint fact(int fa){&nbsp; if(fa==1){&nbsp; &nbsp; return 1;&nbsp; }&nbsp; if(fa==0){&nbsp; &nbsp; return 1;&nbsp; }&nbsp; else{&nbsp; &nbsp; return fa*fact(fa-1);&nbsp; }}//********************int IndexOfLargestValueSmallerThan(float _targetArcLength) {&nbsp; int index = 0;&nbsp; for (int i=0; i<SUM.size()-1; i++) {&nbsp; &nbsp; if (SUM.get(i)<=_targetArcLength) {&nbsp; &nbsp; &nbsp; index = i;&nbsp; &nbsp; }&nbsp; }&nbsp; return index;}void mouseDragged() {&nbsp; int which = -1;&nbsp; if ((mouseX<width)&&(mouseX>0)&&(mouseY<height)&&(mouseY>0)) {&nbsp; &nbsp; for (int i=0; i<=num; i++) {&nbsp; &nbsp; &nbsp; if (dist(mouseX, mouseY, pt.get(i).x, pt.get(i).y)<80) {&nbsp; &nbsp; &nbsp; &nbsp; which = i;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (which>-1) {&nbsp; &nbsp; &nbsp; pt.get(which).update(mouseX, mouseY);&nbsp; &nbsp; }&nbsp; }}class RClass {&nbsp; //get ttf file&nbsp; //create rfont&nbsp; RFont fnt;&nbsp; //turn rfont to rgroup to get points&nbsp; RGroup rg;&nbsp; //going to get point in path, so that the characters in the string can be seperated&nbsp; RPoint [][]rp;&nbsp; //floatlist to store coefficients&nbsp; FloatList Cx;&nbsp; FloatList Cy;&nbsp; RClass() {&nbsp; &nbsp; fnt = new RFont("Zapfino.ttf", 100);&nbsp; &nbsp; rg = fnt.toGroup(str);&nbsp; &nbsp; rp = rg.getPointsInPaths();&nbsp; &nbsp; //RCommand.setSegmentAngle(random(0,HALF_PI));&nbsp;&nbsp; &nbsp; //RCommand.setSegmentator(RCommand.ADAPTATIVE);&nbsp; &nbsp; RCommand.setSegmentLength(3);&nbsp;&nbsp; &nbsp; RCommand.setSegmentator(RCommand.UNIFORMLENGTH);&nbsp; &nbsp; Cx = new FloatList();&nbsp; &nbsp; Cy = new FloatList();&nbsp; }&nbsp; //**********************************here&nbsp; void crtPoly(ArrayList<cntrlPoint> _pt){&nbsp; &nbsp; float ptsize = _pt.size();&nbsp; &nbsp; for(int j=0; j<ptsize; j++){&nbsp; &nbsp; &nbsp; float coefx = 0;&nbsp; &nbsp; &nbsp; float coefy = 0;&nbsp; &nbsp; &nbsp; float pi = 1;&nbsp; &nbsp; &nbsp; float sigx = 0;&nbsp; &nbsp; &nbsp; float sigy = 0;&nbsp; &nbsp; &nbsp; for(int m=0; m<=j-1; m++){&nbsp; &nbsp; &nbsp; &nbsp; pi *= (ptsize-1-m);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; for(int i=0; i<=j; i++){&nbsp; &nbsp; &nbsp; &nbsp; sigx += (pow(-1,i+j)*pt.get(i).x)/(fact(i)*fact(j-i));&nbsp; &nbsp; &nbsp; &nbsp; sigy += (pow(-1,i+j)*pt.get(i).y)/(fact(i)*fact(j-i));&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; coefx = pi*sigx;&nbsp; &nbsp; &nbsp; coefy = pi*sigy;&nbsp; &nbsp; &nbsp; Cx.set(j,coefx);&nbsp; &nbsp; &nbsp; Cy.set(j,coefy);&nbsp; &nbsp; }&nbsp; }&nbsp; //**************************************&nbsp; void drwText() {&nbsp; &nbsp; float indent = SUM.get(0);&nbsp;&nbsp;&nbsp; &nbsp; beginShape();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; for (int i=0; i<rp.length; i++) {&nbsp; &nbsp; &nbsp; if(i>0){&nbsp; &nbsp; &nbsp; &nbsp; beginContour();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; for (int j=0; j<rp[i].length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; float t = 0;&nbsp; &nbsp; &nbsp; &nbsp; indent = rp[i][j].x+SUM.get(0);&nbsp; &nbsp; &nbsp; &nbsp; float targetArcLength = indent;&nbsp; &nbsp; &nbsp; &nbsp; int index = IndexOfLargestValueSmallerThan(targetArcLength);&nbsp; &nbsp; &nbsp; &nbsp; if (SUM.get(index)==targetArcLength) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t = index/(SUM.size()-1);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float lengthBefore = SUM.get(index);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float lengthAfter = SUM.get(index+1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float segmentLength = lengthAfter - lengthBefore;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float segmentFraction = (targetArcLength - lengthBefore)/segmentLength;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t = (index+segmentFraction)/(SUM.size()-1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //***************************here&nbsp; &nbsp; &nbsp; &nbsp; float x = 0;&nbsp; &nbsp; &nbsp; &nbsp; float y = 0;&nbsp; &nbsp; &nbsp; &nbsp; float vx = 0;&nbsp; &nbsp; &nbsp; &nbsp; float vy = 0;&nbsp; &nbsp; &nbsp; &nbsp; for(int l=0; l<=num; l++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x += Cx.get(l)*pow(t,l);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y += Cy.get(l)*pow(t,l);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for(int l=1; l<=num; l++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vx += l*Cx.get(l)*pow(t,l-1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vy += l*Cy.get(l)*pow(t,l-1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //**************************************&nbsp; &nbsp; &nbsp; &nbsp; PVector P = new PVector(x, rp[i][j].y+y);&nbsp; &nbsp; &nbsp; &nbsp; PVector ldir = new PVector(P.x-x, P.y-y);&nbsp; &nbsp; &nbsp; &nbsp; PVector dir = new PVector(vy, -vx);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; ldir.rotate(dir.heading()+PI/2);&nbsp; &nbsp; &nbsp; &nbsp; vertex(x+ldir.x, y+ldir.y);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if(i>0&&i<rp.length){&nbsp; &nbsp; &nbsp; &nbsp; endContour();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; endShape();&nbsp; }}class cntrlPoint{&nbsp; float x,y;&nbsp; cntrlPoint(float _x, float _y){&nbsp; &nbsp; x = _x;&nbsp; &nbsp; y = _y;&nbsp; }&nbsp; void update(float _newx, float _newy){&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; x = _newx;&nbsp; &nbsp; y = _newy;&nbsp; &nbsp;&nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答