commit f948fcb240c29648c29931547d4766a229da22ea Author: Timo Ley Date: Thu May 18 16:14:03 2023 +0200 init diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..00a51af --- /dev/null +++ b/.gitattributes @@ -0,0 +1,6 @@ +# +# https://help.github.com/articles/dealing-with-line-endings/ +# +# These are explicitly windows files and should use crlf +*.bat text eol=crlf + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c0b04e6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.gradle +build +bin \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..a9c3eda --- /dev/null +++ b/build.gradle @@ -0,0 +1,16 @@ +plugins { + id 'application' +} + +repositories { + mavenCentral() +} + +dependencies { + +} + +application { + mainClass = 'ma2tetr.App' +} + diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..7454180 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..e750102 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..1b6c787 --- /dev/null +++ b/gradlew @@ -0,0 +1,234 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..161a482 --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'ma2tetr' diff --git a/src/main/java/ma2tetr/App.java b/src/main/java/ma2tetr/App.java new file mode 100644 index 0000000..d143df9 --- /dev/null +++ b/src/main/java/ma2tetr/App.java @@ -0,0 +1,39 @@ +package ma2tetr; + +import ma2tetr.api.ITetrahedronCoordCalculator; +import ma2tetr.impl.TetrahedronMovingCalculator; +import ma2tetr.impl.TetrahedronScalingCalculator; +import ma2tetr.model.Tetrahedron; + +public class App { + + public static void main(String[] args) { + double radius; + if (args.length != 2) { + throw new RuntimeException("Needs 2 arguments"); + } + try { + radius = Double.parseDouble(args[1]); + } catch (NumberFormatException e) { + throw new RuntimeException("Invalid radius: " + args[1]); + } + ITetrahedronCoordCalculator calc; + switch(args[0]) { + case "scaling": + calc = new TetrahedronScalingCalculator(); + break; + case "moving": + calc = new TetrahedronMovingCalculator(); + break; + default: + throw new RuntimeException("Invalid calculator: " + args[0]); + } + calc.setRadius(radius); + calc.calculate(); + Tetrahedron t = calc.getTetrahedron(); + System.out.println(t.getTop()); + System.out.println(t.getP1()); + System.out.println(t.getP2()); + System.out.println(t.getP3()); + } +} diff --git a/src/main/java/ma2tetr/api/ITetrahedronCoordCalculator.java b/src/main/java/ma2tetr/api/ITetrahedronCoordCalculator.java new file mode 100644 index 0000000..f8e4977 --- /dev/null +++ b/src/main/java/ma2tetr/api/ITetrahedronCoordCalculator.java @@ -0,0 +1,15 @@ +package ma2tetr.api; + +import ma2tetr.model.Tetrahedron; + +public interface ITetrahedronCoordCalculator { + + void setRadius(double radius); + + double getRadius(); + + void calculate(); + + Tetrahedron getTetrahedron(); + +} diff --git a/src/main/java/ma2tetr/impl/TetrahedronMovingCalculator.java b/src/main/java/ma2tetr/impl/TetrahedronMovingCalculator.java new file mode 100644 index 0000000..a656692 --- /dev/null +++ b/src/main/java/ma2tetr/impl/TetrahedronMovingCalculator.java @@ -0,0 +1,62 @@ +package ma2tetr.impl; + +import java.util.HashSet; +import java.util.Set; + +import ma2tetr.api.ITetrahedronCoordCalculator; +import ma2tetr.invariant.RadiusInvariant; +import ma2tetr.invariant.TetrahedronInvariant; +import ma2tetr.model.Coords3D; +import ma2tetr.model.Point3D; +import ma2tetr.model.Tetrahedron; +import ma2tetr.model.Vector3D; + +public class TetrahedronMovingCalculator implements ITetrahedronCoordCalculator { + + private double radius = 1.0; + private Tetrahedron tetrahedron = null; + + @Override + public void setRadius(double radius) { + this.radius = radius; + } + + @Override + public void calculate() { + Coords3D center = new Vector3D(0, 0, 0); + Point3D top = new Point3D(0, radius, 0); + TriangleBaseMover triangle = new TriangleBaseMover(radius); + triangle.move(-radius); + Set points = new HashSet<>(); + points.add(top); + points.add(triangle.getP1()); + points.add(triangle.getP2()); + points.add(triangle.getP3()); + RadiusInvariant rinv = new RadiusInvariant(points, center, radius); + TetrahedronInvariant tinv = new TetrahedronInvariant(top, triangle.getP1(), triangle.getP2(), triangle.getP3()); + double scaling = radius / 10; + while(true) { + triangle.move(scaling); + if (!rinv.isFulfilled()) { + throw new RuntimeException("Unexpected error"); + } else if (tinv.isFulfilled()) { + break; + } else if (tinv.firstDifference() < 0) { + triangle.move(-scaling); + scaling = scaling / 10; + } + } + tetrahedron = new Tetrahedron(top, triangle.getP1(), triangle.getP2(), triangle.getP3()); + } + + @Override + public Tetrahedron getTetrahedron() { + return this.tetrahedron; + } + + @Override + public double getRadius() { + return this.radius; + } + +} diff --git a/src/main/java/ma2tetr/impl/TetrahedronScalingCalculator.java b/src/main/java/ma2tetr/impl/TetrahedronScalingCalculator.java new file mode 100644 index 0000000..35a716b --- /dev/null +++ b/src/main/java/ma2tetr/impl/TetrahedronScalingCalculator.java @@ -0,0 +1,50 @@ +package ma2tetr.impl; + +import ma2tetr.api.ITetrahedronCoordCalculator; +import ma2tetr.invariant.RadiusInvariant; +import ma2tetr.invariant.TetrahedronInvariant; +import ma2tetr.model.Coords3D; +import ma2tetr.model.Tetrahedron; +import ma2tetr.model.Vector3D; + +public class TetrahedronScalingCalculator implements ITetrahedronCoordCalculator { + + private double radius = 1.0; + private Tetrahedron tetrahedron = null; + + @Override + public void setRadius(double radius) { + this.radius = radius; + } + + @Override + public void calculate() { + Coords3D center = new Vector3D(0, 0, 0); + tetrahedron = new Tetrahedron(0, radius, 0); + TetrahedronInvariant tinv = new TetrahedronInvariant(tetrahedron); + RadiusInvariant rinv = new RadiusInvariant(tetrahedron.getPointsSet(), center, radius); + double scaling = radius / 10; + while(true) { + tetrahedron.scale(scaling); + if (!tinv.isFulfilled()) { + throw new RuntimeException("Unexpected error"); + } else if (rinv.isFulfilled()) { + break; + } else if (rinv.firstDifference() < 0) { + tetrahedron.scale(-scaling); + scaling = scaling / 10; + } + } + } + + @Override + public Tetrahedron getTetrahedron() { + return this.tetrahedron; + } + + @Override + public double getRadius() { + return this.radius; + } + +} diff --git a/src/main/java/ma2tetr/impl/TriangleBaseMover.java b/src/main/java/ma2tetr/impl/TriangleBaseMover.java new file mode 100644 index 0000000..f2f68ec --- /dev/null +++ b/src/main/java/ma2tetr/impl/TriangleBaseMover.java @@ -0,0 +1,87 @@ +package ma2tetr.impl; + +import ma2tetr.model.EquilateralTriangleBuilder; +import ma2tetr.model.Point3D; +import ma2tetr.model.Vector3D; + +public class TriangleBaseMover { + + private Point3D p1; + private Point3D p2; + private Point3D p3; + + private Point3D center; + + private Vector3D v1; + private Vector3D v2; + private Vector3D v3; + + private Vector3D yVec = new Vector3D(0, 1, 0); + private Vector3D origin = new Vector3D(0, 0, 0); + + private double radius; + + public TriangleBaseMover(double radius) { + this.center = new Point3D(0, 0, 0); + this.radius = radius; + EquilateralTriangleBuilder builder = new EquilateralTriangleBuilder(center, 1, 0); + builder.build(); + p1 = builder.getP1(); + p2 = builder.getP2(); + p3 = builder.getP3(); + + v1 = Vector3D.createFromPoints(center, p1); + v2 = Vector3D.createFromPoints(center, p2); + v3 = Vector3D.createFromPoints(center, p3); + + scalePoints(radius); + } + + public void move(double yScale) { + double oldScaling = getScaling(); + movePoints(yScale); + double factor = calcNewScaling(oldScaling); + scalePoints(factor); + } + + private void scalePoints(double factor) { + p1.move(v1, factor); + p2.move(v2, factor); + p3.move(v3, factor); + } + + private void movePoints(double yScale) { + p1.move(yVec, yScale); + p2.move(yVec, yScale); + p3.move(yVec, yScale); + center.move(yVec, yScale); + } + + private double calcNewScaling(double oldScaling) { + double b = center.distance(origin); + double c = radius; + double a = Math.sqrt(Math.pow(c, 2) - Math.pow(b, 2)); + return a - oldScaling; + } + + private double getScaling() { + return center.distance(p1); + } + + public Point3D getP1() { + return p1; + } + + public Point3D getP2() { + return p2; + } + + public Point3D getP3() { + return p3; + } + + public Point3D getCenter() { + return center; + } + +} diff --git a/src/main/java/ma2tetr/invariant/AbstractInvariant.java b/src/main/java/ma2tetr/invariant/AbstractInvariant.java new file mode 100644 index 0000000..8e08847 --- /dev/null +++ b/src/main/java/ma2tetr/invariant/AbstractInvariant.java @@ -0,0 +1,17 @@ +package ma2tetr.invariant; + +public abstract class AbstractInvariant { + + private double accuracy = 0.00000000001; + + public abstract boolean isFulfilled(); + + public boolean checkEqual(double v1, double v2) { + return Math.abs(v1 - v2) <= accuracy; + } + + public void setAccuracy(double accuracy) { + this.accuracy = accuracy; + } + +} diff --git a/src/main/java/ma2tetr/invariant/EquilateralTriangleInvariant.java b/src/main/java/ma2tetr/invariant/EquilateralTriangleInvariant.java new file mode 100644 index 0000000..15eabf4 --- /dev/null +++ b/src/main/java/ma2tetr/invariant/EquilateralTriangleInvariant.java @@ -0,0 +1,37 @@ +package ma2tetr.invariant; + +import ma2tetr.model.Coords3D; + +public class EquilateralTriangleInvariant extends AbstractInvariant { + + private Coords3D p1; + private Coords3D p2; + private Coords3D p3; + + private double len12; + private double len23; + private double len13; + + public EquilateralTriangleInvariant(Coords3D p1, Coords3D p2, Coords3D p3) { + this.p1 = p1; + this.p2 = p2; + this.p3 = p3; + } + + private void recalcDistance() { + len12 = p1.distance(p2); + len23 = p2.distance(p3); + len13 = p1.distance(p3); + } + + @Override + public boolean isFulfilled() { + this.recalcDistance(); + return checkEqual(len12, len23) && checkEqual(len12, len13); + } + + public double getDistance() { + return this.isFulfilled() ? len12 : -1; + } + +} diff --git a/src/main/java/ma2tetr/invariant/RadiusInvariant.java b/src/main/java/ma2tetr/invariant/RadiusInvariant.java new file mode 100644 index 0000000..dfe3b56 --- /dev/null +++ b/src/main/java/ma2tetr/invariant/RadiusInvariant.java @@ -0,0 +1,40 @@ +package ma2tetr.invariant; + +import java.util.Set; + +import ma2tetr.model.Coords3D; + +public class RadiusInvariant extends AbstractInvariant { + + private Set points; + private Coords3D center; + private double radius; + + public RadiusInvariant(Set points, Coords3D center, double radius) { + this.points = points; + this.center = center; + this.radius = radius; + } + + @Override + public boolean isFulfilled() { + for (Coords3D point : points) { + double len = center.distance(point); + if (!checkEqual(len, radius)) { + return false; + } + } + return true; + } + + public double firstDifference() { + for (Coords3D point : points) { + double len = center.distance(point); + if (!checkEqual(len, radius)) { + return radius - len; + } + } + return 0.0; + } + +} diff --git a/src/main/java/ma2tetr/invariant/TetrahedronInvariant.java b/src/main/java/ma2tetr/invariant/TetrahedronInvariant.java new file mode 100644 index 0000000..1965d4a --- /dev/null +++ b/src/main/java/ma2tetr/invariant/TetrahedronInvariant.java @@ -0,0 +1,62 @@ +package ma2tetr.invariant; + +import ma2tetr.model.Coords3D; +import ma2tetr.model.Tetrahedron; + +public class TetrahedronInvariant extends AbstractInvariant { + + private Coords3D top; + private Coords3D p1; + private Coords3D p2; + private Coords3D p3; + + private double len1; + private double len2; + private double len3; + private double lenbase; + + private EquilateralTriangleInvariant base; + + public TetrahedronInvariant(Coords3D top, Coords3D p1, Coords3D p2, Coords3D p3) { + this.top = top; + this.p1 = p1; + this.p2 = p2; + this.p3 = p3; + this.base = new EquilateralTriangleInvariant(p1, p2, p3); + } + + public TetrahedronInvariant(Tetrahedron t) { + this(t.getTop(), t.getP1(), t.getP2(), t.getP3()); + } + + private void recalcDistance() { + len1 = top.distance(p1); + len2 = top.distance(p2); + len3 = top.distance(p3); + lenbase = base.getDistance(); + } + + public boolean isFulfilled() { + this.recalcDistance(); + return checkEqual(lenbase, len1) && checkEqual(lenbase, len2) && checkEqual(lenbase, len3); + } + + public double firstDifference() { + recalcDistance(); + if (!checkEqual(lenbase, len1)) { + return len1 - lenbase; + } else if (!checkEqual(lenbase, len2)) { + return len2 - lenbase; + } else if (!checkEqual(lenbase, len3)) { + return len3 - lenbase; + } + return 0; + } + + @Override + public void setAccuracy(double accuracy) { + base.setAccuracy(accuracy); + super.setAccuracy(accuracy); + } + +} diff --git a/src/main/java/ma2tetr/model/Coords3D.java b/src/main/java/ma2tetr/model/Coords3D.java new file mode 100644 index 0000000..35e5fb5 --- /dev/null +++ b/src/main/java/ma2tetr/model/Coords3D.java @@ -0,0 +1,39 @@ +package ma2tetr.model; + +public abstract class Coords3D { + + protected double x; + protected double y; + protected double z; + + public Coords3D(double x, double y, double z) { + this.x = x; + this.y = y; + this.z = z; + } + + public double getX() { + return x; + } + + public double getY() { + return y; + } + + public double getZ() { + return z; + } + + public double distance(Coords3D other) { + double xDist = other.x - this.x; + double yDist = other.y - this.y; + double zDist = other.z - this.z; + return Math.sqrt(Math.pow(xDist, 2) + Math.pow(yDist, 2) + Math.pow(zDist, 2)); + } + + @Override + public String toString() { + return "[x=" + x + ", y=" + y + ", z=" + z + "]"; + } + +} diff --git a/src/main/java/ma2tetr/model/EquilateralTriangleBuilder.java b/src/main/java/ma2tetr/model/EquilateralTriangleBuilder.java new file mode 100644 index 0000000..8ea834c --- /dev/null +++ b/src/main/java/ma2tetr/model/EquilateralTriangleBuilder.java @@ -0,0 +1,48 @@ +package ma2tetr.model; + +public class EquilateralTriangleBuilder { + + private Point3D p1; + private Point3D p2; + private Point3D p3; + + private Coords3D center; + private double radius; + + public EquilateralTriangleBuilder(Coords3D center, double radius, double rotation) { + this.center = center; + this.radius = radius; + } + + public void build() { + p1 = new Point3D(0, center.getY(), radius); + + double p2x = (p1.getX() * Math.cos((Math.PI * 2)/3)) - (p1.getZ() * Math.sin((Math.PI * 2)/3)); + double p2z = (p1.getX() * Math.sin((Math.PI * 2)/3)) + (p1.getZ() * Math.cos((Math.PI * 2)/3)); + p2 = new Point3D(p2x, center.getY(), p2z); + + double p3x = (p1.getX() * Math.cos((Math.PI * 4)/3)) - (p1.getZ() * Math.sin((Math.PI * 4)/3)); + double p3z = (p1.getX() * Math.sin((Math.PI * 4)/3)) + (p1.getZ() * Math.cos((Math.PI * 4)/3)); + p3 = new Point3D(p3x, center.getY(), p3z); + + p1.setX(p1.getX() + center.getX()); + p1.setZ(p1.getZ() + center.getZ()); + p2.setX(p2.getX() + center.getX()); + p2.setZ(p2.getZ() + center.getZ()); + p3.setX(p3.getX() + center.getX()); + p3.setZ(p3.getZ() + center.getZ()); + } + + public Point3D getP1() { + return p1; + } + + public Point3D getP2() { + return p2; + } + + public Point3D getP3() { + return p3; + } + +} diff --git a/src/main/java/ma2tetr/model/Point3D.java b/src/main/java/ma2tetr/model/Point3D.java new file mode 100644 index 0000000..4d1ac99 --- /dev/null +++ b/src/main/java/ma2tetr/model/Point3D.java @@ -0,0 +1,30 @@ +package ma2tetr.model; + +/** + * Represents a movable point in space + */ +public class Point3D extends Coords3D { + + public Point3D(double x, double y, double z) { + super(x, y, z); + } + + public void setX(double x) { + this.x = x; + } + + public void setY(double y) { + this.y = y; + } + + public void setZ(double z) { + this.z = z; + } + + public void move(Coords3D direction, double multiplier) { + this.x += multiplier * direction.getX(); + this.y += multiplier * direction.getY(); + this.z += multiplier * direction.getZ(); + } + +} diff --git a/src/main/java/ma2tetr/model/Tetrahedron.java b/src/main/java/ma2tetr/model/Tetrahedron.java new file mode 100644 index 0000000..e80925e --- /dev/null +++ b/src/main/java/ma2tetr/model/Tetrahedron.java @@ -0,0 +1,83 @@ +package ma2tetr.model; + +import java.util.HashSet; +import java.util.Set; + +public class Tetrahedron { + + private Point3D top; + private Point3D p1; + private Point3D p2; + private Point3D p3; + + private Vector3D v1; + 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); + builder.build(); + p1 = builder.getP1(); + p2 = builder.getP2(); + p3 = builder.getP3(); + + v1 = Vector3D.createFromPoints(top, p1); + v2 = Vector3D.createFromPoints(top, p2); + v3 = Vector3D.createFromPoints(top, p3); + + this.scale(-1); + } + + + + public Tetrahedron(Point3D top, Point3D p1, Point3D p2, Point3D p3) { + this.top = top; + this.p1 = p1; + this.p2 = p2; + this.p3 = p3; + + v1 = Vector3D.createFromPoints(top, p1); + v2 = Vector3D.createFromPoints(top, p2); + v3 = Vector3D.createFromPoints(top, p3); + } + + public Coords3D getTop() { + return top; + } + + public Coords3D getP1() { + return p1; + } + + public Coords3D getP2() { + return p2; + } + + public Coords3D getP3() { + return p3; + } + + public Set getPointsSet() { + Set set = new HashSet<>(); + set.add(p1); + set.add(p2); + set.add(p3); + set.add(top); + return set; + } + + public void scale(double factor) { + p1.move(v1, factor); + p2.move(v2, factor); + p3.move(v3, factor); + } + + public void move(Coords3D direction, double multiplier) { + top.move(direction, multiplier); + p1.move(direction, multiplier); + p2.move(direction, multiplier); + p3.move(direction, multiplier); + } + +} diff --git a/src/main/java/ma2tetr/model/TetrahedronBuilder.java b/src/main/java/ma2tetr/model/TetrahedronBuilder.java new file mode 100644 index 0000000..95cf3ca --- /dev/null +++ b/src/main/java/ma2tetr/model/TetrahedronBuilder.java @@ -0,0 +1,47 @@ +package ma2tetr.model; + +import ma2tetr.invariant.EquilateralTriangleInvariant; + +public class TetrahedronBuilder { + + private Point3D p1; + private Point3D p2; + private Point3D p3; + + private Point3D top; + + public TetrahedronBuilder(Point3D top) { + this.top = top; + } + + public void build() { + EquilateralTriangleBuilder trib = new EquilateralTriangleBuilder(top, 1, 0); + trib.build(); + p1 = trib.getP1(); + p2 = trib.getP2(); + p3 = trib.getP3(); + EquilateralTriangleInvariant inv = new EquilateralTriangleInvariant(p1, p2, p3); + if (!inv.isFulfilled()) { + throw new RuntimeException("Unexpected error"); + } + double c = inv.getDistance(); + double b = top.distance(p1); + double a = Math.sqrt(Math.pow(c, 2) - Math.pow(b, 2)); + p1.setY(p1.getY() - a); + p2.setY(p2.getY() - a); + p3.setY(p3.getY() - a); + } + + public Point3D getP1() { + return p1; + } + + public Point3D getP2() { + return p2; + } + + public Point3D getP3() { + return p3; + } + +} diff --git a/src/main/java/ma2tetr/model/Vector3D.java b/src/main/java/ma2tetr/model/Vector3D.java new file mode 100644 index 0000000..143ff5e --- /dev/null +++ b/src/main/java/ma2tetr/model/Vector3D.java @@ -0,0 +1,16 @@ +package ma2tetr.model; + +public class Vector3D extends Coords3D { + + public Vector3D(double x, double y, double z) { + super(x, y, z); + } + + public static Vector3D createFromPoints(Coords3D p1, Coords3D p2) { + double x = p2.getX() - p1.getX(); + double y = p2.getY() - p1.getY(); + double z = p2.getZ() - p1.getZ(); + return new Vector3D(x, y, z); + } + +}