Skip to content

Commit

Permalink
collinearity test for vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
miho committed Apr 19, 2019
1 parent d3e40f4 commit a633eee
Show file tree
Hide file tree
Showing 22 changed files with 173 additions and 24 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@
/.nb-gradle/
VVecMath.iml

/.classpath
/.vscode
/.project
/.settings

/bin

9 changes: 4 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ apply plugin: 'java'
apply from: 'gradle/publishing.gradle'


task wrapper(type: Wrapper) {
gradleVersion = '4.9'
distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
wrapper {
gradleVersion = '5.4'
}

apply plugin: 'java'
Expand All @@ -30,7 +29,7 @@ repositories {
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
}


Expand All @@ -43,7 +42,7 @@ license {
groovy = 'SLASHSTAR_STYLE'
fxml = 'XML_STYLE'
}
ext.year = '2018'
ext.year = '2019'
ext.author = 'Michael Hoffer <[email protected]>'
exclude '**/*.svg'
}
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ APP_NAME="Gradle"
APP_BASE_NAME=`basename "$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=""
DEFAULT_JVM_OPTS='"-Xmx64m"'

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
Expand Down
2 changes: 1 addition & 1 deletion gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@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=
set DEFAULT_JVM_OPTS="-Xmx64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/eu/mihosoft/vvecmath/Matrix4d.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Michael Hoffer <[email protected]>. All rights reserved.
* Copyright 2017-2019 Michael Hoffer <[email protected]>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Michael Hoffer <[email protected]>. All rights reserved.
* Copyright 2017-2019 Michael Hoffer <[email protected]>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
Expand Down Expand Up @@ -32,7 +32,6 @@
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of Michael Hoffer <[email protected]>.
*/

package eu.mihosoft.vvecmath;

import static eu.mihosoft.vvecmath.StoredVector3d.getStructSize;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/eu/mihosoft/vvecmath/ModifiableVector3d.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Michael Hoffer <[email protected]>. All rights reserved.
* Copyright 2017-2019 Michael Hoffer <[email protected]>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Michael Hoffer <[email protected]>. All rights reserved.
* Copyright 2017-2019 Michael Hoffer <[email protected]>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/eu/mihosoft/vvecmath/Plane.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Michael Hoffer <[email protected]>. All rights reserved.
* Copyright 2017-2019 Michael Hoffer <[email protected]>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/eu/mihosoft/vvecmath/Spline.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Michael Hoffer <[email protected]>. All rights reserved.
* Copyright 2017-2019 Michael Hoffer <[email protected]>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/eu/mihosoft/vvecmath/Spline3D.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Michael Hoffer <[email protected]>. All rights reserved.
* Copyright 2017-2019 Michael Hoffer <[email protected]>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/eu/mihosoft/vvecmath/StoredVector3d.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Michael Hoffer <[email protected]>. All rights reserved.
* Copyright 2017-2019 Michael Hoffer <[email protected]>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/eu/mihosoft/vvecmath/StoredVector3dImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Michael Hoffer <[email protected]>. All rights reserved.
* Copyright 2017-2019 Michael Hoffer <[email protected]>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/eu/mihosoft/vvecmath/Transform.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Michael Hoffer <[email protected]>. All rights reserved.
* Copyright 2017-2019 Michael Hoffer <[email protected]>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/eu/mihosoft/vvecmath/Vector3d-iface.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Michael Hoffer <[email protected]>. All rights reserved.
* Copyright 2017-2019 Michael Hoffer <[email protected]>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
Expand Down
72 changes: 71 additions & 1 deletion src/main/java/eu/mihosoft/vvecmath/Vector3d.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Michael Hoffer <[email protected]>. All rights reserved.
* Copyright 2017-2019 Michael Hoffer <[email protected]>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
Expand Down Expand Up @@ -636,4 +636,74 @@ public default Vector3d project(Vector3d v) {
return this.times(pScale);
}

/**
* Indicates whether the two given points are collinear with this
* vector/point.
*
* @param p2 second point
* @param p3 third point
*
* @return {@code true} if all three points are collinear;
* {@code false} otherwise
*/
default boolean collinear(Vector3d p2, Vector3d p3) {

// The points p1, p2, p3 are collinear (are on the same line segment)
// if and only if the largest of the lenghts of
//
// a = P1P2,
// b = P1P2
// c = P2P3
//
// is equal to the sum of the other two.
//
// Explanation:
//
// If p1, p2 and p3 are on the same line then the point in the 'middle'
// cuts the line segment into two smaller pieces. That is the sum of the
// lengths of the smaller pieces is equal to the largest one.
//
// That is, the following expression determines whether the three points
// are collinear
//
// boolean collinear = largest > (second + third)

double a = this.distance(p2);
double b = this.distance(p3);
double c = p2.distance(p3);

double largest;
double second;
double third;

if (a > b && a > c) {
// a is largest
largest = a;
second = b;
third = c;
} else if (b > a && b > c) {
// b is largest
largest = b;
second = a;
third = c;
} else if (c > a && c > b) {
// c is largest
largest = c;
second = a;
third = b;
} else {
// lengths are not distinct.
//
// there are two possibilities:
//
// a: they are vertices of a equilateral triangle -> false
// b: they are zero, i.e., all points are identical -> true
//
return a == 0 && b == 0 && c == 0;
}

return Math.abs(largest - (second + third)) < Plane.TOL;

}

}
2 changes: 1 addition & 1 deletion src/main/java/eu/mihosoft/vvecmath/Vector3dImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Michael Hoffer <[email protected]>. All rights reserved.
* Copyright 2017-2019 Michael Hoffer <[email protected]>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/eu/mihosoft/vvecmath/VectorUtilInternal.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Michael Hoffer <[email protected]>. All rights reserved.
* Copyright 2017-2019 Michael Hoffer <[email protected]>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/eu/mihosoft/vvecmath/Vectors3d.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Michael Hoffer <[email protected]>. All rights reserved.
* Copyright 2017-2019 Michael Hoffer <[email protected]>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
Expand Down
74 changes: 74 additions & 0 deletions src/test/java/eu/mihosoft/vvecmath/Vector3dTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2017-2019 Michael Hoffer <[email protected]>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* If you use this software for scientific research then please cite the following publication(s):
*
* M. Hoffer, C. Poliwoda, & G. Wittum. (2013). Visual reflection library:
* a framework for declarative GUI programming on the Java platform.
* Computing and Visualization in Science, 2013, 16(4),
* 181–192. http://doi.org/10.1007/s00791-014-0230-y
*
* THIS SOFTWARE IS PROVIDED BY Michael Hoffer <[email protected]> "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Michael Hoffer <[email protected]> OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of Michael Hoffer <[email protected]>.
*/
package eu.mihosoft.vvecmath;

import org.junit.Test;

import junit.framework.Assert;

public class Vector3dTest {

@Test
public void collinearityTest() {
{
Vector3d p1 = Vector3d.xyz(1,1,1);
Vector3d p2 = p1.times(5);
Vector3d p3 = p1.times(10);

Assert.assertTrue("p1, p2, p3 must be collinear", p1.collinear(p2, p3));
}
{
Vector3d p1 = Vector3d.xyz(1,1,1);
Vector3d p2 = p1.times(5,5,4);
Vector3d p3 = p1.times(10);

Assert.assertTrue("p1, p2, p3 must not be collinear", !p1.collinear(p2, p3));
}
{
Vector3d p1 = Vector3d.xyz(10,10,10);
Vector3d p2 = Vector3d.xyz(-1,-1,-1);
Vector3d p3 = p1.times(5);

Assert.assertTrue("p1, p2, p3 must be collinear", p1.collinear(p2, p3));
}
{
Vector3d p1 = Vector3d.xyz(10,20,10);
Vector3d p2 = p1.clone();
Vector3d p3 = p2.clone();

Assert.assertTrue("p1, p2, p3 must be collinear", p1.collinear(p2, p3));
}
}
}

0 comments on commit a633eee

Please sign in to comment.