feat: add accuracy argument

This commit is contained in:
Timo Ley 2023-05-21 13:14:20 +02:00
parent cf21f34a1a
commit e2469b1e31
6 changed files with 41 additions and 14 deletions

View file

@ -9,17 +9,13 @@ public class App {
public static void main(String[] args) {
double radius;
if (args.length != 2) {
System.out.println("Required arguments: <calculator> <radius>");
if (args.length != 2 && args.length != 3) {
System.out.println("Arguments: <calculator> <radius> [<accuracy>]");
System.out.println(" - calculator: either 'scaling' or 'moving'");
System.out.println(" - radius: the sphere radius as a number");
System.out.println(" - accuracy: the accuracy for double comparison");
return;
}
try {
radius = Double.parseDouble(args[1]);
} catch (NumberFormatException e) {
throw new RuntimeException("Invalid radius: " + args[1]);
}
ITetrahedronCoordCalculator calc;
switch(args[0]) {
case "scaling":
@ -31,6 +27,14 @@ public class App {
default:
throw new RuntimeException("Invalid calculator: " + args[0]);
}
try {
radius = Double.parseDouble(args[1]);
if (args.length == 3) {
calc.setAccuracy(Double.parseDouble(args[2]));
}
} catch (NumberFormatException e) {
throw new RuntimeException("Invalid radius: " + args[1]);
}
calc.setRadius(radius);
calc.calculate();
Tetrahedron t = calc.getTetrahedron();

View file

@ -16,5 +16,7 @@ public interface ITetrahedronCoordCalculator {
Tetrahedron getTetrahedron();
List<IterationState> getStateLog();
void setAccuracy(double accuracy);
}

View file

@ -19,6 +19,7 @@ public class TetrahedronMovingCalculator implements ITetrahedronCoordCalculator
private double radius = 1.0;
private Tetrahedron tetrahedron = null;
private List<IterationState> log = new ArrayList<>();
private double accuracy = 0.00000000001;
@Override
public void setRadius(double radius) {
@ -38,13 +39,15 @@ public class TetrahedronMovingCalculator implements ITetrahedronCoordCalculator
points.add(triangle.getP3());
StateLogger logger = new StateLogger(center, points, log);
RadiusInvariant rinv = new RadiusInvariant(points, center, radius);
rinv.setAccuracy(accuracy);
TetrahedronInvariant tinv = new TetrahedronInvariant(top, triangle.getP1(), triangle.getP2(), triangle.getP3());
tinv.setAccuracy(accuracy);
double scaling = radius / 10;
while(true) {
triangle.move(scaling);
if (!rinv.isFulfilled()) {
throw new RuntimeException("Unexpected error");
} else if (tinv.isFulfilled()) {
} else if (tinv.isFulfilled() || scaling <= accuracy) {
break;
} else if (tinv.firstDifference() < 0) {
triangle.move(-scaling);
@ -69,5 +72,10 @@ public class TetrahedronMovingCalculator implements ITetrahedronCoordCalculator
public List<IterationState> getStateLog() {
return log;
}
@Override
public void setAccuracy(double accuracy) {
this.accuracy = accuracy;
}
}

View file

@ -8,6 +8,7 @@ import ma2tetr.invariant.RadiusInvariant;
import ma2tetr.invariant.TetrahedronInvariant;
import ma2tetr.model.Coords3D;
import ma2tetr.model.IterationState;
import ma2tetr.model.Point3D;
import ma2tetr.model.Tetrahedron;
import ma2tetr.model.Vector3D;
@ -16,6 +17,7 @@ public class TetrahedronScalingCalculator implements ITetrahedronCoordCalculator
private double radius = 1.0;
private Tetrahedron tetrahedron = null;
private List<IterationState> log = new ArrayList<>();
private double accuracy = 0.00000000001;
@Override
public void setRadius(double radius) {
@ -25,16 +27,18 @@ public class TetrahedronScalingCalculator implements ITetrahedronCoordCalculator
@Override
public void calculate() {
Coords3D center = new Vector3D(0, 0, 0);
tetrahedron = new Tetrahedron(0, radius, 0);
tetrahedron = new Tetrahedron(new Point3D(0, radius, 0), accuracy);
StateLogger logger = new StateLogger(center, tetrahedron.getPointsSet(), log);
TetrahedronInvariant tinv = new TetrahedronInvariant(tetrahedron);
tinv.setAccuracy(accuracy);
RadiusInvariant rinv = new RadiusInvariant(tetrahedron.getPointsSet(), center, radius);
rinv.setAccuracy(accuracy);
double scaling = radius / 10;
while(true) {
tetrahedron.scale(scaling);
if (!tinv.isFulfilled()) {
throw new RuntimeException("Unexpected error");
} else if (rinv.isFulfilled()) {
} else if (rinv.isFulfilled() || scaling < accuracy) {
break;
} else if (rinv.firstDifference() < 0) {
tetrahedron.scale(-scaling);
@ -59,4 +63,9 @@ public class TetrahedronScalingCalculator implements ITetrahedronCoordCalculator
return log;
}
@Override
public void setAccuracy(double accuracy) {
this.accuracy = accuracy;
}
}

View file

@ -14,9 +14,9 @@ public class Tetrahedron {
private Vector3D v2;
private Vector3D v3;
public Tetrahedron(double x, double y, double z) {
top = new Point3D(x, y, z);
TetrahedronBuilder builder = new TetrahedronBuilder(top);
public Tetrahedron(Point3D top, double accuracy) {
this.top = top;
TetrahedronBuilder builder = new TetrahedronBuilder(top, accuracy);
builder.build();
p1 = builder.getP1();
p2 = builder.getP2();

View file

@ -10,8 +10,11 @@ public class TetrahedronBuilder {
private Point3D top;
public TetrahedronBuilder(Point3D top) {
private double accuracy;
public TetrahedronBuilder(Point3D top, double accuracy) {
this.top = top;
this.accuracy = accuracy;
}
public void build() {
@ -21,6 +24,7 @@ public class TetrahedronBuilder {
p2 = trib.getP2();
p3 = trib.getP3();
EquilateralTriangleInvariant inv = new EquilateralTriangleInvariant(p1, p2, p3);
inv.setAccuracy(accuracy);
if (!inv.isFulfilled()) {
throw new RuntimeException("Unexpected error");
}