comparison dwtx/draw2d/graph/Segment.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) 2004, 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.graph.Segment;
14
15 import dwt.dwthelper.utils;
16 import tango.text.convert.Format;
17
18 import dwtx.draw2d.geometry.Geometry;
19 import dwtx.draw2d.geometry.Point;
20 import dwtx.draw2d.graph.Vertex;
21
22 /**
23 * A Segment representation for the ShortestPathRouting. A segment is a line between
24 * two vertices.
25 *
26 * This class is for internal use only
27 * @author Whitney Sorenson
28 * @since 3.0
29 */
30 class Segment {
31
32 Vertex start, end;
33
34 /**
35 * Creates a segment between the given start and end points.
36 * @param start the start vertex
37 * @param end the end vertex
38 */
39 this(Vertex start, Vertex end) {
40 this.start = start;
41 this.end = end;
42 }
43
44 /**
45 * Returns the cosine of the made between this segment and the given segment
46 * @param otherSegment the other segment
47 * @return cosine value (not arc-cos)
48 */
49 double cosine(Segment otherSegment) {
50 double cos = (((start.x - end.x) * (otherSegment.end.x - otherSegment.start.x))
51 + ((start.y - end.y) * (otherSegment.end.y - otherSegment.start.y)))
52 / (getLength() * otherSegment.getLength());
53 double sin = (((start.x - end.x) * (otherSegment.end.y - otherSegment.start.y))
54 - ((start.y - end.y) * (otherSegment.end.x - otherSegment.start.x)));
55 if (sin < 0.0)
56 return (1 + cos);
57
58 return -(1 + cos);
59 }
60
61 /**
62 * Returns the cross product of this segment and the given segment
63 * @param otherSegment the other segment
64 * @return the cross product
65 */
66 long crossProduct(Segment otherSegment) {
67 return (((start.x - end.x) * (otherSegment.end.y - end.y))
68 - ((start.y - end.y) * (otherSegment.end.x - end.x)));
69 }
70
71 private double getLength() {
72 return (end.getDistance(start));
73 }
74
75 /**
76 * Returns a number that represents the sign of the slope of this segment. It does
77 * not return the actual slope.
78 * @return number representing sign of the slope
79 */
80 double getSlope() {
81 if (end.x - start.x >= 0)
82 return (end.y - start.y);
83 else
84 return -(end.y - start.y);
85 }
86
87 /**
88 * Returns true if the given segment intersects this segment.
89 * @param sx start x
90 * @param sy start y
91 * @param tx end x
92 * @param ty end y
93 * @return true if the segments intersect
94 */
95 bool intersects(int sx, int sy, int tx, int ty) {
96 return Geometry.linesIntersect(start.x, start.y, end.x, end.y, sx, sy, tx, ty);
97 }
98
99 /**
100 * Return true if the segment represented by the points intersects this segment.
101 * @param s start point
102 * @param t end point
103 * @return true if the segments intersect
104 */
105 bool intersects(Point s, Point t) {
106 return intersects(s.x, s.y, t.x, t.y);
107 }
108
109 /**
110 * @see java.lang.Object#toString()
111 */
112 public String toString() {
113 return Format( "{}---{}", start, end ); //$NON-NLS-1$
114 }
115
116 }