Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 166050 Details for
Bug 310397
Draw2D Euclidean Geometry (Ray, Straight) should support double precision.
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
Patch demonstrating a possible refactoring of Ray/Straight
310397.txt (text/plain), 34.47 KB, created by
Alexander Nyßen
on 2010-04-26 03:17:57 EDT
(
hide
)
Description:
Patch demonstrating a possible refactoring of Ray/Straight
Filename:
MIME Type:
Creator:
Alexander Nyßen
Created:
2010-04-26 03:17:57 EDT
Size:
34.47 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.draw2d >Index: META-INF/MANIFEST.MF >=================================================================== >RCS file: /cvsroot/tools/org.eclipse.gef/plugins/org.eclipse.draw2d/META-INF/MANIFEST.MF,v >retrieving revision 1.22 >diff -u -r1.22 MANIFEST.MF >--- META-INF/MANIFEST.MF 8 Jul 2009 20:24:42 -0000 1.22 >+++ META-INF/MANIFEST.MF 26 Apr 2010 07:15:37 -0000 >@@ -7,8 +7,9 @@ > Bundle-Localization: plugin > Import-Package: com.ibm.icu.text;version="[3.8.1,5.0.0)" > Export-Package: org.eclipse.draw2d, >- org.eclipse.draw2d.graph, > org.eclipse.draw2d.geometry, >+ org.eclipse.draw2d.geometry.euclidean, >+ org.eclipse.draw2d.graph, > org.eclipse.draw2d.images, > org.eclipse.draw2d.internal;x-internal:=true, > org.eclipse.draw2d.parts, >Index: src/org/eclipse/draw2d/geometry/Ray.java >=================================================================== >RCS file: /cvsroot/tools/org.eclipse.gef/plugins/org.eclipse.draw2d/src/org/eclipse/draw2d/geometry/Ray.java,v >retrieving revision 1.12 >diff -u -r1.12 Ray.java >--- src/org/eclipse/draw2d/geometry/Ray.java 18 Feb 2010 03:34:14 -0000 1.12 >+++ src/org/eclipse/draw2d/geometry/Ray.java 26 Apr 2010 07:15:37 -0000 >@@ -5,233 +5,222 @@ > * which accompanies this distribution, and is available at > * http://www.eclipse.org/legal/epl-v10.html > * >- * Contributors: >- * IBM Corporation - initial API and implementation >- * Research Group Software Construction, >- * RWTH Aachen University, Germany - Contribution for Bugzilla 245182 > *******************************************************************************/ > package org.eclipse.draw2d.geometry; > > /** >- * Represents a 2-dimensional directional Vector, or Ray. {@link java.util.Vector} is >- * commonly imported, so the name Ray was chosen. >- */ >-public final class Ray { >- >-/** the X value */ >-public int x; >-/** the Y value*/ >-public int y; >- >-/** >- * Constructs a Ray <0, 0> with no direction and magnitude. >- * @since 2.0 >- */ >-public Ray() { } >- >-/** >- * Constructs a Ray pointed in the specified direction. >+ * Represents a 2-dimensional directional Vector, or Ray. >+ * {@link java.util.Vector} is commonly imported, so the name Ray was chosen. > * >- * @param x X value. >- * @param y Y value. >- * @since 2.0 >- */ >-public Ray(int x, int y) { >- this.x = x; >- this.y = y; >-} >- >-/** >- * Constructs a Ray pointed in the direction specified by a Point. >- * @param p the Point >- * @since 2.0 >- */ >-public Ray(Point p) { >- x = p.x; y = p.y; >-} >- >-/** >- * Constructs a Ray representing the direction and magnitude between to provided Points. >- * @param start Strarting Point >- * @param end End Point >- * @since 2.0 >+ * @deprecated Use {@link Vector} instead, which offers double precision instead >+ * of integer precision. > */ >-public Ray(Point start, Point end) { >- x = end.x - start.x; >- y = end.y - start.y; >-} >+public final class Ray { > >-/** >- * Constructs a Ray representing the difference between two provided Rays. >- * @param start The start Ray >- * @param end The end Ray >- * @since 2.0 >- */ >-public Ray(Ray start, Ray end) { >- x = end.x - start.x; >- y = end.y - start.y; >-} >+ /** the X value */ >+ public int x; >+ /** the Y value */ >+ public int y; >+ >+ /** >+ * Constructs a Ray <0, 0> with no direction and magnitude. >+ * >+ * @since 2.0 >+ */ >+ public Ray() { >+ } > >-/** >- * Calculates the magnitude of the cross product of this Ray with another. >- * Represents the amount by which two Rays are directionally different. >- * Parallel Rays return a value of 0. >- * @param r Ray being compared >- * @return The assimilarity >- * @see #similarity(Ray) >- * @since 2.0 >- */ >-public int assimilarity(Ray r) { >- return Math.abs(x * r.y - y * r.x); >-} >+ /** >+ * Constructs a Ray pointed in the specified direction. >+ * >+ * @param x >+ * X value. >+ * @param y >+ * Y value. >+ * @since 2.0 >+ */ >+ public Ray(int x, int y) { >+ this.x = x; >+ this.y = y; >+ } > >-/** >- * Calculates the dot product of this Ray with another. >- * @param r the Ray used to perform the dot product >- * @return The dot product >- * @since 2.0 >- */ >-public int dotProduct(Ray r) { >- return x * r.x + y * r.y; >-} >+ /** >+ * Constructs a Ray pointed in the direction specified by a Point. >+ * >+ * @param p >+ * the Point >+ * @since 2.0 >+ */ >+ public Ray(Point p) { >+ x = p.x; >+ y = p.y; >+ } > >-/** >- * Calculates the dot product of this Ray with another. >- * @param r the Ray used to perform the dot product >- * @return The dot product as <code>long</code> to avoid possible integer overflow >- * @since 3.4.1 >- */ >-long dotProductL(Ray r) { >- return (long) x * r.x + (long) y * r.y; >-} >+ /** >+ * Constructs a Ray representing the direction and magnitude between to >+ * provided Points. >+ * >+ * @param start >+ * Strarting Point >+ * @param end >+ * End Point >+ * @since 2.0 >+ */ >+ public Ray(Point start, Point end) { >+ x = end.x - start.x; >+ y = end.y - start.y; >+ } > >-/** >- * @see java.lang.Object#equals(Object) >- */ >-public boolean equals(Object obj) { >- if (obj == this) >- return true; >- if (obj instanceof Ray) { >- Ray r = (Ray)obj; >- return x == r.x && y == r.y; >+ /** >+ * Constructs a Ray representing the difference between two provided Rays. >+ * >+ * @param start >+ * The start Ray >+ * @param end >+ * The end Ray >+ * @since 2.0 >+ */ >+ public Ray(Ray start, Ray end) { >+ x = end.x - start.x; >+ y = end.y - start.y; > } >- return false; >-} > >-/** >- * Creates a new Ray which is the sum of this Ray with another. >- * @param r Ray to be added with this Ray >- * @return a new Ray >- * @since 2.0 >- */ >-public Ray getAdded(Ray r) { >- return new Ray(r.x + x, r.y + y); >-} >+ /** >+ * Calculates the magnitude of the cross product of this Ray with another. >+ * Represents the amount by which two Rays are directionally different. >+ * Parallel Rays return a value of 0. >+ * >+ * @param r >+ * Ray being compared >+ * @return The assimilarity >+ * @see #similarity(Ray) >+ * @since 2.0 >+ */ >+ public int assimilarity(Ray r) { >+ return Math.abs(x * r.y - y * r.x); >+ } > >-/** >- * Creates a new Ray which is the difference of this Ray with the provided Ray. >- * >- * @param q >- * Ray to be subtracted from this Ray >- * @return a new Ray >- * @since 3.6 >- */ >-public Ray getSubtracted(Ray q) { >- return new Ray(x - q.x, y - q.y); >-} >+ /** >+ * Calculates the dot product of this Ray with another. >+ * >+ * @param r >+ * the Ray used to perform the dot product >+ * @return The dot product >+ * @since 2.0 >+ */ >+ public int dotProduct(Ray r) { >+ return x * r.x + y * r.y; >+ } > >-/** >- * Returns the angle between this Ray and the provided Ray. >- * @param q Ray to calculate the angle. >- * @return the angle between the two Rays. >- * @since 3.6 >- */ >-public double getAngle(Ray q) { >- double cosAlpha = dotProduct(q) / (length() * q.length()); >- return Math.acos(cosAlpha); >-} >+ /** >+ * Calculates the dot product of this Ray with another. >+ * >+ * @param r >+ * the Ray used to perform the dot product >+ * @return The dot product as <code>long</code> to avoid possible integer >+ * overflow >+ * @since 3.4.1 >+ */ >+ long dotProductL(Ray r) { >+ return (long) x * r.x + (long) y * r.y; >+ } > >-/** >- * Creates a new Ray which represents the average of this Ray with another. >- * @param r Ray to calculate the average. >- * @return a new Ray >- * @since 2.0 >- */ >-public Ray getAveraged(Ray r) { >- return new Ray ((x + r.x) / 2, (y + r.y) / 2); >-} >+ /** >+ * @see java.lang.Object#equals(Object) >+ */ >+ public boolean equals(Object obj) { >+ if (obj == this) >+ return true; >+ if (obj instanceof Ray) { >+ Ray r = (Ray) obj; >+ return x == r.x && y == r.y; >+ } >+ return false; >+ } > >-/** >- * Creates a new Ray which represents this Ray scaled by the amount provided. >- * @param s Value providing the amount to scale. >- * @return a new Ray >- * @since 2.0 >- */ >-public Ray getScaled(int s) { >- return new Ray(x * s, y * s); >-} >+ /** >+ * Creates a new Ray which is the sum of this Ray with another. >+ * >+ * @param r >+ * Ray to be added with this Ray >+ * @return a new Ray >+ * @since 2.0 >+ */ >+ public Ray getAdded(Ray r) { >+ return new Ray(r.x + x, r.y + y); >+ } > >-/** >- * Returns the orthogonal complement of this Ray, which is defined to be >- * (-y, x). >- * >- * @return the orthogonal complement of this Ray >- * @since 3.6 >- */ >-public Ray getOrthogonalComplement() { >- return new Ray(-y, x); >-} >+ /** >+ * Creates a new Ray which represents the average of this Ray with another. >+ * >+ * @param r >+ * Ray to calculate the average. >+ * @return a new Ray >+ * @since 2.0 >+ */ >+ public Ray getAveraged(Ray r) { >+ return new Ray((x + r.x) / 2, (y + r.y) / 2); >+ } > >-/** >- * @see java.lang.Object#hashCode() >- */ >-public int hashCode() { >- return (x * y) ^ (x + y); >-} >+ /** >+ * Creates a new Ray which represents this Ray scaled by the amount >+ * provided. >+ * >+ * @param s >+ * Value providing the amount to scale. >+ * @return a new Ray >+ * @since 2.0 >+ */ >+ public Ray getScaled(int s) { >+ return new Ray(x * s, y * s); >+ } > >-/** >- * Returns true if this Ray has a non-zero horizontal comonent. >- * @return true if this Ray has a non-zero horizontal comonent >- * @since 2.0 >- */ >-public boolean isHorizontal() { >- return x != 0; >-} >+ /** >+ * @see java.lang.Object#hashCode() >+ */ >+ public int hashCode() { >+ return (x * y) ^ (x + y); >+ } > >-/** >- * Returns the length of this Ray. >- * @return Length of this Ray >- * @since 2.0 >- */ >-public double length() { >- return Math.sqrt(dotProductL(this)); >-} >+ /** >+ * Returns true if this Ray has a non-zero horizontal comonent. >+ * >+ * @return true if this Ray has a non-zero horizontal comonent >+ * @since 2.0 >+ */ >+ public boolean isHorizontal() { >+ return x != 0; >+ } > >-/** >- * Calculates the similarity of this Ray with another. >- * Similarity is defined as the absolute value of the dotProduct() >- * @param r Ray being tested for similarity >- * @return the Similarity >- * @see #assimilarity(Ray) >- * @since 2.0 >- */ >-public int similarity(Ray r) { >- return Math.abs(dotProduct(r)); >-} >+ /** >+ * Returns the length of this Ray. >+ * >+ * @return Length of this Ray >+ * @since 2.0 >+ */ >+ public double length() { >+ return Math.sqrt(dotProductL(this)); >+ } > >-/** >- * @return a String representation >- */ >-public String toString() { >- return "(" + x + "," + y + ")";//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$ >-} >+ /** >+ * Calculates the similarity of this Ray with another. Similarity is defined >+ * as the absolute value of the dotProduct() >+ * >+ * @param r >+ * Ray being tested for similarity >+ * @return the Similarity >+ * @see #assimilarity(Ray) >+ * @since 2.0 >+ */ >+ public int similarity(Ray r) { >+ return Math.abs(dotProduct(r)); >+ } > >-/** >- * @return A Point representation >- * @since 3.6 >- */ >-public Point toPoint(){ >- return new Point(x,y); >-} >+ /** >+ * @return a String representation >+ */ >+ public String toString() { >+ return "(" + x + "," + y + ")";//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$ >+ } > > } >Index: src/org/eclipse/draw2d/geometry/Straight.java >=================================================================== >RCS file: src/org/eclipse/draw2d/geometry/Straight.java >diff -N src/org/eclipse/draw2d/geometry/Straight.java >--- src/org/eclipse/draw2d/geometry/Straight.java 18 Feb 2010 03:34:14 -0000 1.1 >+++ /dev/null 1 Jan 1970 00:00:00 -0000 >@@ -1,180 +0,0 @@ >-/******************************************************************************* >- * Copyright (c) 2010 IBM Corporation and others. >- * 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: >- * Research Group Software Construction, >- * RWTH Aachen University, Germany - initial API and implementation >- */ >-package org.eclipse.draw2d.geometry; >- >-/** >- * Represents a straight line within a 2-dimensional space. Together with Ray, >- * this allows computations defined within Euclidean geometry. >- * >- * @author Alexander Nyssen >- * @since 3.6 >- */ >-public class Straight { >- >- private Ray p; // Ray indicating the position vector of the straight >- private Ray a; // Ray representing the direction vector of this straight >- >- /** >- * Constructs a new Straight with the given position and direction. >- * >- * @param position >- * @param direction >- */ >- public Straight(Ray position, Ray direction) { >- this.p = position; >- this.a = direction; >- } >- >- /** >- * Constructs a new Straight between the two given Points. >- * >- * @param point1 >- * @param point2 >- */ >- public Straight(Point point1, Point point2) { >- this(new Ray(point1), new Ray(point1, point2)); >- } >- >- /** >- * Returns the position of this Straight. >- * >- * @return A Ray indicating the position of this Straight. >- */ >- public Ray getPosition() { >- return p; >- } >- >- /** >- * Returns the direction of this Straight. >- * >- * @return A Ray indicating the position of this Straight. >- */ >- public Ray getDirection() { >- return a; >- } >- >- /** >- * Checks whether this Straight and the provided one have an intersection >- * point. >- * >- * @param g2 >- * The Straight to use for calculations. It may not be equal to >- * this Straight. >- * @return true if the two Straights intersect, false otherwise. >- */ >- public boolean intersects(Straight g2) { >- // check if there is an intersection point >- return a.dotProduct(g2.getDirection().getOrthogonalComplement()) != 0; >- } >- >- /** >- * Computes the intersection point of this Straight and the provided one, if it exists. >- * >- * @param g2 >- * The Straight to use for calculations. It may not be equal to >- * this Straight. >- * @return A Ray pointing to the intersection point, if it exists, null >- * if no intersection point exists (or the Straights are equal). >- */ >- public Ray getIntersection(Straight g2) { >- if (!intersects(g2)) { >- return null; >- } >- >- // retrieve position and direction Rays of other straight >- Ray q = g2.getPosition(); >- Ray b = g2.getDirection(); >- >- // retrieve orthogonal complements needed during computation >- Ray aOC = a.getOrthogonalComplement(); // orthogonal complement of a >- Ray bOC = b.getOrthogonalComplement(); // orthogonal complement of b >- >- // compute intersection point >- int[] intersection = new int[2]; >- intersection[0] = (q.dotProduct(bOC) * a.x - p.dotProduct(aOC) * b.x) >- / a.dotProduct(bOC); >- intersection[1] = (q.dotProduct(bOC) * a.y - p.dotProduct(aOC) * b.y) >- / a.dotProduct(bOC); >- return new Ray(intersection[0], intersection[1]); >- } >- >- /** >- * Returns the (smallest) angle between this Ray and the provided one. >- * >- * @param g2 >- * The Ray to be used for calculations. >- * @return The angle spanned between the two Rays. >- */ >- public double getAngle(Straight g2) { >- return a.getAngle(g2.getDirection()); >- } >- >- /** >- * Returns the projection of the given Ray onto this Straight, which is the >- * point on this Straight with the minimal distance to the point, denoted by >- * the provided Ray. >- * >- * @param q >- * The Ray whose projection should be determined. >- * @return A new Ray representing the projection of the provided Ray onto >- * this Straight. >- */ >- public Ray getProjection(Ray q) { >- Ray s = getIntersection(new Straight(q, a.getOrthogonalComplement())); >- return s; >- } >- >- /** >- * Returns the distance of the provided Ray to this Straight, which is the >- * distance between the provided Ray and its projection onto this Straight. >- * >- * @param q >- * The Ray whose distance is to be calculated. >- * @return the distance between this Straight and the provided Ray. >- */ >- public double getDistance(Ray q) { >- Ray s = getProjection(q); >- return s.getSubtracted(q).length(); >- } >- >- /** >- * Calculates wether the point indicated by the provided Ray is a point on >- * this Straight. >- * >- * @param q >- * the Ray who has to be checked. >- * @return true if the position indicated by the given Ray is a point of >- * this Straight, false otherwise. >- */ >- public boolean contains(Ray q) { >- return getDistance(q) == 0.0; >- } >- >- /** >- * Checks whether this Straight is equal to the provided Straight. Two >- * Straights s1 and s2 are equal, if the position vector of s2 is a point on >- * s1 and the direction vectors of s1 and s2 are parallel. >- * >- * @see java.lang.Object#equals(java.lang.Object) >- */ >- public boolean equals(Object other) { >- if (!(other instanceof Straight)) { >- return false; >- } else { >- Straight otherLine = (Straight) other; >- return contains(otherLine.getPosition()) >- && a.getOrthogonalComplement().dotProduct( >- otherLine.getDirection()) == 0; >- } >- } >- >-} >\ No newline at end of file >Index: src/org/eclipse/draw2d/geometry/euclidean/Straight.java >=================================================================== >RCS file: src/org/eclipse/draw2d/geometry/euclidean/Straight.java >diff -N src/org/eclipse/draw2d/geometry/euclidean/Straight.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/draw2d/geometry/euclidean/Straight.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,187 @@ >+/******************************************************************************* >+ * Copyright (c) 2010 IBM Corporation and others. >+ * 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: >+ * Research Group Software Construction, >+ * RWTH Aachen University, Germany - initial API and implementation >+ */ >+package org.eclipse.draw2d.geometry.euclidean; >+ >+import org.eclipse.draw2d.geometry.PrecisionPoint; >+ >+/** >+ * Represents a straight line within a 2-dimensional space. Together with >+ * Vector, this allows computations defined within Euclidean geometry. >+ * >+ * @author Alexander Nyssen >+ * @since 3.6 >+ */ >+public class Straight { >+ >+ private Vector p; // Vector indicating the position vector of the straight >+ private Vector a; // Vector representing the direction vector of this >+ // straight >+ >+ /** >+ * Constructs a new Straight with the given position and direction. >+ * >+ * @param position >+ * @param direction >+ */ >+ public Straight(Vector position, Vector direction) { >+ this.p = position; >+ this.a = direction; >+ } >+ >+ /** >+ * Constructs a new Straight between the two given Points. >+ * >+ * @param point1 >+ * the first waypoint >+ * @param point2 >+ * the second waypoint >+ */ >+ public Straight(PrecisionPoint point1, PrecisionPoint point2) { >+ this(new Vector(point1), new Vector(point1, point2)); >+ } >+ >+ /** >+ * Returns the position of this Straight. >+ * >+ * @return A Vector indicating the position of this Straight. >+ */ >+ public Vector getPosition() { >+ return p; >+ } >+ >+ /** >+ * Returns the direction of this Straight. >+ * >+ * @return A Vector indicating the position of this Straight. >+ */ >+ public Vector getDirection() { >+ return a; >+ } >+ >+ /** >+ * Checks whether this Straight and the provided one have an intersection >+ * point. >+ * >+ * @param s2 >+ * The Straight to use for calculations. It may not be equal to >+ * this Straight. >+ * @return true if the two Straights intersect, false otherwise. >+ */ >+ public boolean intersects(Straight s2) { >+ // check if there is an intersection point >+ return a.dotProduct(s2.getDirection().getOrthogonalComplement()) != 0; >+ } >+ >+ /** >+ * Computes the intersection point of this Straight and the provided one, if >+ * it exists. >+ * >+ * @param s2 >+ * The Straight to use for calculations. It may not be equal to >+ * this Straight. >+ * @return A Vector pointing to the intersection point, if it exists, null >+ * if no intersection point exists (or the Straights are equal). >+ */ >+ public Vector getIntersection(Straight s2) { >+ if (!intersects(s2)) { >+ return null; >+ } >+ >+ // retrieve position and direction Vectors of other straight >+ Vector q = s2.getPosition(); >+ Vector b = s2.getDirection(); >+ >+ // retrieve orthogonal complements needed during computation >+ Vector aOC = a.getOrthogonalComplement(); // orthogonal complement of a >+ Vector bOC = b.getOrthogonalComplement(); // orthogonal complement of b >+ >+ // compute intersection point >+ double[] intersection = new double[2]; >+ intersection[0] = (q.dotProduct(bOC) * a.x - p.dotProduct(aOC) * b.x) >+ / a.dotProduct(bOC); >+ intersection[1] = (q.dotProduct(bOC) * a.y - p.dotProduct(aOC) * b.y) >+ / a.dotProduct(bOC); >+ return new Vector(intersection[0], intersection[1]); >+ } >+ >+ /** >+ * Returns the (smallest) angle between this Straight and the provided one. >+ * >+ * @param s2 >+ * The Straight to be used for calculations. >+ * @return The angle spanned between the two Straights. >+ */ >+ public double getAngle(Straight s2) { >+ return a.getAngle(s2.getDirection()); >+ } >+ >+ /** >+ * Returns the projection of the given Vector onto this Straight, which is >+ * the point on this Straight with the minimal distance to the point, >+ * denoted by the provided Vector. >+ * >+ * @param q >+ * The Vector whose projection should be determined. >+ * @return A new Vector representing the projection of the provided Vector >+ * onto this Straight. >+ */ >+ public Vector getProjection(Vector q) { >+ Vector s = getIntersection(new Straight(q, a.getOrthogonalComplement())); >+ return s; >+ } >+ >+ /** >+ * Returns the distance of the provided Vector to this Straight, which is >+ * the distance between the provided Vector and its projection onto this >+ * Straight. >+ * >+ * @param q >+ * The Vector whose distance is to be calculated. >+ * @return the distance between this Straight and the provided Vector. >+ */ >+ public double getDistance(Vector q) { >+ Vector s = getProjection(q); >+ return s.getSubtracted(q).length(); >+ } >+ >+ /** >+ * Calculates whether the point indicated by the provided Vector is a point >+ * on this Straight. >+ * >+ * @param q >+ * the Vector who has to be checked. >+ * @return true if the position indicated by the given Vector is a point of >+ * this Straight, false otherwise. >+ */ >+ public boolean contains(Vector q) { >+ return getDistance(q) == 0.0; >+ } >+ >+ /** >+ * Checks whether this Straight is equal to the provided Straight. Two >+ * straights s1 and s2 are equal, if the position vector of s2 is a point on >+ * s1 and the direction vectors of s1 and s2 are parallel. >+ * >+ * @see java.lang.Object#equals(java.lang.Object) >+ */ >+ public boolean equals(Object other) { >+ if (!(other instanceof Straight)) { >+ return false; >+ } else { >+ Straight otherLine = (Straight) other; >+ return contains(otherLine.getPosition()) >+ && a.getOrthogonalComplement().dotProduct( >+ otherLine.getDirection()) == 0; >+ } >+ } >+ >+} >\ No newline at end of file >Index: src/org/eclipse/draw2d/geometry/euclidean/Vector.java >=================================================================== >RCS file: src/org/eclipse/draw2d/geometry/euclidean/Vector.java >diff -N src/org/eclipse/draw2d/geometry/euclidean/Vector.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/draw2d/geometry/euclidean/Vector.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,249 @@ >+/******************************************************************************* >+ * Copyright (c) 2000, 2010 IBM Corporation and others. >+ * 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: >+ * IBM Corporation - initial API and implementation >+ * Research Group Software Construction, >+ * RWTH Aachen University, Germany - Contribution for Bugzilla 245182 >+ * >+ *******************************************************************************/ >+package org.eclipse.draw2d.geometry.euclidean; >+ >+import org.eclipse.draw2d.geometry.PrecisionPoint; >+import org.eclipse.draw2d.geometry.Ray; >+ >+/** >+ * Represents a 2-dimensional vector as of Euclidean geometry. This class was created based >+ * on {@link Ray}, and is meant as a replacement, using double precision. >+ * >+ * @author Alexander Nyssen >+ * @since 3.6 >+ */ >+public final class Vector { >+ >+ /** the X value */ >+ public double x; >+ /** the Y value */ >+ public double y; >+ >+ /** >+ * Constructs a Vector with no direction and magnitude. >+ * >+ */ >+ public Vector() { >+ } >+ >+ /** >+ * Constructs a Ray pointed in the specified direction. >+ * >+ * @param x >+ * X value. >+ * @param y >+ * Y value. >+ */ >+ public Vector(double x, double y) { >+ this.x = x; >+ this.y = y; >+ } >+ >+ /** >+ * Constructs a Ray pointed in the direction specified by a Point. >+ * >+ * @param p >+ * the point >+ */ >+ public Vector(PrecisionPoint p) { >+ x = p.preciseX; >+ y = p.preciseY; >+ } >+ >+ /** >+ * Constructs a Ray representing the direction and magnitude between to >+ * provided Points. >+ * >+ * @param start >+ * starting point >+ * @param end >+ * End Point >+ */ >+ public Vector(PrecisionPoint start, PrecisionPoint end) { >+ x = end.preciseX - start.preciseX; >+ y = end.preciseY - start.preciseY; >+ } >+ >+ /** >+ * Constructs a Ray representing the difference between two provided Rays. >+ * >+ * @param start >+ * The start Ray >+ * @param end >+ * The end Ray >+ */ >+ public Vector(Vector start, Vector end) { >+ x = end.x - start.x; >+ y = end.y - start.y; >+ } >+ >+ /** >+ * Calculates the magnitude of the cross product of this Ray with another. >+ * Represents the amount by which two Rays are directionally different. >+ * Parallel Rays return a value of 0. >+ * >+ * @param r >+ * Ray being compared >+ * @return The assimilarity >+ * @see #similarity(Vector) >+ */ >+ public double assimilarity(Vector r) { >+ return Math.abs(x * r.y - y * r.x); >+ } >+ >+ /** >+ * Calculates the dot product of this Ray with another. >+ * >+ * @param r >+ * the Ray used to perform the dot product >+ * @return The dot product >+ */ >+ public double dotProduct(Vector r) { >+ return x * r.x + y * r.y; >+ } >+ >+ /** >+ * @see java.lang.Object#equals(Object) >+ */ >+ public boolean equals(Object obj) { >+ if (obj == this) >+ return true; >+ if (obj instanceof Vector) { >+ Vector r = (Vector) obj; >+ return x == r.x && y == r.y; >+ } >+ return false; >+ } >+ >+ /** >+ * Creates a new Ray which is the sum of this Ray with another. >+ * >+ * @param r >+ * Ray to be added with this Ray >+ * @return a new Ray >+ */ >+ public Vector getAdded(Vector r) { >+ return new Vector(r.x + x, r.y + y); >+ } >+ >+ /** >+ * Creates a new Ray which is the difference of this Ray with the provided >+ * Ray. >+ * >+ * @param q >+ * Ray to be subtracted from this Ray >+ * @return a new Ray >+ */ >+ public Vector getSubtracted(Vector q) { >+ return new Vector(x - q.x, y - q.y); >+ } >+ >+ /** >+ * Returns the angle between this Ray and the provided Ray. >+ * >+ * @param q >+ * Ray to calculate the angle. >+ * @return the angle between the two Rays. >+ */ >+ public double getAngle(Vector q) { >+ double cosAlpha = dotProduct(q) / (length() * q.length()); >+ return Math.acos(cosAlpha); >+ } >+ >+ /** >+ * Creates a new Ray which represents the average of this Ray with another. >+ * >+ * @param r >+ * Ray to calculate the average. >+ * @return a new Ray >+ */ >+ public Vector getAveraged(Vector r) { >+ return new Vector((x + r.x) / 2, (y + r.y) / 2); >+ } >+ >+ /** >+ * Creates a new Ray which represents this Ray scaled by the amount >+ * provided. >+ * >+ * @param s >+ * Value providing the amount to scale. >+ * @return a new Ray >+ */ >+ public Vector getScaled(int s) { >+ return new Vector(x * s, y * s); >+ } >+ >+ /** >+ * Returns the orthogonal complement of this Ray, which is defined to be >+ * (-y, x). >+ * >+ * @return the orthogonal complement of this Ray >+ */ >+ public Vector getOrthogonalComplement() { >+ return new Vector(-y, x); >+ } >+ >+ /** >+ * @see java.lang.Object#hashCode() >+ */ >+ public int hashCode() { >+ return (int) (x * y) ^ (int) (x + y); >+ } >+ >+ /** >+ * Returns true if this Ray has a non-zero horizontal comonent. >+ * >+ * @return true if this Ray has a non-zero horizontal comonent >+ */ >+ public boolean isHorizontal() { >+ return x != 0; >+ } >+ >+ /** >+ * Returns the length of this Ray. >+ * >+ * @return Length of this Ray >+ */ >+ public double length() { >+ return Math.sqrt(dotProduct(this)); >+ } >+ >+ /** >+ * Calculates the similarity of this Ray with another. Similarity is defined >+ * as the absolute value of the dotProduct() >+ * >+ * @param r >+ * Ray being tested for similarity >+ * @return the Similarity >+ * @see #assimilarity(Vector) >+ */ >+ public double similarity(Vector r) { >+ return Math.abs(dotProduct(r)); >+ } >+ >+ /** >+ * @return a String representation >+ */ >+ public String toString() { >+ return "(" + x + "," + y + ")";//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$ >+ } >+ >+ /** >+ * @return a PrecisionPoint representation >+ */ >+ public PrecisionPoint toPoint() { >+ return new PrecisionPoint(x, y); >+ } >+ >+} >#P org.eclipse.draw2d.test >Index: src/org/eclipse/draw2d/test/Draw2dTestSuite.java >=================================================================== >RCS file: /cvsroot/tools/org.eclipse.gef/test/org.eclipse.draw2d.test/src/org/eclipse/draw2d/test/Draw2dTestSuite.java,v >retrieving revision 1.28 >diff -u -r1.28 Draw2dTestSuite.java >--- src/org/eclipse/draw2d/test/Draw2dTestSuite.java 18 Feb 2010 03:34:13 -0000 1.28 >+++ src/org/eclipse/draw2d/test/Draw2dTestSuite.java 26 Apr 2010 07:15:38 -0000 >@@ -51,6 +51,7 @@ > addTest(new TestSuite(RectangleTest.class)); > // addTest(new TestSuite(ColorConstantTest.class)); > addTest(new TestSuite(RayTest.class)); >+ addTest(new TestSuite(VectorTest.class)); > addTest(new TestSuite(StraightTest.class)); > addTest(new TestSuite(RelativeBendpointTest.class)); > addTest(new TestSuite(GeometryTest.class)); >Index: src/org/eclipse/draw2d/test/RayTest.java >=================================================================== >RCS file: /cvsroot/tools/org.eclipse.gef/test/org.eclipse.draw2d.test/src/org/eclipse/draw2d/test/RayTest.java,v >retrieving revision 1.3 >diff -u -r1.3 RayTest.java >--- src/org/eclipse/draw2d/test/RayTest.java 18 Feb 2010 03:34:13 -0000 1.3 >+++ src/org/eclipse/draw2d/test/RayTest.java 26 Apr 2010 07:15:38 -0000 >@@ -40,11 +40,6 @@ > testLengthValues(3, 4, 5); > testLengthValues(0, Integer.MAX_VALUE, Integer.MAX_VALUE); > } >- >- public void test_getOrthoComplement() { >- Ray a = new Ray(3, -5); >- assertTrue(a.getOrthogonalComplement().equals(new Ray(5, 3))); >- } > > public void test_getScalarProduct() { > Ray a = new Ray(3, 2); >Index: src/org/eclipse/draw2d/test/StraightTest.java >=================================================================== >RCS file: /cvsroot/tools/org.eclipse.gef/test/org.eclipse.draw2d.test/src/org/eclipse/draw2d/test/StraightTest.java,v >retrieving revision 1.1 >diff -u -r1.1 StraightTest.java >--- src/org/eclipse/draw2d/test/StraightTest.java 18 Feb 2010 03:34:13 -0000 1.1 >+++ src/org/eclipse/draw2d/test/StraightTest.java 26 Apr 2010 07:15:38 -0000 >@@ -13,8 +13,9 @@ > > import junit.framework.TestCase; > >-import org.eclipse.draw2d.geometry.Ray; >-import org.eclipse.draw2d.geometry.Straight; >+import org.eclipse.draw2d.geometry.PrecisionPoint; >+import org.eclipse.draw2d.geometry.euclidean.Straight; >+import org.eclipse.draw2d.geometry.euclidean.Vector; > > /** > * @author Alexander Nyssen >@@ -23,16 +24,27 @@ > public class StraightTest extends TestCase { > > public void test_getIntersection() { >- Ray p = new Ray(1, 1); >- Ray a = new Ray(2, 1); >- Ray q = new Ray(1, 4); >- Ray b = new Ray(1, -1); >+ // test integer precision >+ Vector p = new Vector(1, 1); >+ Vector a = new Vector(2, 1); >+ Vector q = new Vector(1, 4); >+ Vector b = new Vector(1, -1); > >- Ray intersection = new Straight(p, a) >- .getIntersection(new Straight(q, b)); >+ Vector intersection = new Straight(p, a).getIntersection(new Straight( >+ q, b)); > >- assertTrue(intersection.equals(new Ray(3, 2))); >+ assertTrue(intersection.equals(new Vector(3, 2))); > >+ // test double precision >+ p = new Vector(0, 0); >+ a = new Vector(new PrecisionPoint(0, 0), new PrecisionPoint(5, 5)); >+ q = new Vector(0, 5); >+ b = new Vector(new PrecisionPoint(0, 5), new PrecisionPoint(5, 0)); >+ >+ intersection = new Straight(p, a).getIntersection(new Straight(q, b)); >+ assertTrue(intersection.equals(new Vector(2.5, 2.5))); >+ >+ // check straight does not intersect itself > assertNull(new Straight(p, a).getIntersection(new Straight(p, a))); > } > } >Index: src/org/eclipse/draw2d/test/VectorTest.java >=================================================================== >RCS file: src/org/eclipse/draw2d/test/VectorTest.java >diff -N src/org/eclipse/draw2d/test/VectorTest.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/draw2d/test/VectorTest.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,60 @@ >+/******************************************************************************* >+ * Copyright (c) 2008, 2010 IBM Corporation and others. >+ * 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: >+ * IBM Corporation - initial API and implementation >+ *******************************************************************************/ >+package org.eclipse.draw2d.test; >+ >+import junit.framework.TestCase; >+ >+import org.eclipse.draw2d.geometry.euclidean.Vector; >+ >+/** >+ * Vector's tests >+ * >+ * @author aboyko >+ * >+ */ >+public class VectorTest extends TestCase { >+ >+ /** >+ * @see TestCase#setUp() >+ */ >+ protected void setUp() throws Exception { >+ super.setUp(); >+ } >+ >+ /** >+ * @see TestCase#tearDown() >+ */ >+ protected void tearDown() throws Exception { >+ super.tearDown(); >+ } >+ >+ public void test_length() { >+ testLengthValues(3, 4, 5); >+ testLengthValues(0, Integer.MAX_VALUE, Integer.MAX_VALUE); >+ } >+ >+ public void test_getOrthoComplement() { >+ Vector a = new Vector(3, -5); >+ assertTrue(a.getOrthogonalComplement().equals(new Vector(5, 3))); >+ } >+ >+ public void test_getScalarProduct() { >+ Vector a = new Vector(3, 2); >+ Vector b = new Vector(2, -2); >+ assertTrue(a.dotProduct(b) == 2); >+ } >+ >+ private void testLengthValues(int x, int y, double expectedLength) { >+ Vector Vector = new Vector(x, y); >+ assertEquals(expectedLength, Vector.length(), 0); >+ } >+ >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 310397
: 166050 |
168041
|
168047