From 9ee49fefb152f965cb696c06e8f865f50ee2baa4 Mon Sep 17 00:00:00 2001 From: xLinka Date: Tue, 25 Jun 2024 14:40:34 +0100 Subject: [PATCH] Create FibonacciNode.cs --- .../ProtoFlux/Math/FibonacciNode.cs | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 ProjectObsidian/ProtoFlux/Math/FibonacciNode.cs diff --git a/ProjectObsidian/ProtoFlux/Math/FibonacciNode.cs b/ProjectObsidian/ProtoFlux/Math/FibonacciNode.cs new file mode 100644 index 0000000..5c96c5a --- /dev/null +++ b/ProjectObsidian/ProtoFlux/Math/FibonacciNode.cs @@ -0,0 +1,40 @@ +using System; +using Elements.Core; +using FrooxEngine.ProtoFlux; +using ProtoFlux.Core; +using ProtoFlux.Runtimes.Execution; + +namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Math +{ + [NodeCategory("Obsidian/Math")] + [NodeName("Fibonacci")] + public class FibonacciNode : ValueFunctionNode + { + public ValueInput Input; + + protected override int Compute(FrooxEngineContext context) + { + int n = Input.Evaluate(context); + return Fibonacci(n); + } + + private int Fibonacci(int n) + { + if (n < 0) + throw new ArgumentException("Negative numbers are not allowed."); + if (n == 0) + return 0; + if (n == 1) + return 1; + + int a = 0, b = 1, temp; + for (int i = 2; i <= n; i++) + { + temp = a + b; + a = b; + b = temp; + } + return b; + } + } +}