php三次方贝赛尔曲线,求三次贝塞尔曲线的控制点------点集用分段三次贝塞尔曲线插值生成光滑曲线...

void InterpolateBeizers(std::vector& points,

bool bClosedCurve,

double smoothValue,

std::vector<:vector>>& beizers)

{

int size=points.size();

if (size<3)

return;

//if is close curve then add the first point at the end

if (bClosedCurve)

points.push_back(points.at(0));

for (int i=0;i

{

// Assume we need to calculate the control

// points between (x1,y1) and (x2,y2).

// Then x0,y0 - the previous vertex,

// x3,y3 - the next one.

double x1=points[i].x();

double y1=points[i].y();

double x2=points[i+1].x();

double y2=points[i+1].y();

double x0=0.0;

double y0=0.0;

if (i==0) //if is first point

{

if (bClosedCurve)

{

QPointF& previousPoint=points[size-2];//last Point, but one (due inserted the first at the end)

x0=previousPoint.x();

y0=previousPoint.y();

}

else //Get some previouse point

{

QPointF& previousPoint=points[i]; //if is the first point the previous one will be it self

x0=previousPoint.x();

y0=previousPoint.y();

}

}

else

{

x0=points[i-1].x(); //Previous Point

y0=points[i-1].y();

}

double x3=0.0;

double y3=0.0;

if (i==size-2) //if is the last point

{

if (bClosedCurve)

{

QPointF& nextPoint=points[1]; //second Point(due inserted the first at the end)

x3=nextPoint.x();

y3=nextPoint.y();

}

else //Get some next point

{

QPointF& nextPoint=points[i+1]; //if is the last point the next point will be the last one

x3=nextPoint.x();

y3=nextPoint.y();

}

}

else

{

x3=points[i+2].x(); //Next Point

y3=points[i+2].y();

}

double xc1=(x0+x1)*0.5;

double yc1=(y0+y1)*0.5;

double xc2=(x1+x2)*0.5;

double yc2=(y1+y2)*0.5;

double xc3=(x2+x3)*0.5;

double yc3=(y2+y3)*0.5;

double len1=sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0));

double len2=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));

double len3=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));

double k1=len1/(len1+len2);

double k2=len2/(len2+len3);

double xm1=xc1+(xc2-xc1)*k1;

double ym1=yc1+(yc2-yc1)*k1;

double xm2=xc2+(xc3-xc2)*k2;

double ym2=yc2+(yc3-yc2)*k2;

// Resulting control points. Here smooth_value is mentioned

// above coefficient K whose value should be in range [0...1].

double ctrl1_x=xm1+(xc2-xm1)*smoothValue+x1-xm1;

double ctrl1_y=ym1+(yc2-ym1)*smoothValue+y1-ym1;

double ctrl2_x=xm2+(xc2-xm2)*smoothValue+x2-xm2;

double ctrl2_y=ym2+(yc2-ym2)*smoothValue+y2-ym2;

std::vector beizer;

QPointF startPoint(x1,y1);

QPointF endPoint(x2,y2);

QPointF firstControlPoint=(i==0&&!bClosedCurve?QPointF(x1,y1):QPointF(ctrl1_x,ctrl1_y));

QPointF secondControlPoint=(i==points.size()-2&&!bClosedCurve?QPointF(x2,y2):QPointF(ctrl2_x,ctrl2_y));

beizer.push_back(startPoint);

beizer.push_back(endPoint);

beizer.push_back(firstControlPoint);

beizer.push_back(secondControlPoint);

beizers.push_back(beizer);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现加入购物车飞入动画,可以使用贝塞尔曲线来控制动画路径。具体实现步骤如下: 1. 在购买按钮点击事件中,获取购买按钮的位置和购物车图标的位置,并计算出两者之间的距离。 2. 创建一个贝塞尔曲线控制点设置为购买按钮位置和购物车图标位置的中心点,终点设置为购物车图标位置。 3. 使用CSS3动画或JavaScript来实现动画效果,让购买按钮沿着贝塞尔曲线飞入购物车。 下面是一个简单的实现示例: ```html <template> <div> <button @click="addToCart">加入购物车</button> <div class="cart-icon"></div> <div class="ball"></div> </div> </template> <script> export default { methods: { addToCart() { const button = document.querySelector('button') const cartIcon = document.querySelector('.cart-icon') const ball = document.querySelector('.ball') const buttonRect = button.getBoundingClientRect() const cartIconRect = cartIcon.getBoundingClientRect() const deltaX = cartIconRect.left - buttonRect.left const deltaY = cartIconRect.top - buttonRect.top const controlPointX = (buttonRect.left + cartIconRect.left) / 2 const controlPointY = buttonRect.top - 100 ball.style.left = buttonRect.left + 'px' ball.style.top = buttonRect.top + 'px' const bezierPath = `M${buttonRect.left},${buttonRect.top} Q${controlPointX},${controlPointY} ${cartIconRect.left},${cartIconRect.top}` ball.animate( [ { transform: `translate(0,0)` }, { transform: `translate(${deltaX}px, ${deltaY}px)` } ], { duration: 500, easing: 'ease-out', fill: 'forwards' } ) ball.animate( [ { transform: `translate(${deltaX}px, ${deltaY}px)` }, { transform: `translate(0,0)` } ], { duration: 500, easing: 'ease-in', fill: 'forwards', delay: 500 } ) ball.animate( [ { opacity: 1 }, { opacity: 0 } ], { duration: 500, fill: 'forwards', delay: 1000 } ) const path = document.createElementNS('http://www.w3.org/2000/svg', 'path') path.setAttribute('d', bezierPath) path.setAttribute('fill', 'none') path.setAttribute('stroke', 'red') path.setAttribute('stroke-width', '2') document.querySelector('.container').appendChild(path) } } } </script> <style> .cart-icon { position: absolute; top: 20px; right: 20px; width: 50px; height: 50px; background: url('cart.png'); background-size: cover; } .ball { position: absolute; width: 20px; height: 20px; border-radius: 50%; background: red; } .container { position: relative; } </style> ``` 这里使用了CSS3动画和JavaScript动画结合的方式来实现购买按钮飞入购物车的效果。在addToCart方法中,首先获取购买按钮和购物车图标的位置,并计算出两者之间的距离。然后根据贝塞尔曲线控制点计算出贝塞尔曲线路径,并创建一个SVG path元素来显示路径。最后使用CSS3动画和JavaScript动画来实现购买按钮沿着贝塞尔曲线飞入购物车的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值