Compare commits

...

3 Commits

2 changed files with 61 additions and 9 deletions

2
.idea/compiler.xml generated
View File

@@ -6,7 +6,7 @@
<sourceOutputDir name="target/generated-sources/annotations" /> <sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" /> <sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" /> <outputRelativeToContentRoot value="true" />
<module name="terrain-utilities" /> <module name="teren-utils" />
</profile> </profile>
</annotationProcessing> </annotationProcessing>
</component> </component>

View File

@@ -8,6 +8,40 @@ public class GeomUtils {
private static final Logger logger = LoggerFactory.getLogger(GeomUtils.class); private static final Logger logger = LoggerFactory.getLogger(GeomUtils.class);
/**
* Wyznacza indeks sektora położenia badanego punktu o względnych współrzędnych (punkt odniesienia (0, 0)).
*
* @param dx względna współrzędna x badanego punktu
* @param dy względna współrzędna y badanego punktu
* @return indeks sektora z zakresu {0, 1, 2, ..., 7}
*/
public static int sector(int dx, int dy) {
if (0 <= dx && dx < dy) {
return 0;
}
if (0 < dy && dy <= dx) {
return 1;
}
if (-dx < dy && dy <= 0) {
return 2;
}
if (0 < dx && dx <= -dy) {
return 3;
}
if (dy < dx && dx <= 0) {
return 4;
}
if (dx <= dy && dy < 0) {
return 5;
}
if (0 <= dy && dy < -dx) {
return 6;
}
// (-dy <= dx && dx < 0)
return 7;
}
/** /**
* <p> Wyznacznik macierzy (kwadratowej stopnia 3) * <p> Wyznacznik macierzy (kwadratowej stopnia 3)
* [[p.x, p.y, 1], * [[p.x, p.y, 1],
@@ -23,7 +57,7 @@ public class GeomUtils {
* det3 mniejsze od 0, gdy punkt lezy po prawej stronie wektora, * det3 mniejsze od 0, gdy punkt lezy po prawej stronie wektora,
*/ */
public static long det3(Coord.Grid p, Coord.Grid q, Coord.Grid r) { public static long det3(Coord.Grid p, Coord.Grid q, Coord.Grid r) {
return p.x * (q.y - r.y) + q.x * (r.y - p.y) + r.x * (p.y - q.y); return (long) p.x * (q.y - r.y) + (long) q.x * (r.y - p.y) + (long) r.x * (p.y - q.y);
} }
/** /**
@@ -44,7 +78,7 @@ public class GeomUtils {
* det3 mniejsze od 0, gdy punkt lezy po prawej stronie wektora, * det3 mniejsze od 0, gdy punkt lezy po prawej stronie wektora,
*/ */
public static long det3(int px, int py, int qx, int qy, int rx, int ry) { public static long det3(int px, int py, int qx, int qy, int rx, int ry) {
return px * (qy - ry) + qx * (ry - py) + rx * (py - qy); return (long) px * (qy - ry) + (long) qx * (ry - py) + (long) rx * (py - qy);
} }
/** /**
@@ -228,7 +262,7 @@ public class GeomUtils {
/** /**
* Wyznacza ograniczenia dolne i górne współrzędnych: xMin, yMin, xMax, yMax. * Wyznacza ograniczenia dolne i górne współrzędnych: xMin, yMin, xMax, yMax.
* *
* @param vertices * @param vertices wierzchołki
* @return vertices bounds: 0 - xMin, 1 - yMin, 2 - xMax, 3 - yMax * @return vertices bounds: 0 - xMin, 1 - yMin, 2 - xMax, 3 - yMax
*/ */
public static int[] getBounds(Coord.Grid[] vertices) { public static int[] getBounds(Coord.Grid[] vertices) {
@@ -248,10 +282,10 @@ public class GeomUtils {
/** /**
* Funkcja bada zawieranie się punktu w ramach wielokąta. * Funkcja bada zawieranie się punktu w ramach wielokąta.
* *
* @param polygon * @param polygon współrzędne wierzchołków wielokąta
* @param x * @param x współrzedna x badanego punktu
* @param y * @param y współrzedna y badanego punktu
* @return * @return true, jeśli testowany punkt należy do wielokąta
*/ */
public static boolean insidePolygon(Coord.Grid[] polygon, int x, int y) { public static boolean insidePolygon(Coord.Grid[] polygon, int x, int y) {
// Sprawdzenie należenia punktu (x, y) do dowolnej krawędzi wielokąta. // Sprawdzenie należenia punktu (x, y) do dowolnej krawędzi wielokąta.
@@ -274,7 +308,7 @@ public class GeomUtils {
for (int i = 0; i < polygon.length; i++) { for (int i = 0; i < polygon.length; i++) {
int x1 = polygon[i].x; int x1 = polygon[i].x;
int y1 = polygon[i].y; int y1 = polygon[i].y;
int ii = (i + 1) % polygon.length; final int ii = (i + 1) % polygon.length;
int x2 = polygon[ii].x; int x2 = polygon[ii].x;
int y2 = polygon[ii].y; int y2 = polygon[ii].y;
if (y < y1 != y < y2) { if (y < y1 != y < y2) {
@@ -882,6 +916,24 @@ public class GeomUtils {
// ===================================================================== // =====================================================================
public static void main(String[] args) { public static void main(String[] args) {
int se = sector(0, 0);
int sek = sector(0, 9);
sek = sector(2, 6);
sek = sector(4, 4);
sek = sector(5, 2);
sek = sector(7, 0);
sek = sector(5, -2);
sek = sector(5, -5);
sek = sector(2, -5);
sek = sector(0, -5);
sek = sector(-4, -6);
sek = sector(-3, -3);
sek = sector(-5, -1);
sek = sector(-4, 0);
sek = sector(-5, 3);
sek = sector(-4, 4);
sek = sector(-1, 4);
// Coord.Grid[] segments = new Coord.Grid[4]; // Coord.Grid[] segments = new Coord.Grid[4];
// segments[0] = new Coord.Grid(1, 1); // segments[0] = new Coord.Grid(1, 1);