【数学篇】07 # 如何用向量和参数方程描述曲线?

简介: 【数学篇】07 # 如何用向量和参数方程描述曲线?

说明

【跟月影学可视化】学习笔记。



如何用向量描述曲线?

用向量绘制折线的方法来绘制正多边形

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>如何用向量描述曲线</title>
    <style>
        canvas {
            border: 1px dashed salmon;
        }
    </style>
</head>
<body>
    <canvas width="512" height="512"></canvas>
    <script type="module">
        import { Vector2D } from './common/lib/vector2d.js';
        const canvas = document.querySelector('canvas');
        const ctx = canvas.getContext('2d');
        const {width, height} = canvas;
        ctx.translate(0.5 * width, 0.5 * height);
        ctx.scale(1, -1);
        /**
         * 边数 edges
         * 起点 x, y
         * 一条边的长度 step
         * */
        function regularShape(edges = 3, x, y, step) {
            const ret = [];
            const delta = Math.PI * (1 - (edges - 2) / edges);
            let p = new Vector2D(x, y);
            const dir = new Vector2D(step, 0);
            ret.push(p);
            for(let i = 0; i < edges; i++) {
                p = p.copy().add(dir.rotate(delta));
                ret.push(p);
            }
            return ret;
        }
        function draw(points, strokeStyle = 'salmon', fillStyle = null) {
            ctx.strokeStyle = strokeStyle;
            ctx.beginPath();
            ctx.moveTo(...points[0]);
            for(let i = 1; i < points.length; i++) {
                ctx.lineTo(...points[i]);
            }
            ctx.closePath();
            if(fillStyle) {
                ctx.fillStyle = fillStyle;
                ctx.fill();
            }
            ctx.stroke();
        }
        draw(regularShape(3, 128, 128, 100));  // 绘制三角形
        draw(regularShape(6, -64, 128, 50));  // 绘制六边形
        draw(regularShape(11, -64, -64, 30));  // 绘制十一边形
        draw(regularShape(60, 128, -64, 6));  // 绘制六十边形
    </script>
</body>
</html>


5320d63fe5524a3b92a9e121c760b9d9.png



如何用参数方程描述曲线?


1. 画圆

圆可以用一组参数方程来定义。定义了一个圆心在(x0,y0),半径为 r 的圆。


73a48413e2c24cd29caadac8eaedc283.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>画圆</title>
    <style>
        canvas {
            border: 1px dashed salmon;
        }
    </style>
</head>
<body>
    <canvas width="512" height="512"></canvas>
    <script>
        const canvas = document.querySelector('canvas');
        const ctx = canvas.getContext('2d');
        const {width, height} = canvas;
        ctx.translate(0.5 * width, 0.5 * height);
        ctx.scale(1, -1);
        const TAU_SEGMENTS = 60;
        const TAU = Math.PI * 2;
        function arc(x0, y0, radius, startAng = 0, endAng = Math.PI * 2) {
            const ang = Math.min(TAU, endAng - startAng);
            const ret = ang === TAU ? [] : [[x0, y0]];
            const segments = Math.round(TAU_SEGMENTS * ang / TAU);
            for(let i = 0; i <= segments; i++) {
                const x = x0 + radius * Math.cos(startAng + ang * i / segments);
                const y = y0 + radius * Math.sin(startAng + ang * i / segments);
                ret.push([x, y]);
            }
            return ret;
        }
        function draw(points, strokeStyle = 'salmon', fillStyle = null) {
            ctx.strokeStyle = strokeStyle;
            ctx.beginPath();
            ctx.moveTo(...points[0]);
            for(let i = 1; i < points.length; i++) {
                ctx.lineTo(...points[i]);
            }
            ctx.closePath();
            if(fillStyle) {
                ctx.fillStyle = fillStyle;
                ctx.fill();
            }
            ctx.stroke();
        }
        draw(arc(0, 0, 100));
    </script>
</body>
</html>


ad385adb71ac47cca8558c796eb84dfd.png



2. 画圆锥曲线

椭圆

a、b 分别是椭圆的长轴和短轴,当 a = b = r 时,这个方程是就圆的方程式。

f831b50cbac84ac08f7e77d7e2d6d37b.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>椭圆</title>
    <style>
        canvas {
            border: 1px dashed salmon;
        }
    </style>
</head>
<body>
    <canvas width="512" height="512"></canvas>
    <script>
        const canvas = document.querySelector('canvas');
        const ctx = canvas.getContext('2d');
        const {width, height} = canvas;
        ctx.translate(0.5 * width, 0.5 * height);
        ctx.scale(1, -1);
        const TAU_SEGMENTS = 60;
        const TAU = Math.PI * 2;
        function ellipse(x0, y0, radiusX, radiusY, startAng = 0, endAng = Math.PI * 2) {
            const ang = Math.min(TAU, endAng - startAng);
            const ret = ang === TAU ? [] : [[x0, y0]];
            const segments = Math.round(TAU_SEGMENTS * ang / TAU);
            for(let i = 0; i <= segments; i++) {
                const x = x0 + radiusX * Math.cos(startAng + ang * i / segments);
                const y = y0 + radiusY * Math.sin(startAng + ang * i / segments);
                ret.push([x, y]);
            }
            return ret;
        }
        function draw(points, strokeStyle = 'salmon', fillStyle = null) {
            ctx.strokeStyle = strokeStyle;
            ctx.beginPath();
            ctx.moveTo(...points[0]);
            for(let i = 1; i < points.length; i++) {
                ctx.lineTo(...points[i]);
            }
            ctx.closePath();
            if(fillStyle) {
                ctx.fillStyle = fillStyle;
                ctx.fill();
            }
            ctx.stroke();
        }
        draw(ellipse(0, 0, 100, 50));
    </script>
</body>
</html>


14a479cc17924e4fadc2158b1d861175.png

抛物线

抛物线的参数方程。其中 p 是常数,为焦点到准线的距离。

e335b8f6ef03486e95466b1b8affea87.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>抛物线</title>
    <style>
        canvas {
            border: 1px dashed salmon;
        }
    </style>
</head>
<body>
    <canvas width="512" height="512"></canvas>
    <script>
        const canvas = document.querySelector('canvas');
        const ctx = canvas.getContext('2d');
        const {width, height} = canvas;
        ctx.translate(0.5 * width, 0.5 * height);
        ctx.scale(1, -1);
        const LINE_SEGMENTS = 60;
        function parabola(x0, y0, p, min, max) {
            const ret = [];
            for(let i = 0; i <= LINE_SEGMENTS; i++) {
                const s = i / 60;
                const t = min * (1 - s) + max * s;
                const x = x0 + 2 * p * t ** 2;
                const y = y0 + 2 * p * t;
                ret.push([x, y]);
            }
            return ret;
        }
        function draw(points, strokeStyle = 'salmon', fillStyle = null) {
            ctx.strokeStyle = strokeStyle;
            ctx.beginPath();
            ctx.moveTo(...points[0]);
            for(let i = 1; i < points.length; i++) {
                ctx.lineTo(...points[i]);
            }
            ctx.closePath();
            if(fillStyle) {
                ctx.fillStyle = fillStyle;
                ctx.fill();
            }
            ctx.stroke();
        }
        draw(parabola(0, 0, 5.5, -10, 10));
    </script>
</body>
</html>

87fa50b8e3594673ad48e7417ce4e7ca.png


3. 画其他常见曲线

在 lib 下面新建一个 parametric.js 文件,封装一个更简单的 JavaScript 参数方程绘图模块。

// 根据点来绘制图形
function draw(
    points,
    context,
    { strokeStyle = "salmon", fillStyle = null, close = false } = {}
) {
    context.strokeStyle = strokeStyle;
    context.beginPath();
    context.moveTo(...points[0]);
    for (let i = 1; i < points.length; i++) {
        context.lineTo(...points[i]);
    }
    if (close) context.closePath();
    if (fillStyle) {
        context.fillStyle = fillStyle;
        context.fill();
    }
    context.stroke();
}
// 导出高阶函数绘图模块
export function parametric(xFunc, yFunc, zFunc) {
    /**
     * start、end 表示参数方程中关键参数范围的参数
     * seg 表示采样点个数的参数,当 seg 默认 100 时,就表示在 start、end 范围内采样 101(seg+1)个点
     * ...args 后续其他参数是作为常数传给参数方程的数据。
     * */ 
    return function (start, end, seg = 100, ...args) {
        const points = [];
        for (let i = 0; i <= seg; i++) {
            const p = i / seg;
            const t = start * (1 - p) + end * p;
            const x = xFunc(t, ...args); // 计算参数方程组的x
            const y = yFunc(t, ...args); // 计算参数方程组的y
            if (zFunc) {
                points.push(zFunc(x, y));
            } else {
                points.push([x, y]);
            }
        }
        return {
            draw: draw.bind(null, points),
            points, // 生成的顶点数据
        };
    };
}

下面使用上面封装的实现一下抛物线,阿基米德螺旋线,星形线。

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>画其他常见曲线</title>
        <style>
            canvas {
                border: 1px dashed salmon;
            }
        </style>
    </head>
    <body>
        <canvas width="512" height="512"></canvas>
        <script type="module">
            import { parametric } from "./common/lib/parametric.js";
            const canvas = document.querySelector("canvas");
            const ctx = canvas.getContext("2d");
            const { width, height } = canvas;
            const w = 0.5 * width,
                h = 0.5 * height;
            ctx.translate(w, h);
            ctx.scale(1, -1);
            // 绘制坐标轴
            function drawAxis() {
                ctx.save();
                ctx.strokeStyle = "#ccc";
                ctx.beginPath();
                ctx.moveTo(-w, 0);
                ctx.lineTo(w, 0);
                ctx.stroke();
                ctx.beginPath();
                ctx.moveTo(0, -h);
                ctx.lineTo(0, h);
                ctx.stroke();
                ctx.restore();
            }
            drawAxis();
            // 绘制抛物线
            const para = parametric(
                (t) => 25 * t,
                (t) => 25 * t ** 2
            );
            para(-5.5, 5.5).draw(ctx);
            // 绘制阿基米德螺旋线
            const helical = parametric(
                (t, l) => l * t * Math.cos(t),
                (t, l) => l * t * Math.sin(t)
            );
            helical(0, 50, 500, 5).draw(ctx, { strokeStyle: "MediumPurple" });
            // 绘制星形线
            const star = parametric(
                (t, l) => l * Math.cos(t) ** 3,
                (t, l) => l * Math.sin(t) ** 3
            );
            star(0, Math.PI * 2, 50, 150).draw(ctx, { strokeStyle: "Orange" });
        </script>
    </body>
</html>

871e6bdd798e4c7c96e4b86f80b17078.png


4. 画贝塞尔曲线

贝塞尔曲线是一种使用数学方法描述的曲线,被广泛用于计算机图形学和动画中。在矢量图中,贝塞尔曲线用于定义可无限放大的光滑曲线。可以用来构建 Catmull–Rom 曲线。


贝塞尔曲线又分为二阶贝塞尔曲线(Quadratic Bezier Curve)和三阶贝塞尔曲线(Qubic Bezier Curve)。

二阶贝塞尔曲线

二阶贝塞尔曲线由三个点确定,P0是起点,P1是控制点,P2是终点


453e38ee4b0b42589810df534f52597a.png


二阶贝塞尔曲线的原理


9fd9bb39b1c60b095531ac2e2ceef31b.gif

绘制30条从圆心出发,旋转不同角度的二阶贝塞尔曲线

 
         

效果如下

0c28f500e3664893ac5aa75e6043f0f9.png


三阶贝塞尔曲线

三阶贝塞尔曲线的参数方程为:


7adeba4be41547329ee1a8dd84d8e70d.png


三阶贝塞尔曲线的原理示意图:

bd62f1dfea6688f9a9e679bb3529230c.gif

 
         

9a8a734794934d78b0ff7c6dc50c1910.png


目录
相关文章
|
Web App开发 监控 Java
|
应用服务中间件 测试技术 nginx
dpdk环境搭建及运行helloworld测试
dpdk环境搭建及运行helloworld测试
958 0
|
SQL 存储 监控
功能解读|快速上手 OceanBase 数据迁移服务
在上一期的 OceanBase 功能解读系列文章中,我们为大家带来了 ODC 功能解读(戳这里回顾:《OceanBase 开发者中心 ODC 功能解读》),本文将为大家分享数据迁移服务的相关内容。
1580 0
功能解读|快速上手 OceanBase 数据迁移服务
|
25天前
|
存储 弹性计算 人工智能
阿里云轻量应用服务器官网全新升级:200M带宽配置指南、无固定流量及配置价格表
阿里云轻量应用服务器是面向个人开发者与中小企业的轻量化云服务,200Mbps峰值带宽+不限流量,预装WordPress、Docker等30+应用镜像,开箱即用。官网全新升级,涵盖价格、选型、免费试用(2核1G/1月)及AI Agent等解决方案,最低68元/年,新用户秒杀仅38元。阿里云轻量应用服务器官网:https://t.aliyun.com/U/dwftch
222 0
|
7月前
|
人工智能 前端开发 关系型数据库
AI 面试问答系统:一键生成专属面试题,智能评分助你拿 Offer
AI面试问答系统是一款开源智能面试备战工具:上传简历+岗位JD,AI自动生成四维面试题、追问及答案;支持背诵默写智能评分(0-100)、技能掌握度追踪(1-5星)与答案优化。兼容通义千问/GPT/DeepSeek等多模型,本地一键部署,免费开源!
|
8月前
|
人工智能 缓存 API
阿里云百炼Token Plan是什么?阿里云百炼Token Plan团队版全解析:功能、计费、接入与实战配置指南
随着AI智能体与大模型应用的持续普及,阿里云百炼Token Plan团队版将不断扩展模型生态、优化工具适配、提升服务性能,为企业与开发者提供更稳定、更高效、更经济的AI大模型服务能力。无论是日常AI辅助办公、研发编码,还是构建自主智能体,Token Plan团队版都能提供可靠支撑,助力用户释放AI生产力,实现高效协作与创新。
1201 0
|
9月前
|
关系型数据库 MySQL 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
本文梳理阿里云RDS 2026年最新优惠价:MySQL(¥58/年)、MariaDB(¥98)、PostgreSQL(¥118)、SQL Server(¥298),均为新用户包年包月一口价,含ESSD存储、备份、监控等基础服务,支持主备高可用与在线扩容。
|
9月前
|
人工智能 开发工具 开发者
Codex 的两种使用方式:为什么很多人一开始就用错了?
Codex 有两种模式:命令模式(一次性调用,适合脚本/快速任务)和交互模式(类AI员工,支持上下文、连续迭代与工程化操作)。新手常混淆用法,实则前者是“调用AI”,后者才是“雇佣AI”。推荐日常开发优先使用交互模式。
4559 7
|
11月前
|
存储 人工智能 安全
AI智能体的开发费用
2026年AI智能体开发成本两极分化:低代码工具使简单Agent成本降至5千-2万元,而复杂企业级系统仍需30万以上。费用涵盖开发、API、算力及维护,建议从中级智能体切入,结合开源平台与工作流模式降本增效。#AI智能体 #降本策略
|
12月前
|
SQL 关系型数据库 MySQL
MySQL 高效学习指南:从入门到优化的科学路径
本文系统梳理MySQL三阶段学习路径:入门(夯实基础操作)、核心深化(掌握多表关联与事务)、优化进阶(理解索引与执行计划),结合实操案例与资源推荐,助力开发者从CRUD迈向性能调优,真正掌握解决实际问题的数据库技能。

热门文章

最新文章