Skip to content

Commit

Permalink
Add MinAngle, Fix Asin/Atan/Acos functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SnaveSutit committed Nov 2, 2023
1 parent df14efb commit e9f65b2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public int getRequiredArguments() {
@Override
public double _evaluate(Expr[] arguments, ExecutionContext ctx) {
double a = this.evaluateArgument(arguments, ctx, 0);
if (a > 1) {
if (Math.abs(a) > 1) {
return 0;
}
return Math.acos(a);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public int getRequiredArguments() {
@Override
public double _evaluate(Expr[] arguments, ExecutionContext ctx) {
double a = this.evaluateArgument(arguments, ctx, 0);
if (a > 1) {
if (Math.abs(a) > 1) {
return 0;
}
return Math.asin(a);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public int getRequiredArguments() {
@Override
public double _evaluate(Expr[] arguments, ExecutionContext ctx) {
double a = this.evaluateArgument(arguments, ctx, 0);
if (a > 1) {
if (Math.abs(a) > 1) {
return 0;
}
return Math.atan(a); }
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/eliotlash/molang/functions/utility/MinAngle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.eliotlash.molang.functions.utility;

import com.eliotlash.molang.ast.Expr;
import com.eliotlash.molang.functions.Function;
import com.eliotlash.molang.utils.Interpolations;
import com.eliotlash.molang.variables.ExecutionContext;

public class MinAngle extends Function {

public MinAngle(String name) {
super(name);
}

@Override
public int getRequiredArguments() {
return 3;
}

@Override
public double _evaluate(Expr[] arguments, ExecutionContext ctx) {
double result = this.evaluateArgument(arguments, ctx, 0);
// Clamp the result to -360 to 360, then add 360 to make it positive, then mod 360 to get the positive angle
result = ((result % 360) + 360) % 360;
// If the result is greater than 180, subtract 360 to get the negative angle
if (result > 179) result -= 360;
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public ExecutionContext(Evaluator evaluator) {
registerFunction("math", new Trunc("trunc"));
registerFunction("math", new Lerp("lerp"));
registerFunction("math", new LerpRotate("lerprotate"));
registerFunction("math", new MinAngle("min_angle"));
registerFunction("math", new Random("random"));
registerFunction("math", new RandomInteger("random_integer"));
registerFunction("math", new DiceRoll("dice_roll"));
Expand Down

0 comments on commit e9f65b2

Please sign in to comment.