【视觉基础篇】15 # 如何用极坐标系绘制有趣图案?

简介: 【视觉基础篇】15 # 如何用极坐标系绘制有趣图案?

说明

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



极坐标示意图

极坐标系使用相对极点的距离,以及与 x 轴正向的夹角来表示点的坐标,如(3,60°)。



d18bbd4e77cb4265af02da7f4dd8e0c5.png

直角坐标和极坐标相互转换

// 直角坐标影射为极坐标
function toPolar(x, y) {
  const r = Math.hypot(x, y);
  const θ= Math.atan2(y, x);
  return [r, θ];
}
// 极坐标映射为直角坐标
function fromPolar(r, θ) {
  const x = r * cos(θ);
  const y = r * sin(θ);
  return [x, y];
}


如何用极坐标方程绘制曲线

<!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();
            // fromPolar 作为 parametric 的参数是坐标映射函数,通过它可以将任意坐标映射为直角坐标
            const fromPolar = (r, theta) => {
                return [r * Math.cos(theta), r * Math.sin(theta)];
            };
            // 画一个半径为 200 的半圆
            const arc = parametric(
                (t) => 200,
                (t) => t,
                fromPolar
            );
            arc(0, Math.PI).draw(ctx);
            // 玫瑰线
            const rose = parametric(
                (t, a, k) => a * Math.cos(k * t),
                (t) => t,
                fromPolar
            );
            rose(0, Math.PI, 100, 200, 5).draw(ctx, { strokeStyle: "blue" });
            // 心形线
            const heart = parametric(
                (t, a) => a - a * Math.sin(t),
                (t) => t,
                fromPolar
            );
            heart(0, 2 * Math.PI, 100, 100).draw(ctx, { strokeStyle: "red" });
            // 双纽线
            const foliumRight = parametric(
                (t, a) => Math.sqrt(2 * a ** 2 * Math.cos(2 * t)),
                (t) => t,
                fromPolar
            );
            const foliumLeft = parametric(
                (t, a) => -Math.sqrt(2 * a ** 2 * Math.cos(2 * t)),
                (t) => t,
                fromPolar
            );
            foliumRight(-Math.PI / 4, Math.PI / 4, 100, 100).draw(ctx, {
                strokeStyle: "green",
            });
            foliumLeft(-Math.PI / 4, Math.PI / 4, 100, 100).draw(ctx, {
                strokeStyle: "green",
            });
        </script>
    </body>
</html>

002819fe83a04e8ba66c50c733d1476f.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>
        <script src="./common/lib/gl-renderer.js"></script>
        <canvas width="512" height="512"></canvas>
        <script>
            const vertex = `
                attribute vec2 a_vertexPosition;
                attribute vec2 uv;
                varying vec2 vUv;
                void main() {
                    gl_PointSize = 1.0;
                    vUv = uv;
                    gl_Position = vec4(a_vertexPosition, 1, 1);
                }
            `;
            // // 三瓣玫瑰线
            // const fragment = `
            //     #ifdef GL_ES
            //     precision highp float;
            //     #endif
            //     varying vec2 vUv;
            //     vec2 polar(vec2 st) {
            //         return vec2(length(st), atan(st.y, st.x));
            //     }
            //     void main() {
            //         vec2 st = vUv - vec2(0.5);
            //         st = polar(st);
            //         float d = 0.5 * cos(st.y * 3.0) - st.x;
            //         gl_FragColor.rgb = smoothstep(-0.01, 0.01, d) * vec3(1.0);
            //         gl_FragColor.a = 1.0;
            //     }
            // `;
            // // 不同瓣数的玫瑰线图案
            // const fragment = `
            //     #ifdef GL_ES
            //     precision highp float;
            //     #endif
            //     varying vec2 vUv;
            //     uniform float u_k;
            //     vec2 polar(vec2 st) {
            //         return vec2(length(st), atan(st.y, st.x));
            //     }
            //     void main() {
            //         vec2 st = vUv - vec2(0.5);
            //         st = polar(st);
            //         float d = 0.5 * cos(st.y * u_k) - st.x;
            //         gl_FragColor.rgb = smoothstep(-0.01, 0.01, d) * vec3(1.0);
            //         gl_FragColor.a = 1.0;
            //     }
            // `;
            // 花瓣线
            const fragment = `
                #ifdef GL_ES
                precision highp float;
                #endif
                varying vec2 vUv;
                uniform float u_k;
                vec2 polar(vec2 st) {
                    return vec2(length(st), atan(st.y, st.x));
                }
                void main() {
                    vec2 st = vUv - vec2(0.5);
                    st = polar(st);
                    float d = 0.5 * abs(cos(st.y * u_k * 0.5)) - st.x;
                    gl_FragColor.rgb = smoothstep(-0.01, 0.01, d) * vec3(1.0);
                    gl_FragColor.a = 1.0;
                }
            `;
            // // 葫芦图案
            // const fragment = `
            //     #ifdef GL_ES
            //     precision highp float;
            //     #endif
            //     varying vec2 vUv;
            //     uniform float u_k;
            //     uniform float u_scale;
            //     uniform float u_offset;
            //     vec2 polar(vec2 st) {
            //         return vec2(length(st), atan(st.y, st.x));
            //     }
            //     void main() {
            //         vec2 st = vUv - vec2(0.5);
            //         st = polar(st);
            //         float d = u_scale * 0.5 * abs(cos(st.y * u_k * 0.5)) - st.x + u_offset;
            //         gl_FragColor.rgb = smoothstep(-0.01, 0.01, d) * vec3(1.0);
            //         gl_FragColor.a = 1.0;
            //     }
            // `;
            // 花苞图案
            // const fragment = `
            //     #ifdef GL_ES
            //     precision highp float;
            //     #endif
            //     varying vec2 vUv;
            //     uniform float u_k;
            //     uniform float u_scale;
            //     uniform float u_offset;
            //     vec2 polar(vec2 st) {
            //         return vec2(length(st), atan(st.y, st.x));
            //     }
            //     void main() {
            //         vec2 st = vUv - vec2(0.5);
            //         st = polar(st);
            //         float d = smoothstep(-0.3, 1.0, u_scale * 0.5 * cos(st.y * u_k) + u_offset) - st.x;
            //         gl_FragColor.rgb = smoothstep(-0.01, 0.01, d) * vec3(1.0);
            //         gl_FragColor.a = 1.0;
            //     }
            // `;
            const canvas = document.querySelector("canvas");
            const renderer = new GlRenderer(canvas);
            const program = renderer.compileSync(fragment, vertex);
            renderer.useProgram(program);
            // // 不同瓣数的玫瑰线图案
            // renderer.uniforms.u_k = 2;
            // setInterval(() => {
            //     renderer.uniforms.u_k += 2;
            // }, 200);
            // 花瓣线
            // renderer.uniforms.u_k = 3;
            renderer.uniforms.u_k = 1.3; // 1.3 的情况下是苹果
            // // 葫芦图案
            // renderer.uniforms.u_k = 1.7;
            // renderer.uniforms.u_scale = 0.5; // default 1.0
            // renderer.uniforms.u_offset = 0.2; // default 0.0
            // // 花苞图案
            // renderer.uniforms.u_k = 5;
            // renderer.uniforms.u_scale = 0.2; // default 1.0
            // renderer.uniforms.u_offset = 0.2; // default 0.0
            renderer.setMeshData([
                {
                    positions: [
                        [-1, -1],
                        [-1, 1],
                        [1, 1],
                        [1, -1],
                    ],
                    attributes: {
                        uv: [
                            [0, 0],
                            [0, 1],
                            [1, 1],
                            [1, 0],
                        ],
                    },
                    cells: [
                        [0, 1, 2],
                        [2, 0, 3],
                    ],
                },
            ]);
            renderer.render();
        </script>
    </body>
</html>

97e492d7fec644c1958c2914cf58d39d.png




极坐标系如何实现角向渐变?

角向渐变(Conic Gradients)就是以图形中心为轴,顺时针地实现渐变效果。

<!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;
        }
        div.conic {
            display: inline-block;
            width: 150px;
            height: 150px;
            border-radius: 50%;
            background: conic-gradient(red 0%, green 45%, blue);
        }
    </style>
</head>
<body>
    <script src="./common/lib/gl-renderer.js"></script>
    <canvas width="512" height="512"></canvas>
    <div class="conic"></div>
    <script>
        const vertex = `
            attribute vec2 a_vertexPosition;
            attribute vec2 uv;
            varying vec2 vUv;
            void main() {
                gl_PointSize = 1.0;
                vUv = uv;
                gl_Position = vec4(a_vertexPosition, 1, 1);
            }
        `;
        const fragment = `
            #ifdef GL_ES
            precision highp float;
            #endif
            varying vec2 vUv;
            vec2 polar(vec2 st) {
                return vec2(length(st), atan(st.y, st.x));
            }
            void main() {
                vec2 st = vUv - vec2(0.5);
                st = polar(st);
                float d = smoothstep(st.x, st.x + 0.01, 0.2);
                // 将角度范围转换到0到2pi之间
                if(st.y < 0.0) st.y += 6.28;
                // 计算p的值,也就是相对角度,p取值0到1
                float p = st.y / 6.28;
                if(p < 0.45) {
                    // p取0到0.45时从红色线性过渡到绿色
                    gl_FragColor.rgb = d * mix(vec3(1.0, 0, 0), vec3(0, 0.5, 0), p /  0.45);
                } else {
                    // p超过0.45从绿色过渡到蓝色
                    gl_FragColor.rgb = d * mix(vec3(0, 0.5, 0), vec3(0, 0, 1.0), (p - 0.45) / (1.0 - 0.45));
                }
                gl_FragColor.a = 1.0;
            }
        `;
        const canvas = document.querySelector("canvas");
        const renderer = new GlRenderer(canvas);
        const program = renderer.compileSync(fragment, vertex);
        renderer.useProgram(program);
        renderer.setMeshData([
            {
                positions: [
                    [-1, -1],
                    [-1, 1],
                    [1, 1],
                    [1, -1],
                ],
                attributes: {
                    uv: [
                        [0, 0],
                        [0, 1],
                        [1, 1],
                        [1, 0],
                    ],
                },
                cells: [
                    [0, 1, 2],
                    [2, 0, 3],
                ],
            },
        ]);
        renderer.render();
    </script>
</body>
</html>

1ae78d1ada3c484c8328a5fa59b3070b.png



极坐标如何绘制 HSV 色轮?

只需要将像素坐标转换为极坐标,再除以 2π,就能得到 HSV 的 H 值。然后用鼠标位置的 x、y 坐标来决定 S 和 V 的值。

<!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>极坐标如何绘制 HSV 色轮</title>
    <style>
        canvas {
            border: 1px dashed salmon;
        }
    </style>
</head>
<body>
    <script src="./common/lib/gl-renderer.js"></script>
    <canvas width="512" height="512"></canvas>
    <div class="conic"></div>
    <script>
        const vertex = `
            attribute vec2 a_vertexPosition;
            attribute vec2 uv;
            varying vec2 vUv;
            void main() {
                gl_PointSize = 1.0;
                vUv = uv;
                gl_Position = vec4(a_vertexPosition, 1, 1);
            }
        `;
        const fragment = `
            #ifdef GL_ES
            precision highp float;
            #endif
            varying vec2 vUv;
            uniform vec2 uMouse;
            vec3 hsv2rgb(vec3 c){
            vec3 rgb = clamp(abs(mod(c.x*6.0+vec3(0.0,4.0,2.0), 6.0)-3.0)-1.0, 0.0, 1.0);
            rgb = rgb * rgb * (3.0 - 2.0 * rgb);
            return c.z * mix(vec3(1.0), rgb, c.y);
            }
            vec2 polar(vec2 st) {
            return vec2(length(st), atan(st.y, st.x));
            }
            void main() {
                vec2 st = vUv - vec2(0.5);
                st = polar(st);
                float d = smoothstep(st.x, st.x + 0.01, 0.2);
                if(st.y < 0.0) st.y += 6.28;
                float p = st.y / 6.28;
                gl_FragColor.rgb = d * hsv2rgb(vec3(p, uMouse.x, uMouse.y));
                gl_FragColor.a = 1.0;
            }
        `;
        const canvas = document.querySelector("canvas");
        const renderer = new GlRenderer(canvas);
        const program = renderer.compileSync(fragment, vertex);
        renderer.useProgram(program);
        renderer.uniforms.uMouse = [0.5, 0.5];
        renderer.setMeshData([
            {
                positions: [
                    [-1, -1],
                    [-1, 1],
                    [1, 1],
                    [1, -1],
                ],
                attributes: {
                    uv: [
                        [0, 0],
                        [0, 1],
                        [1, 1],
                        [1, 0],
                    ],
                },
                cells: [
                    [0, 1, 2],
                    [2, 0, 3],
                ],
            },
        ]);
        renderer.render();
        canvas.addEventListener('mousemove', (e) => {
            const {x, y, width, height} = e.target.getBoundingClientRect();
            renderer.uniforms.uMouse = [(e.x - x) / width, 1.0 - (e.y - y) / height];
        });
    </script>
</body>
</html>

image.gif




圆柱坐标与球坐标

圆柱坐标系是一种三维坐标系,又被称为半极坐标系。可以用来绘制一些三维曲线,比如螺旋线、圆内螺旋线、费马曲线等等。

570fd148517d42bdbdb12111b4d1b7e8.png

直角坐标系和圆柱坐标系也可以相互转换,公式如下:

96ea893ef3af42408f24aa8abb0077ba.png


球坐标系用在三维图形绘制、球面定位、碰撞检测等。

07d3620bcec74991bf259fa3c3c9bd9e.png


圆柱坐标系可以和球坐标系相互转换,公式如下:

ab0064408132431a8824b7a775ee3d56.png


目录
相关文章
|
JavaScript 前端开发 数据安全/隐私保护
vue element plus Input 输入框
vue element plus Input 输入框
1478 0
|
机器学习/深度学习 缓存 并行计算
NVIDIA Tesla GPU系列P4、T4、P40以及V100参数性能对比
NVIDIA Tesla系列GPU适用于高性能计算(HPC)、深度学习等超大规模数据计算,Tesla系列GPU能够处理解析PB级的数据,速度比使用传统CPU快几个数量级,NVIDIA Tesla GPU系列P4、T4、P40以及V100是Tesla GPU系列的明星产品,云服务器吧分享NVIDIA.
87791 1
|
编解码
Google Earth Engine(GEE)——监督分类出现ConfusionMatrix (Error) User memory limit exceeded.解决方案
Google Earth Engine(GEE)——监督分类出现ConfusionMatrix (Error) User memory limit exceeded.解决方案
1118 0
|
12月前
|
机器学习/深度学习 人工智能 监控
云原生AI应用开发
本指南系统阐述云原生AI应用开发实践路径,涵盖MLOps体系构建、PAI-DSW开发平台、特征工程管理、AutoML模型训练、A/B测试部署、全链路监控及AI-CICD流水线,结合阿里云PAI工具链与行业案例,助力企业实现高效、稳定、可迭代的AI应用落地。(238字)
646 0
|
12月前
|
人工智能 自然语言处理 搜索推荐
AI客服选型指南:2025年12月主流智能客服系统产品测评
随着企业数字化加速,智能客服正向全渠道、智能化、情感化演进,成为服务与增长双引擎。本文详解瓴羊Quick Service、智齿科技等主流厂商的核心优势与适用场景,并从需求匹配、智能化水平、集成能力等维度提供选型指南,展望情感计算、多模态交互与业务融合的未来趋势,助力企业高效决策。瓴羊 Quick Service 依托阿里巴巴20年服务运营经验与阿里云全球基础设施,为企业提供全渠道、全链路、全场景的智能客服解决方案,覆盖售前咨询、售中支持、售后维护全流程,适配零售、电商、汽车、互联网等多行业需求。
|
11月前
|
运维 并行计算 物联网
用Pandas处理时间序列:滑动窗口、重采样与趋势分析
本文详解Pandas在时间序列分析中的三大核心工具:滑动窗口、重采样与趋势分析,结合物联网、金融、气象等真实场景,展示如何用简洁代码实现异常检测、周期识别与趋势预测,提升数据处理效率与决策精度。
784 0
|
机器学习/深度学习 编解码 人工智能
Kimi开源MoE架构多模态推理模型,小激活参数,大能量!
最近Moonshot AI推出了 Kimi-VL,这是一个高效的开源混合专家(MoE)视觉-语言模型(VLM),它提供了先进的多模态推理、长上下文理解以及强大的代理能力——所有这些功能都只需激活其语言解码器中的2.8B参数(Kimi-VL-A3B)。
1396 1
|
网络协议 应用服务中间件 网络安全
IP申请SSL证书的条件和方法
为IP地址申请SSL证书与域名证书流程不同,主要因SSL基于域名验证。部分CA允许为公有或私有IP地址申请证书,需满足拥有IP所有权、支持单IP或自签名证书、IP可公开访问及符合CA政策等条件。申请步骤包括访问CA官网、选择证书类型、提交申请、验证所有权并安装证书。替代方案是使用自签名证书,适合内部网络或开发环境。
1993 11
|
关系型数据库 MySQL 数据安全/隐私保护
mysql8.0.26忘记密码后重置(centos8)
mysql8.0.26忘记密码后重置(centos8)
1198 2
|
供应链 搜索推荐 数据挖掘
3分钟讲透服装行业数字化转型新方案!附服装数字化成功案例
中国服装企业在数字化转型中面临信息部门支撑不足、系统优化成本高、数据整合不力等问题。织信低代码平台提出解决方案,强调转型而非数字化本身是关键。该平台助企业构建经营管理系统,提升数据分析能力,完善业务流程,消除信息孤岛,并降低数字化成本。以服装企业LC为例,通过织信低代码,4个月内替换优化多系统,提升开发效率71%,节省百万成本,展示低代码在数字化转型中的应用效果。

热门文章

最新文章