-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExampleScript.cs
More file actions
152 lines (132 loc) · 5.73 KB
/
Copy pathExampleScript.cs
File metadata and controls
152 lines (132 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System.Collections.Generic;
using UnityEngine;
using System.Text.RegularExpressions;
public class ExampleScript : MonoBehaviour
{
public static float EvaluateExpression(string equation, Dictionary<string, float> variables)
{
//replace pi with is mathematical version
//equation = HandlePi(equation);
// Substitute variables in trigonometric functions first
equation = EvaluateTrigonometricFunctions(equation, variables);
// Handle implicit multiplication
equation = InsertImplicitMultiplication(equation);
// Replace all other variables
foreach (var variable in variables)
{
equation = equation.Replace(variable.Key, variable.Value.ToString());
}
// Handle nested expressions
equation = EvaluateNestedExpressions(equation);
equation = FixNests(equation);
// Evaluate the final equation
if (ExpressionEvaluator.Evaluate(equation, out float result))
{
return result;
}
else
{
Debug.LogError("Failed to evaluate expression: " + equation);
return float.NaN; // Return NaN if the evaluation fails
}
}
/* private static string HandlePi(string equation)
{
// Replace any instance of π or 'pi' with Mathf.PI
equation = equation.Replace("π", Mathf.PI.ToString());
equation = equation.Replace("pi", Mathf.PI.ToString());
return equation;
}
*/
private static string InsertImplicitMultiplication(string equation)
{
// Handle numbers and variables adjacent to parentheses
equation = Regex.Replace(equation, @"([a-zA-Z])\(", "$1*("); // e.g., x(2-1) -> x*(2-1)
equation = Regex.Replace(equation, @"(\d)\(", "$1*("); // e.g., 2(2+1) -> 2*(2+1)
// Handle cases where there's a variable or number before a closing parenthesis
equation = Regex.Replace(equation, @"(\))([a-zA-Z])", "$1*$2");
equation = Regex.Replace(equation, @"(\))(\d)", "$1*$2");
// Handle expressions like (2-1)x or x(2-1)
equation = Regex.Replace(equation, @"(\d)([a-zA-Z])", "$1*$2"); // e.g., 2x -> 2*x
equation = Regex.Replace(equation, @"([a-zA-Z])(\d)", "$1*$2"); // e.g., x2 -> x*2
return equation;
}
private static string EvaluateTrigonometricFunctions(string equation, Dictionary<string, float> variables)
{
// Replace variables inside trigonometric functions
Regex trigRegex = new Regex(@"\b(sin|cos|tan)\(([^)]+)\)");
// Handle expressions like (2-1)x or x(2-1)
equation = Regex.Replace(equation, @"(\d)([a-zA-Z])", "$1*$2"); // e.g., 2x -> 2*x
equation = Regex.Replace(equation, @"([a-zA-Z])(\d)", "$1*$2"); // e.g., x2 -> x*2
equation = Regex.Replace(equation, @"(x)([a-zA-Z])", "$1*$2"); // e.g., 2x -> 2*x
equation = Regex.Replace(equation, @"([a-zA-Z])(x)", "$1*$2"); // e.g., x2 -> x*2
equation = Regex.Replace(equation, @"(\))(\d)", "$1*$2");
equation = Regex.Replace(equation, @"(\))([a-zA-Z])", "$1*$2");
equation = trigRegex.Replace(equation, match =>
{
string function = match.Groups[1].Value;
string innerExpression = match.Groups[2].Value;
// Replace variables in the inner expression
foreach (var variable in variables)
{
innerExpression = innerExpression.Replace(variable.Key, variable.Value.ToString());
}
//THIS IS WHERE YOU CAN CHOOSE EITHER DEGREES OR RADIANS, JUST DELETE MATH.F DEG2RAD IF YOU WANT IT TO BE RADIANS
if (ExpressionEvaluator.Evaluate(innerExpression, out float value))
{
switch (function)
{
case "sin":
if (value%180<0.01){
return 0.ToString();
}else{
return Mathf.Sin(value * Mathf.Deg2Rad).ToString();
}
case "cos":
if ((value+90)%180<0.01){
return 0.ToString();
}else{
return Mathf.Cos(value * Mathf.Deg2Rad).ToString();
}
case "tan":
return Mathf.Tan(value * Mathf.Deg2Rad).ToString();
default:
return match.Value; // Return original if function not recognized
}
}
else
{
Debug.LogError("Failed to evaluate expression inside trigonometric function: " + innerExpression);
return "NaN";
}
});
return equation;
}
private static string EvaluateNestedExpressions(string equation)
{
Regex nestedExpressionRegex = new Regex(@"\(([^()]+)\)");
while (nestedExpressionRegex.IsMatch(equation))
{
equation = nestedExpressionRegex.Replace(equation, match =>
{
string innerExpression = match.Groups[1].Value;
if (ExpressionEvaluator.Evaluate(innerExpression, out float innerResult))
{
return "["+innerResult.ToString()+"]";
}
else
{
Debug.LogError("Failed to evaluate nested expression: " + innerExpression);
return "NaN"; // Return NaN if the evaluation fails
}
});
}
return equation;
}
private static string FixNests(string equation)
{
equation = equation.Replace('[', '(');
equation = equation.Replace(']', ')');
return equation;
}
}