Mercurial > projects > dwt-addons
comparison dwtx/draw2d/geometry/Geometry.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) 2005, 2008 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.geometry.Geometry; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 | |
17 /** | |
18 * A Utilities class for geometry operations. | |
19 * @author Pratik Shah | |
20 * @since 3.1 | |
21 */ | |
22 public class Geometry | |
23 { | |
24 | |
25 /** | |
26 * Determines whether the two line segments formed by the given coordinates intersect. If | |
27 * one of the two line segments starts or ends on the other line, then they are considered | |
28 * to be intersecting. | |
29 * | |
30 * @param ux x coordinate of starting point of line 1 | |
31 * @param uy y coordinate of starting point of line 1 | |
32 * @param vx x coordinate of ending point of line 1 | |
33 * @param vy y coordinate of endpoing point of line 1 | |
34 * @param sx x coordinate of the starting point of line 2 | |
35 * @param sy y coordinate of the starting point of line 2 | |
36 * @param tx x coordinate of the ending point of line 2 | |
37 * @param ty y coordinate of the ending point of line 2 | |
38 * @return <code>true</code> if the two line segments formed by the given coordinates | |
39 * cross | |
40 * @since 3.1 | |
41 */ | |
42 public static bool linesIntersect(int ux, int uy, int vx, int vy, | |
43 int sx, int sy, int tx, int ty) { | |
44 /* | |
45 * Given the segments: u-------v. s-------t. If s->t is inside the triangle u-v-s, | |
46 * then check whether the line u->u splits the line s->t. | |
47 */ | |
48 /* Values are casted to long to avoid integer overflows */ | |
49 long usX = cast(long) ux - sx; | |
50 long usY = cast(long) uy - sy; | |
51 long vsX = cast(long) vx - sx; | |
52 long vsY = cast(long) vy - sy; | |
53 long stX = cast(long) sx - tx; | |
54 long stY = cast(long) sy - ty; | |
55 if (productSign(cross(vsX, vsY, stX, stY), cross(stX, stY, usX, usY)) >= 0) { | |
56 long vuX = cast(long) vx - ux; | |
57 long vuY = cast(long) vy - uy; | |
58 long utX = cast(long) ux - tx; | |
59 long utY = cast(long) uy - ty; | |
60 return productSign(cross(-usX, -usY, vuX, vuY), cross(vuX, vuY, utX, utY)) <= 0; | |
61 } | |
62 return false; | |
63 } | |
64 | |
65 private static int productSign(long x, long y) { | |
66 if (x is 0 || y is 0) { | |
67 return 0; | |
68 } else if (x < 0 ^ y < 0) { | |
69 return -1; | |
70 } | |
71 return 1; | |
72 } | |
73 | |
74 private static long cross(long x1, long y1, long x2, long y2) { | |
75 return x1 * y2 - x2 * y1; | |
76 } | |
77 | |
78 } |