Curve 曲线 工具
最近研究了曲线绘制的工具,主要是2D方程的绘制。综合了许多工具,完成了一下两个脚本。
绘制的工具:
using UnityEngine;
using System.Collections;
using UnityEngine.UI; public class Curve : MaskableGraphic
{
public Color color;
public float m_LineWidth = ;
[Range(, )]
public int Acc = ;
public System.Func<float, float> xConvert = x=>x;
public System.Func<float, float> yConvert = y => y;
public System.Func<float, float> MainFunc = f => f;
protected override void OnPopulateMesh(VertexHelper vh)
{
var rect = this.rectTransform.rect;
vh.Clear();
Debug.Log(rect.xMin);
Debug.Log(rect.xMax);
Vector2 pos_first = new Vector2(rect.xMin, CalcY() * rect.height+rect.yMin); for (float x = rect.xMin + Acc; x < rect.xMax; x += Acc)
{
Vector2 pos = new Vector2(x, CalcY((x - rect.xMin) / rect.width) * rect.height+rect.yMin);
var quad = GenerateQuad(pos_first, pos); vh.AddUIVertexQuad(quad); pos_first = pos;
} Vector2 pos_last = new Vector2(rect.xMax, CalcY() * rect.height+rect.yMin);
vh.AddUIVertexQuad(GenerateQuad(pos_first, pos_last)); for (int i = ; i < vh.currentVertCount - ; i += )
{
vh.AddTriangle(i + , i + , i + );
vh.AddTriangle(i + , i + , i + );
}
Debug.Log("PopulateMesh..." + vh.currentVertCount);
} //根据曲线组件来计算Y
//**** x 为 (x - rect.xMin) / rect.width) 所以范围 为 0~1
//返回值 为y ,取值范围限定在 0~1;
private float CalcY(float x)
{
return yConvert((MainFunc(xConvert(x))));
} private UIVertex[] GenerateQuad(Vector2 pos1, Vector2 pos2)
{
float dis = Vector2.Distance(pos1, pos2);
float y = m_LineWidth * 0.5f * (pos2.x - pos1.x) / dis;
float x = m_LineWidth * 0.5f * (pos2.y - pos1.y) / dis; if (y <= )
y = -y;
else
x = -x; UIVertex[] vertex = new UIVertex[]; vertex[].position = new Vector3(pos1.x + x, pos1.y + y);
vertex[].position = new Vector3(pos2.x + x, pos2.y + y);
vertex[].position = new Vector3(pos2.x - x, pos2.y - y);
vertex[].position = new Vector3(pos1.x - x, pos1.y - y); for (int i = ; i < vertex.Length; i++)
{
vertex[i].color = color;
} return vertex;
}
}
控制脚本:
using UnityEngine;
using System.Collections;
using System;
[RequireComponent(typeof(Curve))]
public class CurveItem : MonoBehaviour {
private Curve curve;
private Vector2[] posArray; //绘制谱图x 最小值
[Header("绘制谱图x 最小值")]
public float xMin;
//绘制谱图 x 最大值
[Header("绘制谱图x 最大值")]
public float xMax;
//绘制 谱图 y 最小值
[Header("绘制谱图y 最小值")]
public float yMin;
//绘制谱图 y 最大值
[Header("绘制谱图y 最大值")]
public float yMax;
[Header("绘制谱图取样间隔")]
public float delta;
// Use this for initialization
void Start () {
curve = this.GetComponentInChildren<Curve>();
} #region 主要方法 public void ShowPutu(Func<float, float> func)
{
// DrawLine(LineDrawer.GetSampleArray(func, xMin, xMax, delta));
curve.MainFunc = func;
curve.xConvert = MathTool.GetLinear(, xMin, , xMax);
curve.yConvert = MathTool.GetLinear(yMin, , yMax, 1f);
curve.enabled = false;
curve.enabled = true;
} #endregion #region Test private Func<float, float> func = x=>x*x; [ContextMenu("谱图")]
private void Test()
{
ShowPutu(func);
} #endregion
}
Math工具
//获取线性方程
public static Func<float, float> GetLinear(float x0, float y0, float x1, float y1)
{
Func<float, float> linear = x =>
{
float a = (y1 - y0) / (x1 - x0);
return x*a+y0-x0*a;
};
return linear;
}
调用以下方法即可。
ShowPutu(Func<float, float> func)
将两个脚本都挂载在Canvas下的一个空物体上。
演示 x=>x*x:

演示 x=>Mathf.Asin(x):

Curve 曲线 工具的更多相关文章
- canvas基础[二]教你编写贝塞尔曲线工具
贝塞尔曲线 bezierCurveTo 在线工具 https://canvature.appspot.com/ [感觉这个好用一些] https://blogs.sitepointstatic.com ...
- iOS - Quartz 2D 贝塞尔曲线
1.贝塞尔曲线 贝塞尔曲线(Bézier curve),又称贝兹曲线或贝济埃曲线,是应用于二维图形应用程序的数学曲线.一般的矢量图形软件通过它来精确画出曲线,贝兹曲线由线段与节点组成,节点是可拖动的支 ...
- javascript -- canvas绘制曲线
绘制曲线有几种思路: 1.通过quadraticCurveTo(controlX, controlY, endX, endY)方法来绘制二次曲线 2.通过bezierCurveTo(controlX1 ...
- canvas教程(三) 绘制曲线
经过 canvas 教程(二) 绘制直线 我们知道了 canvas 的直线是怎么绘制的 而本次是给大家带来曲线相关的绘制 绘制圆形 在 canvas 中我们可以使用 arc 方法画一个圆 contex ...
- Android - Animation 贝塞尔曲线之美
概述 贝塞尔曲线于1962,由法国工程师皮埃尔·贝塞尔所广泛发表,他运用贝塞尔曲线来为汽车的主体进行设计.贝塞尔曲线最初由Paul de Casteljau于1959年运用de Casteljau演算 ...
- Envelope几何对象 Curve对象几何对象 Multipatch几何对象 Geometry集合接口 IGeometryCollection接口
Envelope是所有几何对象的外接矩形,用于表示几何对象的最小边框,所有的几何对象都有一个Envelope对象,IEnvelope是Envelope对象的主要接口,通过它可以获取几何对象的XMax, ...
- WPF将点列连接成光滑曲线——贝塞尔曲线
原文:WPF将点列连接成光滑曲线--贝塞尔曲线 背景 最近在写一个游戏场景编辑器,虽然很水,但是还是遇到了不少问题.连接离散个点列成为光滑曲线就是一个问题.主要是为了通过关键点产生2D的赛道场景.总之 ...
- Canvas 线性图形(三):曲线
前言 画曲线要用到二次贝塞尔曲线或三次贝塞尔曲线.贝塞尔曲线是计算机图形学中相当重要的参数曲线,在一些比较成熟的位图软件中也有贝塞尔曲线工具,如 PhotoShop. 二次贝塞尔曲线 二次贝塞尔曲线在 ...
- truetype技术和矢量字库的技术原理及实现(转)
源:truetype技术和矢量字库的技术原理及实现 广泛汉字矢量字库(HZKSLxxJ)格式 在矢量字库中,每个汉字都是以128 X 128点阵制成矢量数据.每个汉字 ...
随机推荐
- 【书】.NET及计算机类相关书籍,持续更新...
一级目录 链接: https://pan.baidu.com/s/1y3osr3YCQ7XlM81RzkN1eQ 提取码: gs3r 二级目录 链接: https://pan.baidu.com/s/ ...
- webdirver.Chrom() selenium webdirver调用谷歌浏览器的问题解决
第一个坑: 没有将谷歌驱动放到 /usr/local/bin 目录下 第二个坑: 没有将谷歌浏览器.exe放在 /usr/local/bin 目录下(注意:驱动和浏览器在一个路径下才能使 ...
- java中的默认类型+spring
默认引用类型 Integer Character String(包装类)... private Integer age ; 默认是null 默认基本类型 int char string.privit ...
- java小白也能懂的面向对象
类是对一组具有共同特征和行为的对象的抽象描述. 理解 [1]类是专门用于描述现实生活中的事物的. [2]类描述的事物都具有共同的特征和行为. [3]类就是我们通常所说的 “类型” ,描述的是一个群体性 ...
- hsy单词
题意:略 在ac自动机上,一个节点出现的次数等于能通过fail到它的节点的次数之和.而叶节点就等于它被爬过的次数. #include <iostream> #include <cst ...
- mtcnn
1.widerface样本标签处理 图片名 x1 y1 x2 y2 x11 y11 x22 y22 多人脸框 # -*- coding: utf- -*- ""&qu ...
- x变成y的最少操作次数(层次遍历)
输入x,y,x为源数字,y为目标值.输出x变成y的最少操作次数. x每次可以执行三种操作:-1 . +1 . x2: 如 x=5,y=8:5-1=4,4x2=8;所以输出结果为2(次操作). 可以发现 ...
- 12、类成员访问修饰符public/private/producted/readonly
1.private 类的私有成员 private 类的私有成员,只能在内部访问,在外部访问不到,无法被继承,我们可以将不需要被外部修改的定义为私有的 私有成员,只能在内部访问,在外部访问不到 priv ...
- raid10模型比raid01模型的冗余度高
RAID10和RAID01的冗余度看起来差不多,但本质上有一定的区别. RAID10:左边损坏1块盘,右边还可以容忍一块盘损坏而不影响整体的数据;冗余度=2 RAID01:左边损坏1块盘,此时左边已经 ...
- 详解Linux下swig 3.0.12的手动安装过程
详解Linux下swig 3.0.12的手动安装过程 首先 从http://www.linuxfromscratch.org/blfs/view/cvs/general/swig.html上下载swi ...