/* * Copyright (C) 1998 Ralf Wiebicke * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package de.rw7.polygon; import java.awt.Polygon; /* BUG: Der Algorithmus setzt vorraus, dass, der Testpunkt nicht auf einer Hoehe mit dem ersten Punkt des Polygons liegt. Das ist im dieser Implementation immer der zuletzt hinzugefuegte Punkt. In diesem Falle koennte man einfach beim naechten Punkt anfangen. Das ist aber hier noch nicht implementiert. Den Fehler kann man leicht reproduzieren, z.B. mit folgendem Dreieck: P1 P2 P0 BUGFIX (17.01.98): Dank eines netten Hinweises von Karl Eilebrecht (Karl.Eilebrecht@t-online.de) konnte ein Bug beseitigt werden, der bei dem folgenden Polygon auch die Verlaengerung der Kante P0-P1 bis auf Hoehe von P3 als "Auf der Kante markiert hat: (P0 und P1 auf gleicher Hoehe) P3 P1 P0 P2 */ public class Wiebi implements Algo { Polygon P; synchronized public void setPolygon(Polygon P) { this.P = P; } public int isInside(int x, int y) // Strahl nach rechts { if (P.npoints <= 0) return (-1); int ax = P.xpoints[P.npoints - 1]; // A ist der letzte Punkt vor B, der nicht .. int ay = P.ypoints[P.npoints - 1]; // .. mit dem Testpunkt auf gleicher Hoehe liegt. int bx, by; // B ist der aktuelle Punkt int lx = ax; // L ist IMMER der letzte Punkt vor B (nur x ist von Belang) int zaehl = 0; boolean ignore = false; int zx; for (int i = 0; i < P.npoints; i++) { bx = P.xpoints[i]; by = P.ypoints[i]; if (!ignore) { if ((by == y) && (bx >= x)) { if (bx == x) return (0); ignore = true; } else { if ((ay < y) == (y < by)) { if (x < Math.min(ax, bx)) zaehl++; else if (x > Math.max(ax, bx)); else if (x < (zx = ((bx - ax) * (y - ay) / (by - ay)) + ax)) zaehl++; else if (zx == x) return (0); } ax = bx; ay = by; } } else { if (by == y) { if (((lx < x) == (x < bx)) || (bx == x)) return (0); } else { if ((ay < y) == (y < by)) zaehl++; ignore = false; ax = bx; ay = by; } } lx = bx; } if ((zaehl & 1) > 0) return (1); else return (-1); } }