当前位置:网站首页>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;
}
}
}
边栏推荐
- 如何在shell中实现 backspace
- numpy--疫情数据分析案例
- Zhongang Mining: Fluorite continues to lead the growth of new energy market
- [quick start of Digital IC Verification] 26. Ahb-sramc of SystemVerilog project practice (6) (basic points of APB protocol)
- 2022山东智慧养老展,适老穿戴设备展,养老展,山东老博会
- Detailed explanation of Cocos creator 2.4.0 rendering process
- Gd32 F3 pin mapping problem SW interface cannot be burned
- [quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)
- Three. JS introductory learning notes 11:three JS group composite object
- webgl_ Enter the three-dimensional world (1)
猜你喜欢
HPDC smart base Talent Development Summit essay
It's different for rich people to buy a house
postman生成时间戳,未来时间戳
2022 all open source enterprise card issuing network repair short website and other bugs_ 2022 enterprise level multi merchant card issuing platform source code
【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境
Create lib Library in keil and use lib Library
Ida Pro reverse tool finds the IP and port of the socket server
Three. JS introductory learning notes 18: how to export JSON files with Blender
The download button and debug button in keil are grayed out
Numpy -- epidemic data analysis case
随机推荐
C4D learning notes 3- animation - animation rendering process case
航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
Starting from 1.5, build a microservice framework link tracking traceid
Zhongang Mining: Fluorite continues to lead the growth of new energy market
保证接口数据安全的10种方案
A link opens the applet code. After compilation, it is easy to understand
Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
[original] all management without assessment is nonsense!
JS array foreach source code parsing
Configure mongodb database in window environment
招标公告:福建省农村信用社联合社数据库审计系统采购项目(重新招标)
Gd32 F3 pin mapping problem SW interface cannot be burned
The difference between full-time graduate students and part-time graduate students!
20th anniversary of agile: a failed uprising
Cocos makes Scrollview to realize the effect of zooming in the middle and zooming out on both sides
Wireless sensor networks -- ZigBee and 6LoWPAN
2022山东智慧养老展,适老穿戴设备展,养老展,山东老博会
UE4 exports the picture + text combination diagram through ucanvasrendertarget2d
The "go to definition" in VS2010 does not respond or prompts the solution of "symbol not found"