comparison dwtx/draw2d/geometry/Ray.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.geometry.Ray;
14
15 import dwt.dwthelper.utils;
16 import tango.text.convert.Format;
17
18 import dwtx.draw2d.geometry.Point;
19
20
21 /**
22 * Represents a 2-dimensional directional Vector, or Ray. {@link java.util.Vector} is
23 * commonly imported, so the name Ray was chosen.
24 */
25 public final class Ray {
26
27 /** the X value */
28 public int x;
29 /** the Y value*/
30 public int y;
31
32 /**
33 * Constructs a Ray &lt;0, 0&gt; with no direction and magnitude.
34 * @since 2.0
35 */
36 public this() { }
37
38 /**
39 * Constructs a Ray pointed in the specified direction.
40 *
41 * @param x X value.
42 * @param y Y value.
43 * @since 2.0
44 */
45 public this(int x, int y) {
46 this.x = x;
47 this.y = y;
48 }
49
50 /**
51 * Constructs a Ray pointed in the direction specified by a Point.
52 * @param p the Point
53 * @since 2.0
54 */
55 public this(Point p) {
56 x = p.x; y = p.y;
57 }
58
59 /**
60 * Constructs a Ray representing the direction and magnitude between to provided Points.
61 * @param start Strarting Point
62 * @param end End Point
63 * @since 2.0
64 */
65 public this(Point start, Point end) {
66 x = end.x - start.x;
67 y = end.y - start.y;
68 }
69
70 /**
71 * Constructs a Ray representing the difference between two provided Rays.
72 * @param start The start Ray
73 * @param end The end Ray
74 * @since 2.0
75 */
76 public this(Ray start, Ray end) {
77 x = end.x - start.x;
78 y = end.y - start.y;
79 }
80
81 /**
82 * Calculates the magnitude of the cross product of this Ray with another.
83 * Represents the amount by which two Rays are directionally different.
84 * Parallel Rays return a value of 0.
85 * @param r Ray being compared
86 * @return The assimilarity
87 * @see #similarity(Ray)
88 * @since 2.0
89 */
90 public int assimilarity(Ray r) {
91 return Math.abs(x * r.y - y * r.x);
92 }
93
94 /**
95 * Calculates the dot product of this Ray with another.
96 * @param r the Ray used to perform the dot product
97 * @return The dot product
98 * @since 2.0
99 */
100 public int dotProduct(Ray r) {
101 return x * r.x + y * r.y;
102 }
103
104 /**
105 * @see java.lang.Object#equals(Object)
106 */
107 public override int opEquals(Object obj) {
108 if (obj is this)
109 return true;
110 if ( auto r = cast(Ray)obj ) {
111 return x is r.x && y is r.y;
112 }
113 return false;
114 }
115
116 /**
117 * Creates a new Ray which is the sum of this Ray with another.
118 * @param r Ray to be added with this Ray
119 * @return a new Ray
120 * @since 2.0
121 */
122 public Ray getAdded(Ray r) {
123 return new Ray(r.x + x, r.y + y);
124 }
125
126 /**
127 * Creates a new Ray which represents the average of this Ray with another.
128 * @param r Ray to calculate the average.
129 * @return a new Ray
130 * @since 2.0
131 */
132 public Ray getAveraged(Ray r) {
133 return new Ray ((x + r.x) / 2, (y + r.y) / 2);
134 }
135
136 /**
137 * Creates a new Ray which represents this Ray scaled by the amount provided.
138 * @param s Value providing the amount to scale.
139 * @return a new Ray
140 * @since 2.0
141 */
142 public Ray getScaled(int s) {
143 return new Ray(x * s, y * s);
144 }
145
146 /**
147 * @see java.lang.Object#toHash()
148 */
149 public override hash_t toHash() {
150 return (x * y) ^ (x + y);
151 }
152
153 /**
154 * Returns true if this Ray has a non-zero horizontal comonent.
155 * @return true if this Ray has a non-zero horizontal comonent
156 * @since 2.0
157 */
158 public bool isHorizontal() {
159 return x !is 0;
160 }
161
162 /**
163 * Returns the length of this Ray.
164 * @return Length of this Ray
165 * @since 2.0
166 */
167 public double length() {
168 return Math.sqrt(cast(real)dotProduct(this));
169 }
170
171 /**
172 * Calculates the similarity of this Ray with another.
173 * Similarity is defined as the absolute value of the dotProduct()
174 * @param r Ray being tested for similarity
175 * @return the Similarity
176 * @see #assimilarity(Ray)
177 * @since 2.0
178 */
179 public int similarity(Ray r) {
180 return Math.abs(dotProduct(r));
181 }
182
183 /**
184 * @return a String representation
185 */
186 public String toString() {
187 return Format("({}, {})", x, y );//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
188 }
189
190 }