Project

General

Profile

Download (3.26 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2010 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
 *******************************************************************************/
11
package org.eclipse.draw2d.graph;
12

    
13
import org.eclipse.draw2d.geometry.Geometry;
14
import org.eclipse.draw2d.geometry.Point;
15

    
16
/**
17
 * A Segment representation for the ShortestPathRouting. A segment is a line
18
 * between two vertices.
19
 * 
20
 * This class is for internal use only
21
 * 
22
 * @author Whitney Sorenson
23
 * @since 3.0
24
 */
25
class Segment {
26

    
27
	Vertex start, end;
28

    
29
	/**
30
	 * Creates a segment between the given start and end points.
31
	 * 
32
	 * @param start
33
	 *            the start vertex
34
	 * @param end
35
	 *            the end vertex
36
	 */
37
	Segment(Vertex start, Vertex end) {
38
		this.start = start;
39
		this.end = end;
40
	}
41

    
42
	/**
43
	 * Returns the cosine of the made between this segment and the given segment
44
	 * 
45
	 * @param otherSegment
46
	 *            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)) + ((start.y - end.y) * (otherSegment.end.y - otherSegment.start.y)))
51
				/ (getLength() * otherSegment.getLength());
52
		double sin = (((start.x - end.x) * (otherSegment.end.y - otherSegment.start.y)) - ((start.y - end.y) * (otherSegment.end.x - otherSegment.start.x)));
53
		if (sin < 0.0)
54
			return (1 + cos);
55

    
56
		return -(1 + cos);
57
	}
58

    
59
	/**
60
	 * Returns the cross product of this segment and the given segment
61
	 * 
62
	 * @param otherSegment
63
	 *            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)) - ((start.y - end.y) * (otherSegment.end.x - end.x)));
68
	}
69

    
70
	private double getLength() {
71
		return (end.getDistance(start));
72
	}
73

    
74
	/**
75
	 * Returns a number that represents the sign of the slope of this segment.
76
	 * It does not return the actual slope.
77
	 * 
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
	 * 
90
	 * @param sx
91
	 *            start x
92
	 * @param sy
93
	 *            start y
94
	 * @param tx
95
	 *            end x
96
	 * @param ty
97
	 *            end y
98
	 * @return true if the segments intersect
99
	 */
100
	boolean intersects(int sx, int sy, int tx, int ty) {
101
		return Geometry.linesIntersect(start.x, start.y, end.x, end.y, sx, sy,
102
				tx, ty);
103
	}
104

    
105
	/**
106
	 * Return true if the segment represented by the points intersects this
107
	 * segment.
108
	 * 
109
	 * @param s
110
	 *            start point
111
	 * @param t
112
	 *            end point
113
	 * @return true if the segments intersect
114
	 */
115
	boolean intersects(Point s, Point t) {
116
		return intersects(s.x, s.y, t.x, t.y);
117
	}
118

    
119
	/**
120
	 * @see java.lang.Object#toString()
121
	 */
122
	public String toString() {
123
		return start + "---" + end; //$NON-NLS-1$
124
	}
125

    
126
}
(37-37/49)