Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
dotsPlacementIssue
2D Point Triangulation
============================
Algorithm solves the problem of the points placement according to the length which is specified in the matrix.
This project is dedicated to positioning (triangulating) points in a two-dimensional
Cartesian system, where the origin is always fixed at `(0, 0)`. The algorithms
reconstruct coordinates using a matrix of pairwise Manhattan distances between
points.

Implantation details
Implementation details
============================
The main idea of this algorithm is to try every possible combination of calculated coordinates until the correct answer(s) is found.
This is a recursive algorithm with backtracking approach.
Expand Down Expand Up @@ -65,7 +68,7 @@ According to the matrix we can say the distance between two dots.<br> Thus, the
<ul>
<li> From point [0] to point [0] = 0 (distance to itself is zero -> obvious)</li>
<li> From point [0] to point [1] = 2 </li>
<li> Frm point [0] to point [2] = 2
<li> From point [0] to point [2] = 2
<br>...
<li> 0->9 = 8 </li>
<li> 1->0 = 2 </li>
Expand All @@ -80,3 +83,22 @@ If we visualise the result using Cartesian coordinate system we will get the pic
<br> ![result](./ex.jpg) <br>
<br> _Note_: Blue color was used to indicate rules of calculating the distance.
<br> __Pay attention__: The distance is not weight of the line which _directly_ connects two dots (lines may be angular).

Running tests
============================
The Python implementation includes a small unit test suite. Execute the following command from the project root:

```
PYTHONPATH=python_impl python3 -m unittest
```

The Java implementation uses JUnit 5. Compile the sources and run the tests using the console launcher:

```
javac -d java_impl/build/classes \
$(find java_impl/src -path '*visual*' -prune -o -name '*.java' -print)
javac -d java_impl/build/test-classes -cp java_impl/build/classes:/usr/share/java/junit-platform-console-standalone.jar \
$(find java_impl/tests -name '*.java')
java -jar /usr/share/java/junit-platform-console-standalone.jar \
-cp java_impl/build/classes:java_impl/build/test-classes --scan-class-path
```
36 changes: 36 additions & 0 deletions java_impl/tests/SimplePointsFinderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.gitlab.saylenty;

import com.gitlab.saylenty.entity.Point;
import com.gitlab.saylenty.strategy.PointsFinderStrategy;
import com.gitlab.saylenty.strategy.SimplePointsFinder;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public class SimplePointsFinderTest {
@Test
public void testSimpleTriangulation() {
int[][] matrix = {
{0, 1, 2},
{1, 0, 3},
{2, 3, 0}
};
PointsFinderStrategy solver = new SimplePointsFinder();
List<Point[]> solutions = solver.findSolution(matrix);
assertFalse(solutions.isEmpty(), "Expected at least one solution");
Point[] coords = solutions.get(0);
assertEquals(new Point(0, 0), coords[0]);
int n = matrix.length;
assertEquals(n, coords.length);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int dist = PointsFinderStrategy.absDistance(
coords[i].getX(), coords[i].getY(),
coords[j].getX(), coords[j].getY());
assertEquals(matrix[i][j], dist);
}
}
}
}
5 changes: 5 additions & 0 deletions java_impl/tests/com/sun/istack/internal/NotNull.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.sun.istack.internal;
import java.lang.annotation.*;
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.TYPE})
public @interface NotNull {}
23 changes: 23 additions & 0 deletions python_impl/tests/test_algorythm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import unittest
import algorythm

class TestAlgorythm(unittest.TestCase):
def test_triangulation(self):
matrix = (
(0, 1, 2),
(1, 0, 3),
(2, 3, 0),
)
algorythm.matrix = matrix
coords = algorythm.wr()
self.assertEqual(coords[0], (0, 0))
n = len(matrix)
self.assertEqual(len(coords), n)
for i in range(n):
for j in range(n):
expected = matrix[i][j]
dist = abs(coords[i][0] - coords[j][0]) + abs(coords[i][1] - coords[j][1])
self.assertEqual(dist, expected)

if __name__ == '__main__':
unittest.main()