当前位置:网站首页>U3D_ Infinite Bessel curve
U3D_ Infinite Bessel curve
2022-07-07 15:52:00 【Le_ Sam】
Lua edition
local Bezier = {}
--【 Infinite Bessel curve 】
[email protected] t The corresponding percentage position of the whole line {0-1}
[email protected] node The starting point , The control points 1, The control points 2, The control points 3......, The end point
[email protected] ret Return to the corresponding point / value
function Bezier.BezierCurve(t, node)
local vecs = node
local count = #vecs
local rank = count - 1
local ret = Vector3.zero
local pts = Bezier.GetPascalTriangle(count)
local fs = {}
for i = 1,count do
local f = Bezier.GetFormula(vecs[i],rank,i,pts[i],t)
table.insert(fs,f)
end
for i = 1,#fs do
ret = ret + fs[i].Caclu()
end
return ret
end
-------------------------Private---------------------------------
function Bezier.Exponentiation(num,power)
local n = 1
if power == 0 then return n end
for i = 1,power do
n = n * num
end
return n
end
function Bezier.GetFormula(point,rank,index,ptValue,t)
local ret = {}
ret.point = point
ret.rank = rank
ret.index = index - 1
ret.ptValue = ptValue
ret.t = t
ret.Caclu = function()
local t1 = Bezier.Exponentiation((1 - ret.t),(ret.rank - ret.index))
local t2 = Bezier.Exponentiation(ret.t,ret.index)
return ret.point * t1 * t2 * ret.ptValue
end
return ret
end
function Bezier.GetPascalTriangle(n)
local ret = {}
local t = {}
for i = 1,n do
t[i] = {}
for j = 1,i do
if j == 1 or j == i then
t[i][j] = 1
else
t[i][j] = t[i - 1][j - 1] + t[i - 1][j]
end
end
end
for i = 1,n do
table.insert(ret,t[n][i])
end
t = nil
return ret
end
------------------------ Test functions ------------------------
function Bezier.TEST()
local go = GameObject()
go.name = " Test point "
go.transform.position = Vector3.zero
local go1 = GameObject()
go1.name = " Test point 1"
go1.transform.position = Vector3(-17.4,0,0)
go1.transform:SetParent(go.transform)
local go2 = GameObject()
go2.name = " Test point 2"
go2.transform.position = Vector3(-9.52,0,-3.84)
go2.transform:SetParent(go.transform)
local go3 = GameObject()
go3.name = " Test point 3"
go3.transform.position = Vector3(-2.88,0,5.94)
go3.transform:SetParent(go.transform)
local go4 = GameObject()
go4.name = " Test point 4"
go4.transform.position = Vector3(1.7,0,0)
go4.transform:SetParent(go.transform)
local go5 = GameObject()
go5.name = " Test point 5"
go5.transform.position = Vector3(9.95,0,-4.91)
go5.transform:SetParent(go.transform)
local go6 = GameObject()
go6.name = " Test point 6"
go6.transform.position = Vector3(15.38,0,5.46)
go6.transform:SetParent(go.transform)
local lineCount = 20
go:AddComponent(typeof(UnityEngine.LineRenderer))
local lr = go:GetComponent("LineRenderer")
lr.positionCount = lineCount + 1
for i = 0,lineCount do
lr:SetPosition(i,Bezier.BezierCurve(i / lineCount,go1.transform.position,
go2.transform.position,
go3.transform.position,
go4.transform.position,
go5.transform.position,
go6.transform.position))
end
end
return Bezier
C# edition
//N Jie Bessel
public class Bezier
{
public static Vector3 BezierCurve(float t, List<Vector3> vecs)
{
int count = vecs.Count;
int rank = count - 1;
Vector3 ret = Vector3.zero;
List<int> pts = CacluPascalTriangle(count);
List<Formula> fs = new List<Formula>();
for (int i = 0; i < count; i++)
{
Formula f = new Formula(vecs[i], rank, i, pts[i], t);
fs.Add(f);
}
for (int i = 0; i < fs.Count; i++)
{
ret += fs[i].Caclu();
}
return ret;
}
public static Vector3 BezierCurve(float t, params Vector3[] vecs)
{
int count = vecs.Length;
int rank = count - 1;
Vector3 ret = Vector3.zero;
List<int> pts = CacluPascalTriangle(count);
List<Formula> fs = new List<Formula>();
for (int i = 0; i < count; i++)
{
Formula f = new Formula(vecs[i], rank, i, pts[i], t);
fs.Add(f);
}
for (int i = 0; i < fs.Count; i++)
{
ret += fs[i].Caclu();
}
return ret;
}
public static List<int> CacluPascalTriangle(int n)
{
List<int> ret = new List<int>();
int[,] array = new int[n, n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
if (j == 0 || i == j)
{
array[i, j] = 1;
}
else
{
array[i, j] = array[i - 1, j - 1] + array[i - 1, j];
}
}
}
for (int i = 0; i < n; i++)
{
ret.Add(array[n - 1, i]);
}
return ret;
}
public class Formula
{
// spot
public Vector3 point;
// Stratum
public int rank;
// Indexes
public int index;
// Yang hui triangle
public int PTValue;
public float t;
public Formula(Vector3 point, int rank, int index, int pTValue, float t)
{
this.point = point;
this.rank = rank;
this.index = index;
this.PTValue = pTValue;
this.t = t;
}
public float Exponentiation(float num, int power)
{
float n = 1;
if (power == 0)
{
return n;
}
for (int i = 0; i < power; i++)
{
n *= num;
}
return n;
}
public Vector3 Caclu()
{
Vector3 P = Vector3.zero;
float t1 = Exponentiation((1 - t), (rank - index));
float t2 = Exponentiation(t, index);
P = PTValue * point * t1 * t2;
return P;
}
}
}
边栏推荐
- 无线传感器网络--ZigBee和6LoWPAN
- SPI master rx time out中断
- 一大波开源小抄来袭
- unnamed prototyped parameters not allowed when body is present
- 10 schemes to ensure interface data security
- [quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)
- How to create Apple Developer personal account P8 certificate
- 强化实时数据管理,英方软件助力医保平台安全建设
- 航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
- Do not use memset to clear floating-point numbers
猜你喜欢
Monthly observation of internet medical field in May 2022
When opening the system window under UE4 shipping, the problem of crash is attached with the plug-in download address
webgl_ Enter the three-dimensional world (2)
unnamed prototyped parameters not allowed when body is present
LeetCode1_ Sum of two numbers
C4D learning notes 2- animation - timeline and time function
Cut ffmpeg as needed, and use emscripten to compile and run
HPDC smart base Talent Development Summit essay
Mesh merging under ue4/ue5 runtime
Getting started with webgl (4)
随机推荐
Three. Introduction to JS learning notes 17: mouse control of 3D model rotation of JSON file
Three. JS introductory learning notes 10:three JS grid
Detailed explanation of unity hot update knowledge points and introduction to common solution principles
Create lib Library in keil and use lib Library
[quick start of Digital IC Verification] 24. AHB sramc of SystemVerilog project practice (4) (AHB continues to deepen)
How to understand that binary complement represents negative numbers
Getting started with webgl (4)
持续创作,还得靠它!
XMIND frame drawing tool
Getting started with webgl (3)
Syntax of generator function (state machine)
How to deploy the super signature distribution platform system?
Streaming end, server end, player end
HW primary flow monitoring, what should we do
UE4 exports the picture + text combination diagram through ucanvasrendertarget2d
Please supervise the 2022 plan
[quick start of Digital IC Verification] 29. Ahb-sramc (9) (ahb-sramc svtb overview) of SystemVerilog project practice
L'application à l'échelle de la normalisation mature des produits ai des compagnies maritimes, cimc, leader mondial de l'intelligence artificielle portuaire et maritime / intelligence artificielle des
Three. JS introductory learning notes 15: threejs frame animation module
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)