/* * 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; /* Dieser Algorithmus funktioniert nur, wenn das gegebene Polygon konvex ist. Man konnte natuerlich in setPolygon() einen entsprechenden Check einbauen, aber dann koennte man nicht beobachten, welche Fehler der Algorithmus bei konkaven Polygonen macht. */ public class Konvex implements Algo { private int[] m, n, o; // fuer die Linie P[i] nach P[(i+1)mod npoints] synchronized public void setPolygon(Polygon P) { m = new int[P.npoints]; n = new int[P.npoints]; o = new int[P.npoints]; if (P.npoints <= 0) return; int bx, by; int cx = P.xpoints[P.npoints - 1]; int cy = P.ypoints[P.npoints - 1]; for (int i = 0; i < P.npoints; i++) { bx = P.xpoints[i]; by = P.ypoints[i]; m[i] = bx * cy - by * cx; n[i] = by - cy; o[i] = cx - bx; cx = bx; cy = by; } } public int isInside(int ax, int ay) { if (m.length <= 0) return (-1); int A = 0, B; for (int i = 0; i < m.length; i++) { B = m[i] + ax * n[i] + ay * o[i]; if (((B < 0) && (A > 0)) || ((B > 0) && (A < 0)) || (B == 0)) return (-1); A = B; } return (1); } }