如何利用Javascript生成平滑曲线详解
程序员文章站
2022-03-09 11:01:24
目录附录:vector2d相关的代码前言平滑曲线生成是一个很实用的技术很多时候,我们都需要通过绘制一些折线,然后让计算机平滑的连接起来,先来看下最终效果(红色为我们输入的直线,蓝色为拟合过后的曲线)...
前言
平滑曲线生成是一个很实用的技术
很多时候,我们都需要通过绘制一些折线,然后让计算机平滑的连接起来,
先来看下最终效果(红色为我们输入的直线,蓝色为拟合过后的曲线) 首尾可以特殊处理让图形看起来更好:)
实现思路是利用贝塞尔曲线进行拟合
贝塞尔曲线简介
贝塞尔曲线(英语:bézier curve)是计算机图形学中相当重要的参数曲线。
二次贝塞尔曲线
二次方贝塞尔曲线的路径由给定点p0、p1、p2的函数b(t)追踪:
三次贝塞尔曲线
对于三次曲线,可由线性贝塞尔曲线描述的中介点q0、q1、q2,和由二次曲线描述的点r0、r1所建构
贝塞尔曲线计算函数
根据上面的公式我们可有得到计算函数
二阶
/** * * * @param {number} p0 * @param {number} p1 * @param {number} p2 * @param {number} t * @return {*} * @memberof path */ bezier2p(p0: number, p1: number, p2: number, t: number) { const p0 = p0 * math.pow(1 - t, 2); const p1 = p1 * 2 * t * (1 - t); const p2 = p2 * t * t; return p0 + p1 + p2; } /** * * * @param {point} p0 * @param {point} p1 * @param {point} p2 * @param {number} num * @param {number} tick * @return {*} {point} * @memberof path */ getbeziernowpoint2p( p0: point, p1: point, p2: point, num: number, tick: number, ): point { return { x: this.bezier2p(p0.x, p1.x, p2.x, num * tick), y: this.bezier2p(p0.y, p1.y, p2.y, num * tick), }; } /** * 生成二次方贝塞尔曲线顶点数据 * * @param {point} p0 * @param {point} p1 * @param {point} p2 * @param {number} [num=100] * @param {number} [tick=1] * @return {*} * @memberof path */ create2pbezier( p0: point, p1: point, p2: point, num: number = 100, tick: number = 1, ) { const t = tick / (num - 1); const points = []; for (let i = 0; i < num; i++) { const point = this.getbeziernowpoint2p(p0, p1, p2, i, t); points.push({x: point.x, y: point.y}); } return points; }
三阶
/** * 三次方塞尔曲线公式 * * @param {number} p0 * @param {number} p1 * @param {number} p2 * @param {number} p3 * @param {number} t * @return {*} * @memberof path */ bezier3p(p0: number, p1: number, p2: number, p3: number, t: number) { const p0 = p0 * math.pow(1 - t, 3); const p1 = 3 * p1 * t * math.pow(1 - t, 2); const p2 = 3 * p2 * math.pow(t, 2) * (1 - t); const p3 = p3 * math.pow(t, 3); return p0 + p1 + p2 + p3; } /** * 获取坐标 * * @param {point} p0 * @param {point} p1 * @param {point} p2 * @param {point} p3 * @param {number} num * @param {number} tick * @return {*} * @memberof path */ getbeziernowpoint3p( p0: point, p1: point, p2: point, p3: point, num: number, tick: number, ) { return { x: this.bezier3p(p0.x, p1.x, p2.x, p3.x, num * tick), y: this.bezier3p(p0.y, p1.y, p2.y, p3.y, num * tick), }; } /** * 生成三次方贝塞尔曲线顶点数据 * * @param {point} p0 起始点 { x : number, y : number} * @param {point} p1 控制点1 { x : number, y : number} * @param {point} p2 控制点2 { x : number, y : number} * @param {point} p3 终止点 { x : number, y : number} * @param {number} [num=100] * @param {number} [tick=1] * @return {point []} * @memberof path */ create3pbezier( p0: point, p1: point, p2: point, p3: point, num: number = 100, tick: number = 1, ) { const pointmum = num; const _tick = tick; const t = _tick / (pointmum - 1); const points = []; for (let i = 0; i < pointmum; i++) { const point = this.getbeziernowpoint3p(p0, p1, p2, p3, i, t); points.push({x: point.x, y: point.y}); } return points; }
拟合算法
问题在于如何得到控制点,我们以比较简单的方法
取 p1-pt-p2的角平分线 c1c2垂直于该条角平分线 c2为p2的投影点取短边作为c1-pt c2-pt的长度对该长度进行缩放 这个长度可以大概理解为曲线的弯曲程度
ab线段 这里简单处理 只使用了二阶的曲线生成 -> ???? 这里可以按照个人想法处理
bc线段使用abc计算出来的控制点c2和bcd计算出来的控制点c3 以此类推
/** * 生成平滑曲线所需的控制点 * * @param {vector2d} p1 * @param {vector2d} pt * @param {vector2d} p2 * @param {number} [ratio=0.3] * @return {*} * @memberof path */ createsmoothlinecontrolpoint( p1: vector2d, pt: vector2d, p2: vector2d, ratio: number = 0.3, ) { const vec1t: vector2d = vector2dminus(p1, pt); const vect2: vector2d = vector2dminus(p1, pt); const len1: number = vec1t.length; const len2: number = vect2.length; const v: number = len1 / len2; let delta; if (v > 1) { delta = vector2dminus( p1, vector2dplus(pt, vector2dminus(p2, pt).scale(1 / v)), ); } else { delta = vector2dminus( vector2dplus(pt, vector2dminus(p1, pt).scale(v)), p2, ); } delta = delta.scale(ratio); const control1: point = { x: vector2dplus(pt, delta).x, y: vector2dplus(pt, delta).y, }; const control2: point = { x: vector2dminus(pt, delta).x, y: vector2dminus(pt, delta).y, }; return {control1, control2}; } /** * 平滑曲线生成 * * @param {point []} points * @param {number} ratio * @return {*} * @memberof path */ createsmoothline(points: point[], ratio: number = 0.3) { const len = points.length; let resultpoints = []; const controlpoints = []; if (len < 3) return; for (let i = 0; i < len - 2; i++) { const {control1, control2} = this.createsmoothlinecontrolpoint( new vector2d(points[i].x, points[i].y), new vector2d(points[i + 1].x, points[i + 1].y), new vector2d(points[i + 2].x, points[i + 2].y), ratio, ); controlpoints.push(control1); controlpoints.push(control2); let points1; let points2; // 首端控制点只用一个 if (i === 0) { points1 = this.create2pbezier(points[i], control1, points[i + 1], 50); } else { console.log(controlpoints); points1 = this.create3pbezier( points[i], controlpoints[2 * i - 1], control1, points[i + 1], 50, ); } // 尾端部分 if (i + 2 === len - 1) { points2 = this.create2pbezier( points[i + 1], control2, points[i + 2], 50, ); } if (i + 2 === len - 1) { resultpoints = [...resultpoints, ...points1, ...points2]; } else { resultpoints = [...resultpoints, ...points1]; } } return resultpoints; }
案例代码
const input = [ { x: 0, y: 0 }, { x: 150, y: 150 }, { x: 300, y: 0 }, { x: 400, y: 150 }, { x: 500, y: 0 }, { x: 650, y: 150 }, ] const s = path.createsmoothline(input); let ctx = document.getelementbyid('cv').getcontext('2d'); ctx.strokestyle = 'blue'; ctx.beginpath(); ctx.moveto(0, 0); for (let i = 0; i < s.length; i++) { ctx.lineto(s[i].x, s[i].y); } ctx.stroke(); ctx.beginpath(); ctx.moveto(0, 0); for (let i = 0; i < input.length; i++) { ctx.lineto(input[i].x, input[i].y); } ctx.strokestyle = 'red'; ctx.stroke(); document.getelementbyid('btn').addeventlistener('click', () => { let app = document.getelementbyid('app'); let index = 0; let move = () => { if (index < s.length) { app.style.left = s[index].x - 10 + 'px'; app.style.top = s[index].y - 10 + 'px'; index++; requestanimationframe(move) } } move() })
附录:vector2d相关的代码
/** * * * @class vector2d * @extends {array} */ class vector2d extends array { /** * creates an instance of vector2d. * @param {number} [x=1] * @param {number} [y=0] * @memberof vector2d * */ constructor(x: number = 1, y: number = 0) { super(); this.x = x; this.y = y; } /** * * @param {number} v * @memberof vector2d */ set x(v) { this[0] = v; } /** * * @param {number} v * @memberof vector2d */ set y(v) { this[1] = v; } /** * * * @readonly * @memberof vector2d */ get x() { return this[0]; } /** * * * @readonly * @memberof vector2d */ get y() { return this[1]; } /** * * * @readonly * @memberof vector2d */ get length() { return math.hypot(this.x, this.y); } /** * * * @readonly * @memberof vector2d */ get dir() { return math.atan2(this.y, this.x); } /** * * * @return {*} * @memberof vector2d */ copy() { return new vector2d(this.x, this.y); } /** * * * @param {*} v * @return {*} * @memberof vector2d */ add(v) { this.x += v.x; this.y += v.y; return this; } /** * * * @param {*} v * @return {*} * @memberof vector2d */ sub(v) { this.x -= v.x; this.y -= v.y; return this; } /** * * * @param {*} a * @return {vector2d} * @memberof vector2d */ scale(a) { this.x *= a; this.y *= a; return this; } /** * * * @param {*} rad * @return {*} * @memberof vector2d */ rotate(rad) { const c = math.cos(rad); const s = math.sin(rad); const [x, y] = this; this.x = x * c + y * -s; this.y = x * s + y * c; return this; } /** * * * @param {*} v * @return {*} * @memberof vector2d */ cross(v) { return this.x * v.y - v.x * this.y; } /** * * * @param {*} v * @return {*} * @memberof vector2d */ dot(v) { return this.x * v.x + v.y * this.y; } /** * 归一 * * @return {*} * @memberof vector2d */ normalize() { return this.scale(1 / this.length); } } /** * 向量的加法 * * @param {*} vec1 * @param {*} vec2 * @return {vector2d} */ function vector2dplus(vec1, vec2) { return new vector2d(vec1.x + vec2.x, vec1.y + vec2.y); } /** * 向量的减法 * * @param {*} vec1 * @param {*} vec2 * @return {vector2d} */ function vector2dminus(vec1, vec2) { return new vector2d(vec1.x - vec2.x, vec1.y - vec2.y); } export {vector2d, vector2dplus, vector2dminus};
总结
到此这篇关于如何利用javascript生成平滑曲线的文章就介绍到这了,更多相关js生成平滑曲线内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!