diff dwtx/draw2d/Polygon.d @ 98:95307ad235d9

Added Draw2d code, still work in progress
author Frank Benoit <benoit@tionex.de>
date Sun, 03 Aug 2008 00:52:14 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtx/draw2d/Polygon.d	Sun Aug 03 00:52:14 2008 +0200
@@ -0,0 +1,97 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2005 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *     Alex Selkov - Fix for Bug# 22701
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+module dwtx.draw2d.Polygon;
+
+import dwt.dwthelper.utils;
+import dwtx.dwtxhelper.Collection;
+import dwtx.draw2d.Polyline;
+import dwtx.draw2d.Graphics;
+import dwtx.draw2d.IFigure;
+
+/**
+ * Renders a {@link dwtx.draw2d.geometry.PointList} as a polygonal shape.
+ * This class is similar to Polyline, except the PointList is closed and can be filled in
+ * as a solid shape.
+ * @see Polyline
+ */
+public class Polygon
+    : Polyline
+{
+
+/**
+ * Returns whether the point (x,y) is contained inside this polygon.
+ * @param x the X coordinate
+ * @param y the Y coordinate
+ * @return whether the point (x,y) is contained in this polygon
+ */
+public bool containsPoint(int x, int y) {
+    if (!getBounds().contains(x, y))
+        return false;
+
+    bool isOdd = false;
+    int[] pointsxy = getPoints().toIntArray();
+    int n = pointsxy.length;
+    if (n > 3) { //If there are at least 2 Points (4 ints)
+        int x1, y1;
+        int x0 = pointsxy[n - 2];
+        int y0 = pointsxy[n - 1];
+
+        for (int i = 0; i < n; x0 = x1, y0 = y1) {
+            x1 = pointsxy[i++];
+            y1 = pointsxy[i++];
+
+            if (y0 <= y && y < y1
+              && crossProduct(x1, y1, x0, y0, x, y) > 0)
+                isOdd = !isOdd;
+            if (y1 <= y && y < y0
+              && crossProduct(x0, y0, x1, y1, x, y) > 0)
+                isOdd = !isOdd;
+        }
+        if (isOdd)
+            return true;
+    }
+
+    List children = getChildren();
+    for (int i = 0; i < children.size(); i++)
+        if ((cast(IFigure) children.get(i)).containsPoint(x, y))
+            return true;
+
+    return false;
+}
+
+private int crossProduct(int ax, int ay, int bx, int by, int cx, int cy) {
+    return (ax - cx) * (by - cy) - (ay - cy) * (bx - cx);
+}
+
+/**
+ * Fill the Polygon with the background color set by <i>g</i>.
+ *
+ * @param g the Graphics object
+ * @since 2.0
+ */
+protected void fillShape(Graphics g) {
+    g.fillPolygon(getPoints());
+}
+
+/**
+ * Draw the outline of the Polygon.
+ *
+ * @param g the Graphics object
+ * @since 2.0
+ */
+protected void outlineShape(Graphics g) {
+    g.drawPolygon(getPoints());
+}
+
+}