Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 63a38f32370cf6b72cf665df57f030896344344c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*****************************************************************************
 * Copyright (c) 2014 CEA LIST.
 *   
 * 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:
 * 
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 * 
 *  
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common.geometry;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PrecisionPoint;

/**
 * 
 * This class is used to manipulate segment
 */
public class Segment {


	/**
	 * the start point of the segment
	 */
	private final PrecisionPoint start;

	/**
	 * the end point of the segment
	 */
	private final PrecisionPoint end;

	/**
	 * 
	 * Constructor.
	 * 
	 * @param start
	 *        the start point of the segment
	 * @param end
	 *        the end point of the segment
	 */
	public Segment(final PrecisionPoint start, final PrecisionPoint end) {
		this.start = start.getPreciseCopy();
		this.end = end.getPreciseCopy();
	}

	/**
	 * 
	 * Constructor.
	 * 
	 * @param start
	 *        the start point of the segment
	 * @param end
	 *        the end point of the segment
	 */
	public Segment(final Point start, final Point end) {
		this.start = new PrecisionPoint(start);
		this.end = new PrecisionPoint(end);
	}

	/**
	 * 
	 * @param segment
	 *        a segment
	 * @return
	 *         <code>true</code> if the segment has a common point with this one
	 */
	public boolean hasIntersection(final Segment segment) {
		return getIntersection(segment) != null;
	}

	/**
	 * 
	 * @return
	 *         the start point of the segment
	 */
	public final PrecisionPoint getStartPoint() {
		return this.start;
	}

	/**
	 * 
	 * @return
	 *         the end point of the segment
	 */
	public final PrecisionPoint getEndPoint() {
		return this.end;
	}

	//find line intersection in awt
	//	static Point get_line_intersection(Line2D.Double pLine1, Line2D.Double pLine2) {
	//		Point result = null;
	//
	//		double s1_x = pLine1.x2 - pLine1.x1;
	//		double s1_y = pLine1.y2 - pLine1.y1;
	//		double s2_x = pLine2.x2 - pLine2.x1;
	//		double s2_y = pLine2.y2 - pLine2.y1;
	//
	//		double s = (-s1_y * (pLine1.x1 - pLine2.x1) + s1_x * (pLine1.y1 - pLine2.y1)) / (-s2_x * s1_y + s1_x * s2_y);
	//		double t = (s2_x * (pLine1.y1 - pLine2.y1) - s2_y * (pLine1.x1 - pLine2.x1)) / (-s2_x * s1_y + s1_x * s2_y);
	//
	//		if(s >= 0 && s <= 1 && t >= 0 && t <= 1) {
	//			// Collision detected
	//			result = new Point((int)(pLine1.x1 + (t * s1_x)), (int)(pLine1.y1 + (t * s1_y)));
	//		} // end if
	//
	//		return result;
	//	}

	/**
	 * 
	 * @param segment
	 *        a segment
	 * @return
	 *         the intersection point if exists or <code>null</code>
	 */
	public PrecisionPoint getIntersection(final Segment segment) {
		double s1_x = segment.getEndPoint().preciseX() - segment.getStartPoint().preciseX();
		double s1_y = segment.getEndPoint().preciseY() - segment.getStartPoint().preciseY();
		double s2_x = getEndPoint().preciseX() - getStartPoint().preciseX();
		double s2_y = getEndPoint().preciseY() - getStartPoint().preciseY();
		double s = (-s1_y * (segment.getStartPoint().preciseX() - getStartPoint().preciseX()) + s1_x * (segment.getStartPoint().preciseY() - getStartPoint().preciseY())) / (-s2_x * s1_y + s1_x * s2_y);
		double t = (s2_x * (segment.getStartPoint().preciseY() - getStartPoint().preciseY()) - s2_y * (segment.getStartPoint().preciseX() - getStartPoint().preciseX())) / (-s2_x * s1_y + s1_x * s2_y);

		PrecisionPoint result = null;
		if(s >= 0 && s <= 1 && t >= 0 && t <= 1) {
			// Collision detected
			result = new PrecisionPoint((int)(segment.getStartPoint().preciseX() + (t * s1_x)), (int)(segment.getStartPoint().preciseY() + (t * s1_y)));
		} // end if

		return result;
	}

}

Back to the top