Mercurial > projects > dwt-addons
comparison 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 |
comparison
equal
deleted
inserted
replaced
96:b492ba44e44d | 98:95307ad235d9 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2005 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Alex Selkov - Fix for Bug# 22701 | |
11 * Port to the D programming language: | |
12 * Frank Benoit <benoit@tionex.de> | |
13 *******************************************************************************/ | |
14 module dwtx.draw2d.Polygon; | |
15 | |
16 import dwt.dwthelper.utils; | |
17 import dwtx.dwtxhelper.Collection; | |
18 import dwtx.draw2d.Polyline; | |
19 import dwtx.draw2d.Graphics; | |
20 import dwtx.draw2d.IFigure; | |
21 | |
22 /** | |
23 * Renders a {@link dwtx.draw2d.geometry.PointList} as a polygonal shape. | |
24 * This class is similar to Polyline, except the PointList is closed and can be filled in | |
25 * as a solid shape. | |
26 * @see Polyline | |
27 */ | |
28 public class Polygon | |
29 : Polyline | |
30 { | |
31 | |
32 /** | |
33 * Returns whether the point (x,y) is contained inside this polygon. | |
34 * @param x the X coordinate | |
35 * @param y the Y coordinate | |
36 * @return whether the point (x,y) is contained in this polygon | |
37 */ | |
38 public bool containsPoint(int x, int y) { | |
39 if (!getBounds().contains(x, y)) | |
40 return false; | |
41 | |
42 bool isOdd = false; | |
43 int[] pointsxy = getPoints().toIntArray(); | |
44 int n = pointsxy.length; | |
45 if (n > 3) { //If there are at least 2 Points (4 ints) | |
46 int x1, y1; | |
47 int x0 = pointsxy[n - 2]; | |
48 int y0 = pointsxy[n - 1]; | |
49 | |
50 for (int i = 0; i < n; x0 = x1, y0 = y1) { | |
51 x1 = pointsxy[i++]; | |
52 y1 = pointsxy[i++]; | |
53 | |
54 if (y0 <= y && y < y1 | |
55 && crossProduct(x1, y1, x0, y0, x, y) > 0) | |
56 isOdd = !isOdd; | |
57 if (y1 <= y && y < y0 | |
58 && crossProduct(x0, y0, x1, y1, x, y) > 0) | |
59 isOdd = !isOdd; | |
60 } | |
61 if (isOdd) | |
62 return true; | |
63 } | |
64 | |
65 List children = getChildren(); | |
66 for (int i = 0; i < children.size(); i++) | |
67 if ((cast(IFigure) children.get(i)).containsPoint(x, y)) | |
68 return true; | |
69 | |
70 return false; | |
71 } | |
72 | |
73 private int crossProduct(int ax, int ay, int bx, int by, int cx, int cy) { | |
74 return (ax - cx) * (by - cy) - (ay - cy) * (bx - cx); | |
75 } | |
76 | |
77 /** | |
78 * Fill the Polygon with the background color set by <i>g</i>. | |
79 * | |
80 * @param g the Graphics object | |
81 * @since 2.0 | |
82 */ | |
83 protected void fillShape(Graphics g) { | |
84 g.fillPolygon(getPoints()); | |
85 } | |
86 | |
87 /** | |
88 * Draw the outline of the Polygon. | |
89 * | |
90 * @param g the Graphics object | |
91 * @since 2.0 | |
92 */ | |
93 protected void outlineShape(Graphics g) { | |
94 g.drawPolygon(getPoints()); | |
95 } | |
96 | |
97 } |