您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 其它文档 > 贝塞尔曲线-C++版
//·â±ÕµÄ±´Èû¶ûÇúÏßstructCvPoint{CvPoint(){x=0;y=0;}CvPoint(doubledx,doubledy){x=dx;y=dy;}doublex;doubley;};//Èý´Î±´Èû¶ûÇúÏßfloatbezier3funcX(floatuu,CvPoint*controlP){floatpart0=controlP[0].x*uu*uu*uu;floatpart1=3*controlP[1].x*uu*uu*(1-uu);floatpart2=3*controlP[2].x*uu*(1-uu)*(1-uu);floatpart3=controlP[3].x*(1-uu)*(1-uu)*(1-uu);returnpart0+part1+part2+part3;}floatbezier3funcY(floatuu,CvPoint*controlP){floatpart0=controlP[0].y*uu*uu*uu;floatpart1=3*controlP[1].y*uu*uu*(1-uu);floatpart2=3*controlP[2].y*uu*(1-uu)*(1-uu);floatpart3=controlP[3].y*(1-uu)*(1-uu)*(1-uu);returnpart0+part1+part2+part3;}//¿ØÖƵãÊÕËõϵÊý£¬¾µ÷ÊÔ0.6½ÏºÃ£¬CvPointÊÇopencvµÄ£¬¿É×ÔÐж¨Òå½á¹¹Ìå(x,y)voidcreateCurve(CvPoint*originPoint,intoriginCount,vectorCvPoint&curvePoint,floatscale=0.6){CvPoint*midpoints=newCvPoint[originCount];//Éú³ÉÖеãfor(inti=0;ioriginCount;i++){intnexti=(i+1)%originCount;midpoints[i].x=(originPoint[i].x+originPoint[nexti].x)/2.0;midpoints[i].y=(originPoint[i].y+originPoint[nexti].y)/2.0;}//ƽÒÆÖеãCvPoint*extrapoints=newCvPoint[2*originCount];for(inti=0;ioriginCount;i++){intnexti=(i+1)%originCount;intbacki=(i+originCount-1)%originCount;CvPointmidinmid;midinmid.x=(midpoints[i].x+midpoints[backi].x)/2.0;midinmid.y=(midpoints[i].y+midpoints[backi].y)/2.0;intoffsetx=originPoint[i].x-midinmid.x;intoffsety=originPoint[i].y-midinmid.y;intextraindex=2*i;extrapoints[extraindex].x=midpoints[backi].x+offsetx;extrapoints[extraindex].y=midpoints[backi].y+offsety;//³¯originPoint[i]·½ÏòÊÕËõintaddx=(extrapoints[extraindex].x-originPoint[i].x)*scale;intaddy=(extrapoints[extraindex].y-originPoint[i].y)*scale;extrapoints[extraindex].x=originPoint[i].x+addx;extrapoints[extraindex].y=originPoint[i].y+addy;intextranexti=(extraindex+1)%(2*originCount);extrapoints[extranexti].x=midpoints[i].x+offsetx;extrapoints[extranexti].y=midpoints[i].y+offsety;//³¯originPoint[i]·½ÏòÊÕËõaddx=(extrapoints[extranexti].x-originPoint[i].x)*scale;addy=(extrapoints[extranexti].y-originPoint[i].y)*scale;extrapoints[extranexti].x=originPoint[i].x+addx;extrapoints[extranexti].y=originPoint[i].y+addy;}CvPointcontrolPoint[4];//Éú³É4¿ØÖƵ㣬²úÉú±´Èû¶ûÇúÏßfor(inti=0;ioriginCount;i++){controlPoint[0]=originPoint[i];intextraindex=2*i;controlPoint[1]=extrapoints[extraindex+1];intextranexti=(extraindex+2)%(2*originCount);controlPoint[2]=extrapoints[extranexti];intnexti=(i+1)%originCount;controlPoint[3]=originPoint[nexti];floatu=1;while(u=0){intpx=bezier3funcX(u,controlPoint);intpy=bezier3funcY(u,controlPoint);//uµÄ²½³¤¾ö¶¨ÇúÏßµÄÊèÃÜu-=0.005;CvPointtempP=CvPoint(px,py);//´æÈëÇúÏßµãcurvePoint.push_back(tempP);}}delete[]midpoints;delete[]extrapoints;}
本文标题:贝塞尔曲线-C++版
链接地址:https://www.777doc.com/doc-2226776 .html