Mercurial > projects > dwt-addons
comparison dwtx/draw2d/FanRouter.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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.draw2d.FanRouter; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 | |
17 import dwtx.draw2d.geometry.Point; | |
18 import dwtx.draw2d.geometry.PointList; | |
19 import dwtx.draw2d.geometry.Ray; | |
20 import dwtx.draw2d.AutomaticRouter; | |
21 import dwtx.draw2d.PositionConstants; | |
22 | |
23 /** | |
24 * Automatic router that spreads its {@link Connection Connections} in a fan-like fashion | |
25 * upon collision. | |
26 */ | |
27 public class FanRouter | |
28 : AutomaticRouter | |
29 { | |
30 | |
31 private int separation = 10; | |
32 | |
33 /** | |
34 * Returns the separation in pixels between fanned connections. | |
35 * | |
36 * @return the separation | |
37 * @since 2.0 | |
38 */ | |
39 public int getSeparation() { | |
40 return separation; | |
41 } | |
42 | |
43 /** | |
44 * Modifies a given PointList that collides with some other PointList. The given | |
45 * <i>index</i> indicates that this it the i<sup>th</sup> PointList in a group of | |
46 * colliding points. | |
47 * | |
48 * @param points the colliding points | |
49 * @param index the index | |
50 */ | |
51 protected void handleCollision(PointList points, int index) { | |
52 Point start = points.getFirstPoint(); | |
53 Point end = points.getLastPoint(); | |
54 | |
55 if (start.opEquals(end)) | |
56 return; | |
57 | |
58 Point midPoint = new Point((end.x + start.x) / 2, (end.y + start.y) / 2); | |
59 int position = end.getPosition(start); | |
60 Ray ray; | |
61 if (position is PositionConstants.SOUTH || position is PositionConstants.EAST) | |
62 ray = new Ray(start, end); | |
63 else | |
64 ray = new Ray(end, start); | |
65 double length = ray.length(); | |
66 | |
67 double xSeparation = separation * ray.x / length; | |
68 double ySeparation = separation * ray.y / length; | |
69 | |
70 Point bendPoint; | |
71 | |
72 if (index % 2 is 0) { | |
73 bendPoint = new Point( | |
74 midPoint.x + (index / 2) * (-1 * ySeparation), | |
75 midPoint.y + (index / 2) * xSeparation); | |
76 } else { | |
77 bendPoint = new Point( | |
78 midPoint.x + (index / 2) * ySeparation, | |
79 midPoint.y + (index / 2) * (-1 * xSeparation)); | |
80 } | |
81 if (!bendPoint.opEquals(midPoint)) | |
82 points.insertPoint(bendPoint, 1); | |
83 } | |
84 | |
85 /** | |
86 * Sets the colliding {@link Connection Connection's} separation in pixels. | |
87 * | |
88 * @param value the separation | |
89 * @since 2.0 | |
90 */ | |
91 public void setSeparation(int value) { | |
92 separation = value; | |
93 } | |
94 | |
95 } |