Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "GEF/GEF4/Geometry"

< GEF‎ | GEF4
(AWT2SWT)
(21 intermediate revisions by 2 users not shown)
Line 4: Line 4:
  
 
== Introduction ==
 
== Introduction ==
 +
The <span style="color:#268558">[[GEF/GEF4/Geometry|GEF4 Geometry]]</span> component provides classes to store geometric objects and to perform geometric computations based on those objects. It is internally structured into three modules, namely '''[[#Geometry|Geometry]]''', '''[[#Geometry.Convert.FX|Geometry.Convert.FX]]''', and '''[[#Geometry.Convert.SWT|Geometry.Convert.SWT]]'''.
  
The GEF 4 Geometry component provides classes to store geometric objects and to perform geometric computations based on those objects. It is internally structured into three modules, namely '''[[#Geometry|Geometry]]''', '''[[#Geometry.Convert.FX|Geometry.Convert.FX]]''', and '''[[#Geometry.Convert.SWT|Geometry.Convert.SWT]]'''.
+
[[Image:GEF4-Components-Geometry.png|600px]]
  
 
----
 
----
Line 11: Line 12:
 
== Geometry ==
 
== Geometry ==
  
 +
*'''feature: org.eclipse.gef4.geometry'''
 
*'''bundle: org.eclipse.gef4.geometry'''
 
*'''bundle: org.eclipse.gef4.geometry'''
  
Different kinds of abstractions are provided to support different kinds of geometric calculations, within 2-dimensional Euclidean vector space or its related projective space, or based on 2-dimensional planar objects. Furthermore, conversions are supported to import/export from/to related SWT and AWT representations.
+
The [[#Geometry|Geometry]] module of [[GEF/GEF4/Geometry|GEF4 Geometry]] provides abstractions to support different kinds of geometric calculations within 2-dimensional Euclidean vector space or its related projective space, or based on 2-dimensional planar objects. Furthermore, conversions are supported to import/export from/to related AWT representations.
  
 
The API is universally based on double precision calculations. Result values are computed as precise as possible, most of the time. In situations where result values are approximated, the approximation is as precise as required for the related test methods to agree on the values. An imprecision is used in the comparisons of double precision values throughout the implementation. These imprecise comparisons shall ensure consistency with regard to floating point and approximation errors.
 
The API is universally based on double precision calculations. Result values are computed as precise as possible, most of the time. In situations where result values are approximated, the approximation is as precise as required for the related test methods to agree on the values. An imprecision is used in the comparisons of double precision values throughout the implementation. These imprecise comparisons shall ensure consistency with regard to floating point and approximation errors.
Line 96: Line 98:
 
*'''package: org.eclipse.gef4.geometry.planar'''
 
*'''package: org.eclipse.gef4.geometry.planar'''
  
The planar package provides basic abstractions for computations based on 2-dimensional geometric objects.  
+
The [[#Planar|Planar]] package provides basic abstractions for computations based on 2-dimensional geometric objects.  
  
 
[[Image:GEF4Geometry-planar-overview.png|1104px|Interface hierarchy]]
 
[[Image:GEF4Geometry-planar-overview.png|1104px|Interface hierarchy]]
  
As outlined above, with the exception of the [[#Path|Path]] abstraction, all objects are classified into either being curves, shapes, or multi shapes by means of respective interfaces. An ICurve is a one dimensional geometry, i.e. the result that you get by drawing a continuous line with a pencil. It has a start and an end point and you can approximate it by a series of [[#BezierCurve|BezierCurve]]s. An IShape is a two dimensional geometry, i.e. it continuously encloses a region on the drawing area, without holes. An IMultiShape is a (possibly) non-continuous set of IShapes. An example for an IMultiShape is the [[#Region|Region]]. A [[#Region|Region]] represents the area that results from composing multiple [[#Rectangle|Rectangle]]s. Accordingly, a [[#Ring|Ring]] represents the area that results from composing multiple [[#Polygon|Polygon]]s. It corresponds to the SWT [http://help.eclipse.org/indigo/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/graphics/Region.html Region].
+
==== IGeometry, ICurve, IShape, IMultiShape ====
 +
As outlined above, with the exception of the [[#Path|Path]] abstraction, all objects are classified into either being curves, shapes, or multi shapes by means of respective interfaces. An <code>ICurve</code> is a one dimensional geometry, i.e. the result that you get by drawing a continuous line with a pencil. It has a start and an end point and you can approximate it by a series of [[#BezierCurve|BezierCurve]]s. An <code>IShape</code> is a two dimensional geometry, i.e. it continuously encloses a region on the drawing area, without holes. An <code>IMultiShape</code> is a (possibly) non-continuous set of <code>IShape</code>s. An example for an <code>IMultiShape</code> is the [[#Region|Region]]. A [[#Region|Region]] represents the area that results from composing multiple [[#Rectangle|Rectangle]]s. Accordingly, a [[#Ring|Ring]] represents the area that results from composing multiple [[#Polygon|Polygon]]s. It corresponds to the [http://help.eclipse.org/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/graphics/Region.html org.eclipse.swt.graphics.Region].
  
 
The most general type in the hierarchy is the [[#Path|Path]], because every geometric object can be transfered into it. Unfortunately, the [[#Path|Path]] is incompatible to the rest of the API in that it does not implement the different interfaces, it does not ensure a certain precision for the results of its test and manipulation methods, and it cannot be transferred back into compatible objects.
 
The most general type in the hierarchy is the [[#Path|Path]], because every geometric object can be transfered into it. Unfortunately, the [[#Path|Path]] is incompatible to the rest of the API in that it does not implement the different interfaces, it does not ensure a certain precision for the results of its test and manipulation methods, and it cannot be transferred back into compatible objects.
Line 106: Line 109:
 
[[Image:GEF4Geometry-planar-abstractions-hierarchy.png|930px|Important functionality]]
 
[[Image:GEF4Geometry-planar-abstractions-hierarchy.png|930px|Important functionality]]
  
As you can see in this diagram, an ICurve can be approximated by a number of [[#BezierCurve|BezierCurve]]s using the toBezier() method. The outline of an IShape can be retrieved using its getOutline() method. Additionally, you can split an IShape into a number of ICurves -- which form the outline -- using the getOutlineSegments() method. IMultiShape provides a getShapes() method to get the individual IShapes that are combined by the IMultiShape. It does provide a getOutlineSegments() method, too, which is used to split the IMultiShape into several ICurves. These transfer methods allow the decomposition of any geometric object into a bunch of [[#BezierCurve|BezierCurve]]s:
+
As you can see in this diagram, an <code>ICurve</code> can be approximated by a number of [[#BezierCurve|BezierCurve]]s using the toBezier() method. The outline of an <code>IShape</code> can be retrieved using its getOutline() method. Additionally, you can split an <code>IShape</code> into a number of <code>ICurve</code>s -- which form the outline -- using the getOutlineSegments() method. <code>IMultiShape</code> provides a getShapes() method to get the individual <code>IShape</code>s that are combined by the <code>IMultiShape</code>. It does provide a getOutlineSegments() method, too, which is used to split the <code>IMultiShape</code> into several <code>ICurve</code>s. These transfer methods allow the decomposition of any geometric object into a bunch of [[#BezierCurve|BezierCurve]]s:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
BezierCurve[] fromCurve = curve.toBezier();
 
BezierCurve[] fromCurve = curve.toBezier();
 
BezierCurve[] fromShape = shape.getOutline().toBezier();
 
BezierCurve[] fromShape = shape.getOutline().toBezier();
Line 118: Line 120:
 
     beziers.addAll(Arrays.asList(c.toBezier()));
 
     beziers.addAll(Arrays.asList(c.toBezier()));
 
</source>
 
</source>
</div>
 
  
An important part of a geometry API in general, is the possibility to test the relationship of two geometric objects. The GEF 4 Geometry API provides four methods that perform relation tests. Universally usable is the touches() method for planar objects. It tests if two objects have at least one point in common. Additionally, ICurves can be tested for points of intersection using the intersects() method and for an overlap using the overlaps() method, among each other. An IShape provides a contains() method to test if it fully contains a given planar object. Moreover, the point test is available for every geometric object. It tests if a given [[#Point|Point]] is incidental to the particular object. Supplementary to the intersects() test, a getIntersections() method is offered among ICurves. [[#BezierCurve|BezierCurve]]s do also facilitate the extraction of overlapping segments via the getOverlap() method.
+
An important part of a geometry API in general, is the possibility to test the relationship of two geometric objects. [[GEF/GEF4/Geometry|GEF4 Geometry]] provides four methods that perform relation tests. Universally usable is the touches() method, which is supported for each <code>IGeometry</code>. It tests if two objects have at least one point in common. Additionally, <code>ICurve</code>s can be tested for points of intersection using the intersects() method and for an overlap using the overlaps() method, among each other. An <code>IShape</code> provides a contains() method to test if it fully contains a given planar object. Moreover, the point test is available for every geometric object. It tests if a given [[#Point|Point]] is incidental to the particular object. Supplementary to the intersects() test, a getIntersections() method is offered among ICurves. [[#BezierCurve|BezierCurve]]s do also facilitate the extraction of overlapping segments via the getOverlap() method.
  
 
So, let us consider a few examples:
 
So, let us consider a few examples:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
boolean contained = arc.contains(point);
 
boolean contained = arc.contains(point);
 
boolean contained = pie.contains(point);
 
boolean contained = pie.contains(point);
Line 135: Line 135:
 
boolean contained = region.contains(polygon);
 
boolean contained = region.contains(polygon);
 
</source>
 
</source>
</div>
 
  
Noticeably, the use of interfaces unifies similar operations on different types. Therefore, the fundamental interfaces (ICurve, IShape, etc.) are complemented by three transformation interfaces.
+
==== IRotatable, IScalable, ITranslatable ====
 +
Noticeably, the use of interfaces unifies similar operations on different types. Therefore, the fundamental interfaces (ICurve, IShape, etc.) are complemented by three transformation interfaces: <code>Rotatable</code>, <code>IScalable</code>, and <code>ITranslatable</code>.
  
 
[[Image:GEF4Geometry-planar-transformations-overview.png|785px|Transformation interfaces]]
 
[[Image:GEF4Geometry-planar-transformations-overview.png|785px|Transformation interfaces]]
  
You can either transform your geometric objects via instances of the AffineTransform class, or by using the short-cut methods provided by the ITranslatable, IScalable, and IRotatable interfaces. Transformations can either be directly applied to an object, modifying the object in-place, or to a copy of the original object. This distinction is represented by the names of the short-cut methods. All names starting with 'get' are applied to a copy of the original object. The other methods modify the object in-place.
+
You can either transform your geometric objects via instances of the AffineTransform class, or by using the short-cut methods provided by the <code>Rotatable</code>, <code>IScalable</code>, and <code>ITranslatable</code> interfaces. Transformations can either be directly applied to an object, modifying the object in-place, or to a copy of the original object. This distinction is represented by the names of the short-cut methods. All names starting with 'get' are applied to a copy of the original object. The other methods modify the object in-place.
  
 
Translating an object means moving the object. You can move an object in x- and y-direction. Scaling an object means resizing the object. You can individually scale the object in x- and y-direction. Additionally, scaling requires a relative [[#Point|Point]] to scale to/away from. If you omit this [[#Point|Point]], the scaling method will appropriately choose the relative [[#Point|Point]]. Normally, this will be the center [[#Point|Point]] of the geometric object that you want to scale. Rotation is special in that not all geometric objects can be rotated in-place. [[#Rectangle|Rectangle]]s, for example, are always parallel to the x- and y-axes. That's why the IRotatable interface does only include the getRotated*() short-cut methods which are not directly applied. However, some geometric objects do provide in-place rotation methods. As with scaling, rotation is performed around a relative [[#Point|Point]]. If you omit this [[#Point|Point]], the rotation method will appropriately choose it. Normally, this will be the center [[#Point|Point]] of the geometric object that you want to rotate.
 
Translating an object means moving the object. You can move an object in x- and y-direction. Scaling an object means resizing the object. You can individually scale the object in x- and y-direction. Additionally, scaling requires a relative [[#Point|Point]] to scale to/away from. If you omit this [[#Point|Point]], the scaling method will appropriately choose the relative [[#Point|Point]]. Normally, this will be the center [[#Point|Point]] of the geometric object that you want to scale. Rotation is special in that not all geometric objects can be rotated in-place. [[#Rectangle|Rectangle]]s, for example, are always parallel to the x- and y-axes. That's why the IRotatable interface does only include the getRotated*() short-cut methods which are not directly applied. However, some geometric objects do provide in-place rotation methods. As with scaling, rotation is performed around a relative [[#Point|Point]]. If you omit this [[#Point|Point]], the rotation method will appropriately choose it. Normally, this will be the center [[#Point|Point]] of the geometric object that you want to rotate.
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Polygon rhomb = new Rectangle(10, 10, 10, 10).getRotatedCCW(Angle.fromDeg(45));
 
Polygon rhomb = new Rectangle(10, 10, 10, 10).getRotatedCCW(Angle.fromDeg(45));
 
PolyBezier slanted = new Ellipse(100, 100, 100, 50).getRotatedCCW(Angle.fromDeg(30));
 
PolyBezier slanted = new Ellipse(100, 100, 100, 50).getRotatedCCW(Angle.fromDeg(30));
 
Ring rotatedClippingArea = region.getRotatedCCW(Angle.fromDeg(300));
 
Ring rotatedClippingArea = region.getRotatedCCW(Angle.fromDeg(300));
 
</source>
 
</source>
</div>
 
  
Augmenting the interface hierarchy, all concrete classes are based on abstract geometry classes, depending on the type of geometry used for constructing the objects (i.e. [[#Ellipse|Ellipse]] is an AbstractRectangleBasedGeometry, because it is constructed by means of a [[#Rectangle|Rectangle]]).
+
==== AbstractGeometry, AbstractRectangleBasedGeometry, AbstractArcBasedGeometry, AbstractPointListBasedGeometry, AbstractMultiShape ====
 +
Augmenting the interface hierarchy, all concrete classes are based on abstract geometry classes, depending on the type of geometry used for constructing the objects (i.e. [[#Ellipse|Ellipse]] is an <code>AbstractRectangleBasedGeometry</code>, because it is constructed by means of a [[#Rectangle|Rectangle]]).
  
 
[[Image:GEF4Geometry-planar-inheritance-hierarchy.png|1109px|Inheritance hierarchy]]
 
[[Image:GEF4Geometry-planar-inheritance-hierarchy.png|1109px|Inheritance hierarchy]]
Line 160: Line 159:
  
 
==== Point ====
 
==== Point ====
 
 
<code>Point</code> objects represent a point in 2-dimensional space. For the purpose of imagination, you can assume the coordinate system to be originated in the top left corner of your drawing area, expanding to the right and to the bottom. From a list of <code>Point</code> objects, you can build up most of the planar geometric objects:
 
<code>Point</code> objects represent a point in 2-dimensional space. For the purpose of imagination, you can assume the coordinate system to be originated in the top left corner of your drawing area, expanding to the right and to the bottom. From a list of <code>Point</code> objects, you can build up most of the planar geometric objects:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Point p0 = new Point(); // defaults: x=0, y=0
 
Point p0 = new Point(); // defaults: x=0, y=0
 
Point p1 = new Point(5, 0);
 
Point p1 = new Point(5, 0);
Line 170: Line 167:
 
Polygon triangle = new Polygon(p0, p1, p2);
 
Polygon triangle = new Polygon(p0, p1, p2);
 
</source>
 
</source>
</div>
 
  
 
Additionally, the <code>Point</code> class provides static utility methods to operate on a list of <code>Point</code>s: getBounds(Point...), getCentroid(Point...), and getConvexHull(Point...). They construct a bounding box, the centroid, and a convex hull of the given <code>Point</code> list, respectively.
 
Additionally, the <code>Point</code> class provides static utility methods to operate on a list of <code>Point</code>s: getBounds(Point...), getCentroid(Point...), and getConvexHull(Point...). They construct a bounding box, the centroid, and a convex hull of the given <code>Point</code> list, respectively.
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Polygon convexHull = Point.getConvexHull(points);
 
Polygon convexHull = Point.getConvexHull(points);
 
</source>
 
</source>
</div>
 
  
 
==== Dimension ====
 
==== Dimension ====
 
 
The [[#Dimension|Dimension]] class is the pendant of the org.eclipse.draw2d.geometry.Dimension class. It decouples the location and the width and height of a rectangular object.
 
The [[#Dimension|Dimension]] class is the pendant of the org.eclipse.draw2d.geometry.Dimension class. It decouples the location and the width and height of a rectangular object.
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Rectangle bounds = new Rectangle(
 
Rectangle bounds = new Rectangle(
 
     new Point(50, 50),
 
     new Point(50, 50),
Line 191: Line 183:
 
);
 
);
 
</source>
 
</source>
</div>
+
 
 
==== Line ====
 
==== Line ====
  
Line 201: Line 193:
 
A <code>Line</code> is the straight connection of two [[#Point|Point]]s:
 
A <code>Line</code> is the straight connection of two [[#Point|Point]]s:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">Line line = new Line(p0, p1);</source>
+
Line line = new Line(p0, p1)
</div>
+
</source>
  
 
As it inherits from the [[#BezierCurve|BezierCurve]] class, all the operations for [[#BezierCurve|BezierCurve]]s are available for <code>Line</code> objects, too. Because of its frequent use, <code>Line</code> overrides many of those operations to provide faster implementations for the <code>Line</code>/<code>Line</code> and <code>Line</code>/[[#Point|Point]] cases (equals(), touches(), contains(), intersects(), overlaps(), getIntersections(), and many more). If you want to display a <code>Line</code> using SWT, you can use the Geometry2SWT.toSWTPointArray() method as follows:
 
As it inherits from the [[#BezierCurve|BezierCurve]] class, all the operations for [[#BezierCurve|BezierCurve]]s are available for <code>Line</code> objects, too. Because of its frequent use, <code>Line</code> overrides many of those operations to provide faster implementations for the <code>Line</code>/<code>Line</code> and <code>Line</code>/[[#Point|Point]] cases (equals(), touches(), contains(), intersects(), overlaps(), getIntersections(), and many more). If you want to display a <code>Line</code> using SWT, you can use the Geometry2SWT.toSWTPointArray() method as follows:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">gc.drawPolyline(Geometry2SWT.toSWTPointArray(line));</source>
+
gc.drawPolyline(Geometry2SWT.toSWTPointArray(line));
</div>
+
</source>
  
 
==== Rectangle ====
 
==== Rectangle ====
 
 
[[Image:GEF4Geometry-planar-rectangle-example.png|Rectangle example]]
 
[[Image:GEF4Geometry-planar-rectangle-example.png|Rectangle example]]
  
Line 220: Line 211:
 
A <code>Rectangle</code> is the axes-parallel rectangle defined by a location (x- and y-coordinates) and a [[#Dimension|Dimension]] (width and height):
 
A <code>Rectangle</code> is the axes-parallel rectangle defined by a location (x- and y-coordinates) and a [[#Dimension|Dimension]] (width and height):
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">Rectangle rect = new Rectangle(x, y, w, h);</source>
+
Rectangle rect = new Rectangle(x, y, w, h);
</div>
+
</source>
  
 
Rotating a <code>Rectangle</code> results in a [[#Polygon|Polygon]]:
 
Rotating a <code>Rectangle</code> results in a [[#Polygon|Polygon]]:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">Polygon slanted = rect.getRotatedCCW(Angle.fromDeg(30));
<source lang="java">Polygon slanted = rect.getRotatedCCW(Angle.fromDeg(30));</source>
+
</source>
</div>
+
  
 
<code>Rectangle</code> objects are frequently used, that's why some operations are overridden to provide faster implementations for designated parameter types (equals(), contains(), touches()). If you want to display a <code>Rectangle</code> using SWT, you can use the Geometry2SWT.toSWTRectangle() method as follows:
 
<code>Rectangle</code> objects are frequently used, that's why some operations are overridden to provide faster implementations for designated parameter types (equals(), contains(), touches()). If you want to display a <code>Rectangle</code> using SWT, you can use the Geometry2SWT.toSWTRectangle() method as follows:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
gc.drawRectangle(Geometry2SWT.toSWTRectangle(rect));
 
gc.drawRectangle(Geometry2SWT.toSWTRectangle(rect));
 
</source>
 
</source>
</div>
 
  
 
==== Polyline ====
 
==== Polyline ====
Line 247: Line 235:
 
A <code>Polyline</code> combines multiple [[#Line|Line]] segments to address them as a whole. Consecutive [[#Line|Line]] segments of a <code>Polyline</code> share at least one end [[#Point|Point]]. The outline of some of the IShape implementations can be represented by a <code>Polyline</code> ([[#Rectangle|Rectangle]] and [[#Polygon|Polygon]]):
 
A <code>Polyline</code> combines multiple [[#Line|Line]] segments to address them as a whole. Consecutive [[#Line|Line]] segments of a <code>Polyline</code> share at least one end [[#Point|Point]]. The outline of some of the IShape implementations can be represented by a <code>Polyline</code> ([[#Rectangle|Rectangle]] and [[#Polygon|Polygon]]):
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Polyline polyLine = new Polyline(new Line(p0, p1), new Line(p1, p2));
 
Polyline polyLine = new Polyline(new Line(p0, p1), new Line(p1, p2));
 
Polyline outline = Polygon.getOutline();
 
Polyline outline = Polygon.getOutline();
 
</source>
 
</source>
</div>
 
  
 
To render a <code>Polyline</code> with SWT, you can use the Geometry2SWT.toSWTPointArray() method as follows:
 
To render a <code>Polyline</code> with SWT, you can use the Geometry2SWT.toSWTPointArray() method as follows:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
gc.drawPolyline(Geometry2SWT.toSWTPointArray(polyline));
 
gc.drawPolyline(Geometry2SWT.toSWTPointArray(polyline));
 
</source>
 
</source>
</div>
 
  
 
==== Polygon ====
 
==== Polygon ====
Line 271: Line 255:
 
A <code>Polygon</code> represents a simple polygon, i.e one that does not have intersecting sides:
 
A <code>Polygon</code> represents a simple polygon, i.e one that does not have intersecting sides:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Polygon rhomb = new Polygon(0, 0, 1, -1, 2, 0, 1, 1);
 
Polygon rhomb = new Polygon(0, 0, 1, -1, 2, 0, 1, 1);
 
</source>
 
</source>
</div>
 
  
 
If you need to process self-intersecting polygons, you can use the [[#Ring|Ring]] instead.
 
If you need to process self-intersecting polygons, you can use the [[#Ring|Ring]] instead.
Line 281: Line 263:
 
A <code>Polygon</code> can be rendered with SWT using the Geometry2SWT.toSWTPointArray() method as follows:
 
A <code>Polygon</code> can be rendered with SWT using the Geometry2SWT.toSWTPointArray() method as follows:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
gc.drawPolyline(Geometry2SWT.toSWTPointArray(polygon));
 
gc.drawPolyline(Geometry2SWT.toSWTPointArray(polygon));
 
</source>
 
</source>
</div>
 
  
 
==== Ellipse ====
 
==== Ellipse ====
Line 296: Line 276:
 
An <code>Ellipse</code> is the axes-symmetric oval that can be put into an axes-parallel [[#Rectangle|Rectangle]]:
 
An <code>Ellipse</code> is the axes-symmetric oval that can be put into an axes-parallel [[#Rectangle|Rectangle]]:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Ellipse ellipse = new Ellipse(rect);
 
Ellipse ellipse = new Ellipse(rect);
 
</source>
 
</source>
</div>
 
  
 
Therefore, rotating an <code>Ellipse</code> does not result in another <code>Ellipse</code>, but in a [[#PolyBezier|PolyBezier]] which approximates the rotated <code>Ellipse</code>:
 
Therefore, rotating an <code>Ellipse</code> does not result in another <code>Ellipse</code>, but in a [[#PolyBezier|PolyBezier]] which approximates the rotated <code>Ellipse</code>:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
PolyBezier rotatedEllipse = ellipse.getRotatedCCW(Angle.fromDeg(45));
 
PolyBezier rotatedEllipse = ellipse.getRotatedCCW(Angle.fromDeg(45));
 
</source>
 
</source>
</div>
 
  
 
You can always transfrom a [[#PolyBezier|PolyBezier]] into an <code>Ellipse</code> by using the [[#PolyBezier|PolyBezier]]'s bounds as the <code>Ellipse</code>'s bounds:
 
You can always transfrom a [[#PolyBezier|PolyBezier]] into an <code>Ellipse</code> by using the [[#PolyBezier|PolyBezier]]'s bounds as the <code>Ellipse</code>'s bounds:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Ellipse rotated = new Ellipse(rotatedEllipse.getBounds());
 
Ellipse rotated = new Ellipse(rotatedEllipse.getBounds());
 
</source>
 
</source>
</div>
 
  
 
If you want to draw an <code>Ellipse</code> using SWT, you can directly use the GC's drawOval() method as follows:
 
If you want to draw an <code>Ellipse</code> using SWT, you can directly use the GC's drawOval() method as follows:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
gc.drawOval((int) ellipse.getX(), (int) ellipse.getY(), (int) ellipse.getWidth(), (int) ellipse.getHeight());
 
gc.drawOval((int) ellipse.getX(), (int) ellipse.getY(), (int) ellipse.getWidth(), (int) ellipse.getHeight());
 
</source>
 
</source>
</div>
 
  
 
==== Arc ====
 
==== Arc ====
Line 335: Line 307:
 
An <code>Arc</code> is an open segment of an [[#Ellipse|Ellipse]]:
 
An <code>Arc</code> is an open segment of an [[#Ellipse|Ellipse]]:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">Arc arc = new Arc(ellipse, Angle.fromDeg(45), Angle.fromDeg(90));</source>
+
Arc arc = new Arc(ellipse, Angle.fromDeg(45), Angle.fromDeg(90));
</div>
+
</source>
  
 
Rotating an <code>Arc</code> does not necessarily result in another <code>Arc</code>, that's why the rotation methods return [[#PolyBezier|PolyBezier]]s instead:
 
Rotating an <code>Arc</code> does not necessarily result in another <code>Arc</code>, that's why the rotation methods return [[#PolyBezier|PolyBezier]]s instead:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
PolyBezier polyBezier = arc.getRotatedCCW(Angle.fromDeg(15), new Point());
 
PolyBezier polyBezier = arc.getRotatedCCW(Angle.fromDeg(15), new Point());
 
</source>
 
</source>
</div>
 
  
 
Unfortunately, it is impossible to transfrom a [[#PolyBezier|PolyBezier]] into an <code>Arc</code>.
 
Unfortunately, it is impossible to transfrom a [[#PolyBezier|PolyBezier]] into an <code>Arc</code>.
Line 358: Line 328:
 
A <code>Pie</code> is a closed [[#Arc|Arc]]. Closing the [[#Arc|Arc]] is done by creating two segments: one from the mid [[#Point|Point]] of the related [[#Ellipse|Ellipse]] to the start [[#Point|Point]] of the related [[#Arc|Arc]], the other from the mid [[#Point|Point]] of the [[#Ellipse|Ellipse]] to the end [[#Point|Point]] of the [[#Arc|Arc]].
 
A <code>Pie</code> is a closed [[#Arc|Arc]]. Closing the [[#Arc|Arc]] is done by creating two segments: one from the mid [[#Point|Point]] of the related [[#Ellipse|Ellipse]] to the start [[#Point|Point]] of the related [[#Arc|Arc]], the other from the mid [[#Point|Point]] of the [[#Ellipse|Ellipse]] to the end [[#Point|Point]] of the [[#Arc|Arc]].
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Pie pie = new Pie(arc);
 
Pie pie = new Pie(arc);
 
</source>
 
</source>
</div>
 
  
 
Rotating a <code>Pie</code> results in a [[#Path|Path]], which, unfortunately, cannot be transformed back into a <code>Pie</code>:
 
Rotating a <code>Pie</code> results in a [[#Path|Path]], which, unfortunately, cannot be transformed back into a <code>Pie</code>:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Path rotatedPie = pie.getRotatedCCW(Angle.fromDeg(15), new Point());#
 
Path rotatedPie = pie.getRotatedCCW(Angle.fromDeg(15), new Point());#
 
</source>
 
</source>
</div>
 
  
 
==== RoundedRectangle ====
 
==== RoundedRectangle ====
Line 381: Line 347:
 
A <code>RoundedRectangle</code> is a rectangle with round corners. The corners are 90 degrees [[#Arc|Arc]] objects:
 
A <code>RoundedRectangle</code> is a rectangle with round corners. The corners are 90 degrees [[#Arc|Arc]] objects:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
RoundedRectangle rr = new RoundedRectangle(bounds, arcWidth, arcHeight);
 
RoundedRectangle rr = new RoundedRectangle(bounds, arcWidth, arcHeight);
 
</source>
 
</source>
</div>
 
  
 
Rotating a <code>RoundedRectangle</code> does not result in another <code>RoundedRectangle</code>, but rather in a [[#PolyBezier|PolyBezier]] describing the rotated outline:
 
Rotating a <code>RoundedRectangle</code> does not result in another <code>RoundedRectangle</code>, but rather in a [[#PolyBezier|PolyBezier]] describing the rotated outline:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
PolyBezier rotated = rr.getRotatedCCW();
 
PolyBezier rotated = rr.getRotatedCCW();
 
</source>
 
</source>
</div>
 
  
 
Unfortunately, it is impossible to transform a [[#PolyBezier|PolyBezier]] into a <code>RoundedRectangle</code>.
 
Unfortunately, it is impossible to transform a [[#PolyBezier|PolyBezier]] into a <code>RoundedRectangle</code>.
Line 406: Line 368:
 
A <code>BezierCurve</code> is constructed by a number of control [[#Point|Point]]s: the start [[#Point|Point]], an arbitrary number of handle [[#Point|Point]]s, and the end [[#Point|Point]]. The curve approaches the handle [[#Point|Point]]s. Therefore, the handle [[#Point|Point]]s describe the flow of the curve. The more handle [[#Point|Point]]s used, the more converges the <code>BezierCurve</code> to the [[#Polyline|Polyline]] through the control [[#Point|Point]]s:
 
A <code>BezierCurve</code> is constructed by a number of control [[#Point|Point]]s: the start [[#Point|Point]], an arbitrary number of handle [[#Point|Point]]s, and the end [[#Point|Point]]. The curve approaches the handle [[#Point|Point]]s. Therefore, the handle [[#Point|Point]]s describe the flow of the curve. The more handle [[#Point|Point]]s used, the more converges the <code>BezierCurve</code> to the [[#Polyline|Polyline]] through the control [[#Point|Point]]s:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
BezierCurve curve = new BezierCurve(pStart, pHandle0, pHandle1, pHandle2, ..., pEnd);
 
BezierCurve curve = new BezierCurve(pStart, pHandle0, pHandle1, pHandle2, ..., pEnd);
 
</source>
 
</source>
</div>
 
  
 
==== QuadraticCurve ====
 
==== QuadraticCurve ====
Line 439: Line 399:
 
A <code>PolyBezier</code> combines multiple [[#BezierCurve|BezierCurve]] segments to address them as a whole:
 
A <code>PolyBezier</code> combines multiple [[#BezierCurve|BezierCurve]] segments to address them as a whole:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
PolyBezier polyBezier = new PolyBezier(line, quadCurve, cubicCurve, arbitraryBezierCurve);
 
PolyBezier polyBezier = new PolyBezier(line, quadCurve, cubicCurve, arbitraryBezierCurve);
 
</source>
 
</source>
</div>
 
  
 
Consecutive [[#BezierCurve|BezierCurve]]s are connected with each other, i.e. the end [[#Point|Point]] of one [[#BezierCurve|BezierCurve]] is the start [[#Point|Point]] of the subsequent [[#BezierCurve|BezierCurve]]. The outline of several IShape implementations can be represented by a <code>PolyBezier</code> ([[#Ellipse|Ellipse]], [[#Pie|Pie]], and [[#RoundedRectangle|RoundedRectangle]]):
 
Consecutive [[#BezierCurve|BezierCurve]]s are connected with each other, i.e. the end [[#Point|Point]] of one [[#BezierCurve|BezierCurve]] is the start [[#Point|Point]] of the subsequent [[#BezierCurve|BezierCurve]]. The outline of several IShape implementations can be represented by a <code>PolyBezier</code> ([[#Ellipse|Ellipse]], [[#Pie|Pie]], and [[#RoundedRectangle|RoundedRectangle]]):
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
PolyBezier outline = pie.getOutline();
 
PolyBezier outline = pie.getOutline();
 
</source>
 
</source>
</div>
 
  
 
Besides, the <code>PolyBezier</code> class provides a method to interpolate a number of [[#CubicCurve|CubicCurve]]s through a set of [[#Point|Point]]s:
 
Besides, the <code>PolyBezier</code> class provides a method to interpolate a number of [[#CubicCurve|CubicCurve]]s through a set of [[#Point|Point]]s:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
PolyBezier interpolation = PolyBezier.interpolateCubic(p0, p1, p2, p3, ...);
 
PolyBezier interpolation = PolyBezier.interpolateCubic(p0, p1, p2, p3, ...);
 
</source>
 
</source>
</div>
 
  
 
==== CurvedPolygon ====
 
==== CurvedPolygon ====
Line 479: Line 433:
 
A <code>Region</code> is build up of multiple [[#Rectangle|Rectangle]]s to address their enclosing area as a unit. The [[#Rectangle|Rectangle]]s that build up the <code>Region</code> do not have to touch each other. If they intersect, the [[#Rectangle|Rectangle]]s are divided into a number of internal [[#Rectangle|Rectangle]]s that do not intersect:
 
A <code>Region</code> is build up of multiple [[#Rectangle|Rectangle]]s to address their enclosing area as a unit. The [[#Rectangle|Rectangle]]s that build up the <code>Region</code> do not have to touch each other. If they intersect, the [[#Rectangle|Rectangle]]s are divided into a number of internal [[#Rectangle|Rectangle]]s that do not intersect:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Region region = new Region(rect0, rect1, rect2);
 
Region region = new Region(rect0, rect1, rect2);
 
</source>
 
</source>
</div>
 
  
You can use a <code>Region</code> as a clipping area as in the example image above. For this purpose, it can be transfered into a SWT [http://help.eclipse.org/indigo/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/graphics/Region.html org.eclipse.swt.graphics.Region] using the Geometry2SWT.toSWTRegion() method as follows:
+
You can use a <code>Region</code> as a clipping area as in the example image above. For this purpose, it can be transfered into a SWT [http://help.eclipse.org/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/graphics/Region.html org.eclipse.swt.graphics.Region] using the Geometry2SWT.toSWTRegion() method as follows:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
gc.setClipping(Geometry2SWT.toSWTRegion(region));
 
gc.setClipping(Geometry2SWT.toSWTRegion(region));
 
</source>
 
</source>
</div>
 
  
 
Rotating a <code>Region</code> results in a [[#Ring|Ring]]:
 
Rotating a <code>Region</code> results in a [[#Ring|Ring]]:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Ring rotatedRegion = region.getRotatedCCW(Angle.fromDeg(45));
 
Ring rotatedRegion = region.getRotatedCCW(Angle.fromDeg(45));
 
</source>
 
</source>
</div>
 
  
 
==== Ring ====
 
==== Ring ====
Line 510: Line 458:
 
A <code>Ring</code> is build up of multiple [[#Polygon|Polygon]]s to address their enclosing area as a unit. The [[#Polygon|Polygon]]s that build up the <code>Ring</code> do not have to touch each other. They are transfered into an internal number of triangles that do not intersect:
 
A <code>Ring</code> is build up of multiple [[#Polygon|Polygon]]s to address their enclosing area as a unit. The [[#Polygon|Polygon]]s that build up the <code>Ring</code> do not have to touch each other. They are transfered into an internal number of triangles that do not intersect:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Ring ring = new Ring(poly0, poly1, poly2);
 
Ring ring = new Ring(poly0, poly1, poly2);
 
</source>
 
</source>
</div>
 
  
 
==== Path ====
 
==== Path ====
Line 524: Line 470:
 
Using a <code>Path</code> is like drawing the joker. You can transfer every other geometric object into a <code>Path</code> using the toPath() method. But the <code>Path</code> does not implement the [[GEF/GEF4/Geometry|GEF4 Geometry]] interfaces. It simply delegates to the java.awt.geom.Path2D. That's why you should try to avoid using the <code>Path</code> if you want to perform further computations. On the other hand, a <code>Path</code> is easy to render via SWT:
 
Using a <code>Path</code> is like drawing the joker. You can transfer every other geometric object into a <code>Path</code> using the toPath() method. But the <code>Path</code> does not implement the [[GEF/GEF4/Geometry|GEF4 Geometry]] interfaces. It simply delegates to the java.awt.geom.Path2D. That's why you should try to avoid using the <code>Path</code> if you want to perform further computations. On the other hand, a <code>Path</code> is easy to render via SWT:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
gc.drawPath(new org.eclipse.swt.graphics.Path(Display.getCurrent(), Geometry2SWT.toSWTPathData(gef4Path));
 
gc.drawPath(new org.eclipse.swt.graphics.Path(Display.getCurrent(), Geometry2SWT.toSWTPathData(gef4Path));
 
gc.fillPath(new org.eclipse.swt.graphics.Path(Display.getCurrent(), Geometry2SWT.toSWTPathData(gef4Path));
 
gc.fillPath(new org.eclipse.swt.graphics.Path(Display.getCurrent(), Geometry2SWT.toSWTPathData(gef4Path));
 
</source>
 
</source>
</div>
 
  
 
----
 
----
Line 537: Line 481:
 
*'''package: org.eclipse.gef4.geometry.euclidean'''
 
*'''package: org.eclipse.gef4.geometry.euclidean'''
  
The euclidean package provides core abstractions ([[#Vector|Vector]], [[#Straight|Straight]], and [[#Angle|Angle]]) to support calculations within 2-dimensional Euclidean space.
+
The [[#Euclidean|Euclidean]] package provides core abstractions ([[#Vector|Vector]], [[#Straight|Straight]], and [[#Angle|Angle]]) to support calculations within 2-dimensional Euclidean space.
  
 
[[Image:GEF4Geometry-euclidean-overview.png|907px|Angle, Vector, and Straight]]
 
[[Image:GEF4Geometry-euclidean-overview.png|907px|Angle, Vector, and Straight]]
Line 545: Line 489:
 
Considering rotation and the angular relationship of two straight lines, <code>Angle</code> objects come into play. They abstract over the two commonly used angle units, degrees and radians. The user has to specify the unit of the value an <code>Angle</code> object is constructed from. Moreover, the user can read the value of an <code>Angle</code> object in either degrees or radians. Therefore, the use of <code>Angle</code> objects assures that correct values are used in calculations. This indirection is done due to an inconsistency of several APIs, for example, org.eclipse.swt.graphics.Transform vs. org.eclipse.draw2d.geometry.Transform.
 
Considering rotation and the angular relationship of two straight lines, <code>Angle</code> objects come into play. They abstract over the two commonly used angle units, degrees and radians. The user has to specify the unit of the value an <code>Angle</code> object is constructed from. Moreover, the user can read the value of an <code>Angle</code> object in either degrees or radians. Therefore, the use of <code>Angle</code> objects assures that correct values are used in calculations. This indirection is done due to an inconsistency of several APIs, for example, org.eclipse.swt.graphics.Transform vs. org.eclipse.draw2d.geometry.Transform.
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
// creates a 75% pie chart
 
// creates a 75% pie chart
 
Pie chart = new Pie(0, 0, 100, 100, Angle.fromDeg(15), Angle.fromDeg(270));
 
Pie chart = new Pie(0, 0, 100, 100, Angle.fromDeg(15), Angle.fromDeg(270));
 
</source>
 
</source>
</div>
 
  
 
==== Vector ====
 
==== Vector ====
Line 556: Line 498:
 
A <code>Vector</code> has two components x and y. It can be interpreted as a planar [[#Point|Point]] (toPoint()). The <code>Vector</code> class implements the common arithmetic operations for vectors: addition, multiplication with a scalar, dot product, cross product, and [[#Angle|Angle]] calculation between two <code>Vector</code>s:
 
A <code>Vector</code> has two components x and y. It can be interpreted as a planar [[#Point|Point]] (toPoint()). The <code>Vector</code> class implements the common arithmetic operations for vectors: addition, multiplication with a scalar, dot product, cross product, and [[#Angle|Angle]] calculation between two <code>Vector</code>s:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">Vector u = new Vector(1, 0);
+
Vector u = new Vector(1, 0);
 
Vector v = new Vector(0, 1);
 
Vector v = new Vector(0, 1);
 
double zero = u.getDotProduct(v);
 
double zero = u.getDotProduct(v);
 
</source>
 
</source>
</div>
 
  
 
==== Straight ====
 
==== Straight ====
Line 567: Line 508:
 
A <code>Straight</code> is an infinite planar line. You can build it up from either two [[#Point|Point]]s which the <code>Straight</code> passes through, or by specifying a position and a direction [[#Vector|Vector]]:
 
A <code>Straight</code> is an infinite planar line. You can build it up from either two [[#Point|Point]]s which the <code>Straight</code> passes through, or by specifying a position and a direction [[#Vector|Vector]]:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Straight diagonal = new Straight(new Point(1, 1), new Point(2, 2));
 
Straight diagonal = new Straight(new Point(1, 1), new Point(2, 2));
 
Straight diagonal = new Straight(new Vector(1, 1), new Vector(1, 1)); // exactly the same Straight
 
Straight diagonal = new Straight(new Vector(1, 1), new Vector(1, 1)); // exactly the same Straight
 
</source>
 
</source>
</div>
 
  
 
----
 
----
Line 580: Line 519:
 
*'''package: org.eclipse.gef4.geometry.projective'''
 
*'''package: org.eclipse.gef4.geometry.projective'''
  
The projective package provides classes ([[#Vector3D|Vector3D]], [[#Straight3D|Straight3D]]) to represent euclidean elements in the projective plane.
+
The [[#Projective|Projective]] package provides classes ([[#Vector3D|Vector3D]], [[#Straight3D|Straight3D]]) to represent euclidean elements in the projective plane.
  
 
[[Image:GEF4Geometry-projective-overview.png|479px|Vector3D and Straight3D]]
 
[[Image:GEF4Geometry-projective-overview.png|479px|Vector3D and Straight3D]]
Line 588: Line 527:
 
This approach leads to elegant mathematical solutions to some of the basic planar geometric operations. You want to know if a point lies on a line? Simply substitute its coordinate values into the [[#Straight3D|Straight3D]]'s formula. You want to know the distance of a point to a line? If the [[#Straight3D|Straight3D]]'s vector is normalized (a^2 + b^2 = 1), the formula evaluates to the signed distance of the point to the line, i.e. on one side of the line it is positive and on the other side it is negative. You want to know where two lines are intersecting? Simply compute the cross product of two (x, y, z) triples. These operations are packed in appropriately named methods.
 
This approach leads to elegant mathematical solutions to some of the basic planar geometric operations. You want to know if a point lies on a line? Simply substitute its coordinate values into the [[#Straight3D|Straight3D]]'s formula. You want to know the distance of a point to a line? If the [[#Straight3D|Straight3D]]'s vector is normalized (a^2 + b^2 = 1), the formula evaluates to the signed distance of the point to the line, i.e. on one side of the line it is positive and on the other side it is negative. You want to know where two lines are intersecting? Simply compute the cross product of two (x, y, z) triples. These operations are packed in appropriately named methods.
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Straight3D s = Straight3D.through(new Vector3D(s0), new Vector3D(s1)); // s0, s1 are Points
 
Straight3D s = Straight3D.through(new Vector3D(s0), new Vector3D(s1)); // s0, s1 are Points
 
double signedDistance = s.getSignedDistanceCW(p); // Math.abs() => absolute distance
 
double signedDistance = s.getSignedDistanceCW(p); // Math.abs() => absolute distance
Line 596: Line 534:
 
Point poi = intersection.toPoint();
 
Point poi = intersection.toPoint();
 
</source>
 
</source>
</div>
 
  
 
==== Vector3D ====
 
==== Vector3D ====
 
A <code>Vector3D</code> consists of three components (x, y, z). It represents the [[#Point|Point]] (x/z, y/z) in 2-dimensional space. You can use the common arithmetic operations for vectors on a <code>Vector3D</code> object:
 
A <code>Vector3D</code> consists of three components (x, y, z). It represents the [[#Point|Point]] (x/z, y/z) in 2-dimensional space. You can use the common arithmetic operations for vectors on a <code>Vector3D</code> object:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Vector3D v = new Vector3D(1, 2, 3);
 
Vector3D v = new Vector3D(1, 2, 3);
 
Vector3D u = v.getAdded(new Vector3D(3, -6, 1));
 
Vector3D u = v.getAdded(new Vector3D(3, -6, 1));
 
double zero = v.getDotProduct(u);
 
double zero = v.getDotProduct(u);
 
</source>
 
</source>
</div>
 
  
 
==== Straight3D ====
 
==== Straight3D ====
 
A <code>Straight3D</code> consists of three components (x, y, z). It represents the line ax + by + z = 0 in 2-dimensional space, where (a, b) is a planar [[#Point|Point]]. You can use a <code>Straight3D</code> to calculate the distance of a [[#Vector3D|Vector3D]] to the <code>Straight3D</code>. Moreover, you can compute the point of intersection of two <code>Straight3D</code>s:
 
A <code>Straight3D</code> consists of three components (x, y, z). It represents the line ax + by + z = 0 in 2-dimensional space, where (a, b) is a planar [[#Point|Point]]. You can use a <code>Straight3D</code> to calculate the distance of a [[#Vector3D|Vector3D]] to the <code>Straight3D</code>. Moreover, you can compute the point of intersection of two <code>Straight3D</code>s:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Straight3D s = Straight3D.through(new Vector3D(1, 1, 1), new Vector3D(2, 1, 1));
 
Straight3D s = Straight3D.through(new Vector3D(1, 1, 1), new Vector3D(2, 1, 1));
 
double one = s.getSignedDistanceCW(new Vector3D(1.5, 2, 1));
 
double one = s.getSignedDistanceCW(new Vector3D(1.5, 2, 1));
 
</source>
 
</source>
</div>
 
  
 
----
 
----
Line 625: Line 558:
 
*'''package: org.eclipse.gef4.geometry.convert.awt'''
 
*'''package: org.eclipse.gef4.geometry.convert.awt'''
  
The Convert.AWT package contains helper classes to transfer data from AWT/Geometry to one another.
+
The [[#Convert.AWT|Convert.AWT]] package contains helper classes to transfer data from AWT/Geometry to one another.
  
 
[[Image:GEF4Geometry-conversions-AWT.png|766px|AWT/Geometry conversions]]
 
[[Image:GEF4Geometry-conversions-AWT.png|766px|AWT/Geometry conversions]]
Line 633: Line 566:
 
[[#Point|Point]], [[#Line|Line]], [[#Rectangle|Rectangle]], [[#RoundedRectangle|RoundedRectangle]], and AffineTransform objects can be transfered into their AWT pendants using the toAWT*() methods of the [[#Geometry2AWT |Geometry2AWT]] class:
 
[[#Point|Point]], [[#Line|Line]], [[#Rectangle|Rectangle]], [[#RoundedRectangle|RoundedRectangle]], and AffineTransform objects can be transfered into their AWT pendants using the toAWT*() methods of the [[#Geometry2AWT |Geometry2AWT]] class:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
RoundRectangle2D rr2d = Geometry2AWT.toAWTRoundedRectangle(rr);
 
RoundRectangle2D rr2d = Geometry2AWT.toAWTRoundedRectangle(rr);
 
</source>
 
</source>
</div>
 
==== AWT2Geomety ====
 
  
Correspondingly, the [[#AWT2Geometry |AWT2Geometry]] class provides methods to transfer AWT objects into GEF 4 Geometry objects:
+
==== AWT2Geometry ====
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
Correspondingly, the [[#AWT2Geometry|AWT2Geometry]] class provides methods to transfer AWT objects into GEF 4 Geometry objects:
<source lang="java">
+
 
 +
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
 
RoundedRectangle rr = AWT2Geometry.toRoundedRectangle(rr2d);
 
RoundedRectangle rr = AWT2Geometry.toRoundedRectangle(rr2d);
 
</source>
 
</source>
</div>
 
  
 
----
 
----
Line 652: Line 582:
 
== Geometry.Convert.FX ==
 
== Geometry.Convert.FX ==
  
 +
*'''feature: org.eclipse.gef4.geometry.convert.fx'''
 
*'''bundle: org.eclipse.gef4.geometry.convert.fx'''
 
*'''bundle: org.eclipse.gef4.geometry.convert.fx'''
 +
 +
The [[#Geometry.Convert.FX|Geometry.Convert.FX]] module of [[GEF/GEF4/Geometry|GEF4 Geometry]] contains helper classes for the conversion of JavaFX/Geometry objects to one another.
  
 
----
 
----
Line 660: Line 593:
 
*'''package: org.eclipse.gef4.geometry.convert.fx'''
 
*'''package: org.eclipse.gef4.geometry.convert.fx'''
  
The Convert.FX package contains helper classes to transfer data from JavaFX/Geometry to one another.
+
The [[#Convert.FX|Convert.FX]] package contains helper classes to transfer data from JavaFX/Geometry to one another.
  
 
[[Image:GEF4Geometry-conversions-FX.png|517px|JavaFX/Geometry conversions]]
 
[[Image:GEF4Geometry-conversions-FX.png|517px|JavaFX/Geometry conversions]]
Line 668: Line 601:
 
AffineTransform, Path, Point, and Rectangle objects can be transfered into their JavaFX pendants using the toFX*() methods provided by the <code>Geometry2JavaFX</code> class:
 
AffineTransform, Path, Point, and Rectangle objects can be transfered into their JavaFX pendants using the toFX*() methods provided by the <code>Geometry2JavaFX</code> class:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Rectangle rect = new Rectangle(10, 10, 100, 50);
 
Rectangle rect = new Rectangle(10, 10, 100, 50);
 
javafx.geometry.Bounds rectFx = Geometry2JavaFX.toFXBounds(rect);
 
javafx.geometry.Bounds rectFx = Geometry2JavaFX.toFXBounds(rect);
 
</source>
 
</source>
</div>
 
  
 
Additionally, the #toPathElements(Path) method allows the conversion of a Path object into JavaFX PathElements.
 
Additionally, the #toPathElements(Path) method allows the conversion of a Path object into JavaFX PathElements.
Line 685: Line 616:
 
== Geometry.Convert.SWT ==
 
== Geometry.Convert.SWT ==
  
 +
*'''feature: org.eclipse.gef4.geometry.convert.swt'''
 
*'''bundle: org.eclipse.gef4.geometry.convert.swt'''
 
*'''bundle: org.eclipse.gef4.geometry.convert.swt'''
 +
 +
The [[#Geometry.Convert.SWT|Geometry.Convert.SWT]] module of [[GEF/GEF4/Geometry|GEF4 Geometry]] contains helper classes for the conversion of SWT/Geometry objects to one another and SWT/AWT objects to one another.
  
 
----
 
----
Line 693: Line 627:
 
*'''package: org.eclipse.gef4.geometry.convert.swt'''
 
*'''package: org.eclipse.gef4.geometry.convert.swt'''
  
The Convert.SWT package contains helper classes to transfer data from SWT/Geometry to one another and from SWT/AWT to one another.
+
The [[#Convert.SWT|Convert.SWT]] package contains helper classes to transfer data from SWT/Geometry to one another and from SWT/AWT to one another.
  
 
[[Image:GEF4Geometry-conversions-SWT.png|608px|SWT/Geometry conversions]]
 
[[Image:GEF4Geometry-conversions-SWT.png|608px|SWT/Geometry conversions]]
Line 699: Line 633:
 
==== Geometry2SWT ====
 
==== Geometry2SWT ====
  
Path, Point, Line, Polygon, Polyline, Rectangle, Region, and Ring objects can be transfered into their SWT pendants using the toSWT*() methods provided by the <code>Geometry2SWT</code> class:
+
[[#Path|Path]], [[#Point|Point]], [[#Line|Line]], [[#Polygon|Polygon]], [[#Polyline|Polyline]], [[#Rectangle|Rectangle]], [[#Region|Region]], and [[#Ring|Ring]] objects can be transfered into their SWT pendants using the toSWT*() methods provided by the <code>Geometry2SWT</code> class:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
Rectangle rect = new Rectangle(10, 10, 100, 50);
 
Rectangle rect = new Rectangle(10, 10, 100, 50);
 
org.eclipse.swt.graphics.Rectangle rectSWT = Geometry2SWT.toSWTRectangle(rect);
 
org.eclipse.swt.graphics.Rectangle rectSWT = Geometry2SWT.toSWTRectangle(rect);
 
</source>
 
</source>
</div>
 
  
 
Using the IGeometry#toPath() method, you can easily convert any [[GEF/GEF4/Geometry|GEF4 Geometry]] object into an SWT PathData representation:
 
Using the IGeometry#toPath() method, you can easily convert any [[GEF/GEF4/Geometry|GEF4 Geometry]] object into an SWT PathData representation:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
CubicCurve curve = new CubicCurve(0, 0, 50, 0, 0, 50, 50, 50);
 
CubicCurve curve = new CubicCurve(0, 0, 50, 0, 0, 50, 50, 50);
 
PathData pd = Geometry2SWT.toSWTPathData(curve.toPath());
 
PathData pd = Geometry2SWT.toSWTPathData(curve.toPath());
 
</source>
 
</source>
</div>
 
  
 
==== SWT2Geometry ====
 
==== SWT2Geometry ====
Line 723: Line 653:
 
==== SWT2AWT ====
 
==== SWT2AWT ====
  
The <code>SWT2AWT</code> class contains a toPathIterator() method which transfers an SWT PathData into an AWT PathIterator.
+
The <code>SWT2AWT</code> class contains a toPathIterator() method which transfers an SWT PathData into an [http://docs.oracle.com/javase/7/docs/api/index.html?java/awt/geom/PathIterator.html java.awt.geom.PathIterator].
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
PathIterator pathIterator = SWT2AWT.toPathIterator(pathData, windingRule);
 
PathIterator pathIterator = SWT2AWT.toPathIterator(pathData, windingRule);
 
</source>
 
</source>
</div>
 
  
 
==== AWT2SWT ====
 
==== AWT2SWT ====
  
The <code>AWT2SWT</code> class contains a toSWTPathData() method which transfers an AWT PathIterator into an SWT PathData. Consider that the winding rule of the AWT PathIterator is not kept in the SWT PathData, because the latter does not store this information. Instead, it is provided by an SWT Path or by the SWT GC that is used to draw the SWT PathData object:
+
The <code>AWT2SWT</code> class contains a toSWTPathData() method which transfers an [http://docs.oracle.com/javase/7/docs/api/index.html?java/awt/geom/PathIterator.html java.awt.geom.PathIterator] into an org.eclipse.swt.graphics.PathData. Consider that the winding rule of the AWT PathIterator is not kept in the SWT PathData, because the latter does not store this information. Instead, it is provided by an SWT Path or by the SWT GC that is used to draw the SWT PathData object:
  
<div style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
+
<source lang="java" style="border-style:solid;border-color:#f2f2f2;border-width:1px;padding:10px;margin-bottom:10px">
<source lang="java">
+
 
PathData pathData = AWT2SWT.toSWTPathData(pathIterator);
 
PathData pathData = AWT2SWT.toSWTPathData(pathIterator);
 
</source>
 
</source>
</div>
 
 
[[Category:GEF]]
 
[[Category:GEF]]

Revision as of 07:34, 3 June 2015

Note to non-wiki readers: This documentation is generated from the Eclipse wiki - if you have corrections or additions it would be awesome if you added them in the original wiki page.


Introduction

The GEF4 Geometry component provides classes to store geometric objects and to perform geometric computations based on those objects. It is internally structured into three modules, namely Geometry, Geometry.Convert.FX, and Geometry.Convert.SWT.

GEF4-Components-Geometry.png


Geometry

  • feature: org.eclipse.gef4.geometry
  • bundle: org.eclipse.gef4.geometry

The Geometry module of GEF4 Geometry provides abstractions to support different kinds of geometric calculations within 2-dimensional Euclidean vector space or its related projective space, or based on 2-dimensional planar objects. Furthermore, conversions are supported to import/export from/to related AWT representations.

The API is universally based on double precision calculations. Result values are computed as precise as possible, most of the time. In situations where result values are approximated, the approximation is as precise as required for the related test methods to agree on the values. An imprecision is used in the comparisons of double precision values throughout the implementation. These imprecise comparisons shall ensure consistency with regard to floating point and approximation errors.

GEF4Geometry-jdt-icons-package obj.gifPlanar GEF4Geometry-jdt-icons-package obj.gifEuclidean GEF4Geometry-jdt-icons-package obj.gifProjective GEF4Geometry-jdt-icons-package obj.gifConvert.AWT
GEF4Geometry-jdt-icons-int obj.gifICurve GEF4Geometry-jdt-icons-int obj.gifIShape GEF4Geometry-jdt-icons-int obj.gifIMultiShape GEF4Geometry-jdt-icons-class obj.gifPath GEF4Geometry-jdt-icons-class obj.gifAngle GEF4Geometry-jdt-icons-class obj.gifStraight3D GEF4Geometry-jdt-icons-class obj.gifGeometry2AWT
GEF4Geometry-jdt-icons-class obj.gifArc GEF4Geometry-jdt-icons-class obj.gifEllipse GEF4Geometry-jdt-icons-class obj.gifRegion   GEF4Geometry-jdt-icons-class obj.gifStraight GEF4Geometry-jdt-icons-class obj.gifVector3D GEF4Geometry-jdt-icons-class obj.gifAWT2Geometry
GEF4Geometry-jdt-icons-class obj.gifBezierCurve GEF4Geometry-jdt-icons-class obj.gifPie GEF4Geometry-jdt-icons-class obj.gifRing   GEF4Geometry-jdt-icons-class obj.gifVector    
GEF4Geometry-jdt-icons-class obj.gifCubicCurve GEF4Geometry-jdt-icons-class obj.gifPolygon          
GEF4Geometry-jdt-icons-class obj.gifLine GEF4Geometry-jdt-icons-class obj.gifRectangle          
GEF4Geometry-jdt-icons-class obj.gifQuadraticCurve GEF4Geometry-jdt-icons-class obj.gifRoundedRectangle          
GEF4Geometry-jdt-icons-class obj.gifPolyBezier GEF4Geometry-jdt-icons-class obj.gifCurvedPolygon          
GEF4Geometry-jdt-icons-class obj.gifPolyline            

To ease navigation, the following sections are organized around the source code packages (org.eclipse.gef4.geomtry.*) of the API, as they were designed to represent the different domains of abstractions.


Planar

  • package: org.eclipse.gef4.geometry.planar

The Planar package provides basic abstractions for computations based on 2-dimensional geometric objects.

Interface hierarchy

IGeometry, ICurve, IShape, IMultiShape

As outlined above, with the exception of the Path abstraction, all objects are classified into either being curves, shapes, or multi shapes by means of respective interfaces. An ICurve is a one dimensional geometry, i.e. the result that you get by drawing a continuous line with a pencil. It has a start and an end point and you can approximate it by a series of BezierCurves. An IShape is a two dimensional geometry, i.e. it continuously encloses a region on the drawing area, without holes. An IMultiShape is a (possibly) non-continuous set of IShapes. An example for an IMultiShape is the Region. A Region represents the area that results from composing multiple Rectangles. Accordingly, a Ring represents the area that results from composing multiple Polygons. It corresponds to the org.eclipse.swt.graphics.Region.

The most general type in the hierarchy is the Path, because every geometric object can be transfered into it. Unfortunately, the Path is incompatible to the rest of the API in that it does not implement the different interfaces, it does not ensure a certain precision for the results of its test and manipulation methods, and it cannot be transferred back into compatible objects.

Important functionality

As you can see in this diagram, an ICurve can be approximated by a number of BezierCurves using the toBezier() method. The outline of an IShape can be retrieved using its getOutline() method. Additionally, you can split an IShape into a number of ICurves -- which form the outline -- using the getOutlineSegments() method. IMultiShape provides a getShapes() method to get the individual IShapes that are combined by the IMultiShape. It does provide a getOutlineSegments() method, too, which is used to split the IMultiShape into several ICurves. These transfer methods allow the decomposition of any geometric object into a bunch of BezierCurves:

BezierCurve[] fromCurve = curve.toBezier();
BezierCurve[] fromShape = shape.getOutline().toBezier();
 
ICurve[] fromPolyShape = polyShape.getOutlineSegments();
List<BezierCurve> beziers = new ArrayList<BezierCurve>();
for (ICurve c : fromPolyShape)
    beziers.addAll(Arrays.asList(c.toBezier()));

An important part of a geometry API in general, is the possibility to test the relationship of two geometric objects. GEF4 Geometry provides four methods that perform relation tests. Universally usable is the touches() method, which is supported for each IGeometry. It tests if two objects have at least one point in common. Additionally, ICurves can be tested for points of intersection using the intersects() method and for an overlap using the overlaps() method, among each other. An IShape provides a contains() method to test if it fully contains a given planar object. Moreover, the point test is available for every geometric object. It tests if a given Point is incidental to the particular object. Supplementary to the intersects() test, a getIntersections() method is offered among ICurves. BezierCurves do also facilitate the extraction of overlapping segments via the getOverlap() method.

So, let us consider a few examples:

boolean contained = arc.contains(point);
boolean contained = pie.contains(point);
Point[] intersections = line1.getIntersections(line2);
Point[] intersections = line.getIntersections(polygon.getOutline());
Point[] intersections = polygon.getOutline().getIntersections(roundedRectangle.getOutline());
boolean contained = ellipse.contains(line);
boolean contained = rectangle.contains(ellipse);
boolean contained = region.contains(polygon);

IRotatable, IScalable, ITranslatable

Noticeably, the use of interfaces unifies similar operations on different types. Therefore, the fundamental interfaces (ICurve, IShape, etc.) are complemented by three transformation interfaces: Rotatable, IScalable, and ITranslatable.

Transformation interfaces

You can either transform your geometric objects via instances of the AffineTransform class, or by using the short-cut methods provided by the Rotatable, IScalable, and ITranslatable interfaces. Transformations can either be directly applied to an object, modifying the object in-place, or to a copy of the original object. This distinction is represented by the names of the short-cut methods. All names starting with 'get' are applied to a copy of the original object. The other methods modify the object in-place.

Translating an object means moving the object. You can move an object in x- and y-direction. Scaling an object means resizing the object. You can individually scale the object in x- and y-direction. Additionally, scaling requires a relative Point to scale to/away from. If you omit this Point, the scaling method will appropriately choose the relative Point. Normally, this will be the center Point of the geometric object that you want to scale. Rotation is special in that not all geometric objects can be rotated in-place. Rectangles, for example, are always parallel to the x- and y-axes. That's why the IRotatable interface does only include the getRotated*() short-cut methods which are not directly applied. However, some geometric objects do provide in-place rotation methods. As with scaling, rotation is performed around a relative Point. If you omit this Point, the rotation method will appropriately choose it. Normally, this will be the center Point of the geometric object that you want to rotate.

Polygon rhomb = new Rectangle(10, 10, 10, 10).getRotatedCCW(Angle.fromDeg(45));
PolyBezier slanted = new Ellipse(100, 100, 100, 50).getRotatedCCW(Angle.fromDeg(30));
Ring rotatedClippingArea = region.getRotatedCCW(Angle.fromDeg(300));

AbstractGeometry, AbstractRectangleBasedGeometry, AbstractArcBasedGeometry, AbstractPointListBasedGeometry, AbstractMultiShape

Augmenting the interface hierarchy, all concrete classes are based on abstract geometry classes, depending on the type of geometry used for constructing the objects (i.e. Ellipse is an AbstractRectangleBasedGeometry, because it is constructed by means of a Rectangle).

Inheritance hierarchy

This classification by the construction type of the individual geometry objects allows the generalization of many operations in a few abstract classes. Those abstract classes implement methods that should return an object of the same type as the inheriting class. Thus, type parameters are used to specify such return types.

Point

Point objects represent a point in 2-dimensional space. For the purpose of imagination, you can assume the coordinate system to be originated in the top left corner of your drawing area, expanding to the right and to the bottom. From a list of Point objects, you can build up most of the planar geometric objects:

Point p0 = new Point(); // defaults: x=0, y=0
Point p1 = new Point(5, 0);
Point p2 = new Point(0, 5);
Polygon triangle = new Polygon(p0, p1, p2);

Additionally, the Point class provides static utility methods to operate on a list of Points: getBounds(Point...), getCentroid(Point...), and getConvexHull(Point...). They construct a bounding box, the centroid, and a convex hull of the given Point list, respectively.

Polygon convexHull = Point.getConvexHull(points);

Dimension

The Dimension class is the pendant of the org.eclipse.draw2d.geometry.Dimension class. It decouples the location and the width and height of a rectangular object.

Rectangle bounds = new Rectangle(
    new Point(50, 50),
    new Dimension(80, 20)
);

Line

Line example

  • extends: BezierCurve
  • implements: ICurve, ITranslatable, IScalable, IRotatable

A Line is the straight connection of two Points:

Line line = new Line(p0, p1)

As it inherits from the BezierCurve class, all the operations for BezierCurves are available for Line objects, too. Because of its frequent use, Line overrides many of those operations to provide faster implementations for the Line/Line and Line/Point cases (equals(), touches(), contains(), intersects(), overlaps(), getIntersections(), and many more). If you want to display a Line using SWT, you can use the Geometry2SWT.toSWTPointArray() method as follows:

gc.drawPolyline(Geometry2SWT.toSWTPointArray(line));

Rectangle

Rectangle example

  • extends: AbstractRectangleBasedGeometry
  • implements: IShape, ITranslatable, IScalable, IRotatable

A Rectangle is the axes-parallel rectangle defined by a location (x- and y-coordinates) and a Dimension (width and height):

Rectangle rect = new Rectangle(x, y, w, h);

Rotating a Rectangle results in a Polygon:

Polygon slanted = rect.getRotatedCCW(Angle.fromDeg(30));

Rectangle objects are frequently used, that's why some operations are overridden to provide faster implementations for designated parameter types (equals(), contains(), touches()). If you want to display a Rectangle using SWT, you can use the Geometry2SWT.toSWTRectangle() method as follows:

gc.drawRectangle(Geometry2SWT.toSWTRectangle(rect));

Polyline

PolyLine example

  • extends: AbstractPointListBasedGeometry
  • implements: ICurve, ITranslatable, IScalable, IRotatable

A Polyline combines multiple Line segments to address them as a whole. Consecutive Line segments of a Polyline share at least one end Point. The outline of some of the IShape implementations can be represented by a Polyline (Rectangle and Polygon):

Polyline polyLine = new Polyline(new Line(p0, p1), new Line(p1, p2));
Polyline outline = Polygon.getOutline();

To render a Polyline with SWT, you can use the Geometry2SWT.toSWTPointArray() method as follows:

gc.drawPolyline(Geometry2SWT.toSWTPointArray(polyline));

Polygon

Polygon example

  • extends: AbstractPointListBasedGeometry
  • implements: IShape, ITranslatable, IScalable, IRotatable

A Polygon represents a simple polygon, i.e one that does not have intersecting sides:

Polygon rhomb = new Polygon(0, 0, 1, -1, 2, 0, 1, 1);

If you need to process self-intersecting polygons, you can use the Ring instead.

A Polygon can be rendered with SWT using the Geometry2SWT.toSWTPointArray() method as follows:

gc.drawPolyline(Geometry2SWT.toSWTPointArray(polygon));

Ellipse

Ellipse example

  • extends: AbstractRectangleBasedGeometry
  • implements: IShape, ITranslatable, IScalable, IRotatable

An Ellipse is the axes-symmetric oval that can be put into an axes-parallel Rectangle:

Ellipse ellipse = new Ellipse(rect);

Therefore, rotating an Ellipse does not result in another Ellipse, but in a PolyBezier which approximates the rotated Ellipse:

PolyBezier rotatedEllipse = ellipse.getRotatedCCW(Angle.fromDeg(45));

You can always transfrom a PolyBezier into an Ellipse by using the PolyBezier's bounds as the Ellipse's bounds:

Ellipse rotated = new Ellipse(rotatedEllipse.getBounds());

If you want to draw an Ellipse using SWT, you can directly use the GC's drawOval() method as follows:

gc.drawOval((int) ellipse.getX(), (int) ellipse.getY(), (int) ellipse.getWidth(), (int) ellipse.getHeight());

Arc

Arc example

  • extends: AbstractArcBasedGeometry (which extends AbstractRectangleBasedGeometry)
  • implements: ICurve, ITranslatable, IScalable, IRotatable

An Arc is an open segment of an Ellipse:

Arc arc = new Arc(ellipse, Angle.fromDeg(45), Angle.fromDeg(90));

Rotating an Arc does not necessarily result in another Arc, that's why the rotation methods return PolyBeziers instead:

PolyBezier polyBezier = arc.getRotatedCCW(Angle.fromDeg(15), new Point());

Unfortunately, it is impossible to transfrom a PolyBezier into an Arc.

Pie

Pie example

  • extends: AbstractArcBasedGeometry (which extends AbstractRectangleBasedGeometry)
  • implements: IShape, ITranslatable, IScalable, IRotatable

A Pie is a closed Arc. Closing the Arc is done by creating two segments: one from the mid Point of the related Ellipse to the start Point of the related Arc, the other from the mid Point of the Ellipse to the end Point of the Arc.

Pie pie = new Pie(arc);

Rotating a Pie results in a Path, which, unfortunately, cannot be transformed back into a Pie:

Path rotatedPie = pie.getRotatedCCW(Angle.fromDeg(15), new Point());#

RoundedRectangle

RoundedRectangle example

  • extends: AbstractRectangleBasedGeometry
  • implements: IShape, ITranslatable, IScalable, IRotatable

A RoundedRectangle is a rectangle with round corners. The corners are 90 degrees Arc objects:

RoundedRectangle rr = new RoundedRectangle(bounds, arcWidth, arcHeight);

Rotating a RoundedRectangle does not result in another RoundedRectangle, but rather in a PolyBezier describing the rotated outline:

PolyBezier rotated = rr.getRotatedCCW();

Unfortunately, it is impossible to transform a PolyBezier into a RoundedRectangle.

BezierCurve

BezierCurve example

  • extends: AbstractGeometry
  • implements: ICurve, ITranslatable, IScalable, IRotatable

A BezierCurve is constructed by a number of control Points: the start Point, an arbitrary number of handle Points, and the end Point. The curve approaches the handle Points. Therefore, the handle Points describe the flow of the curve. The more handle Points used, the more converges the BezierCurve to the Polyline through the control Points:

BezierCurve curve = new BezierCurve(pStart, pHandle0, pHandle1, pHandle2, ..., pEnd);

QuadraticCurve

QuadraticCurve example

  • extends: BezierCurve
  • implements: ICurve, ITranslatable, IScalable, IRotatable

A QuadraticCurve is a BezierCurve with three control Points: the start Point, one handle Point, and the end Point.

CubicCurve

CubicCurve example

  • extends: BezierCurve
  • implements: ICurve, ITranslatable, IScalable, IRotatable

A CubicCurve is a BezierCurve with four control Points: the start Point, two handle Points, and the end Point. Many geometry objects approximate their round outline segments using a number of CubicCurves (Ellipse, Arc, Pie, and RoundedRectangle).

PolyBezier

PolyBezier example

  • extends: AbstractGeometry
  • implements: ICurve, ITranslatable, IScalable, IRotatable

A PolyBezier combines multiple BezierCurve segments to address them as a whole:

PolyBezier polyBezier = new PolyBezier(line, quadCurve, cubicCurve, arbitraryBezierCurve);

Consecutive BezierCurves are connected with each other, i.e. the end Point of one BezierCurve is the start Point of the subsequent BezierCurve. The outline of several IShape implementations can be represented by a PolyBezier (Ellipse, Pie, and RoundedRectangle):

PolyBezier outline = pie.getOutline();

Besides, the PolyBezier class provides a method to interpolate a number of CubicCurves through a set of Points:

PolyBezier interpolation = PolyBezier.interpolateCubic(p0, p1, p2, p3, ...);

CurvedPolygon

CurvedPolygon example

  • extends: AbstractGeometry
  • implements: IShape, ITranslatable, IScalable, IRotatable

A CurvedPolygon is composed by a number of BezierCurves where two subsequent BezierCurves have to share one end Point. Moreover, a CurvedPolygon is always closed, so the end Point of the last BezierCurve has to be equal to the start Point of the first BezierCurve.

Region

Region example

  • extends: AbstractPolyShape
  • implements: IMultiShape, ITranslatable, IScalable, IRotatable

A Region is build up of multiple Rectangles to address their enclosing area as a unit. The Rectangles that build up the Region do not have to touch each other. If they intersect, the Rectangles are divided into a number of internal Rectangles that do not intersect:

Region region = new Region(rect0, rect1, rect2);

You can use a Region as a clipping area as in the example image above. For this purpose, it can be transfered into a SWT org.eclipse.swt.graphics.Region using the Geometry2SWT.toSWTRegion() method as follows:

gc.setClipping(Geometry2SWT.toSWTRegion(region));

Rotating a Region results in a Ring:

Ring rotatedRegion = region.getRotatedCCW(Angle.fromDeg(45));

Ring

Ring example

  • extends: AbstractPolyShape
  • implements: IMultiShape, ITranslatable, IScalable, IRotatable

A Ring is build up of multiple Polygons to address their enclosing area as a unit. The Polygons that build up the Ring do not have to touch each other. They are transfered into an internal number of triangles that do not intersect:

Ring ring = new Ring(poly0, poly1, poly2);

Path

Path example

  • extends: AbstractGeometry

Using a Path is like drawing the joker. You can transfer every other geometric object into a Path using the toPath() method. But the Path does not implement the GEF4 Geometry interfaces. It simply delegates to the java.awt.geom.Path2D. That's why you should try to avoid using the Path if you want to perform further computations. On the other hand, a Path is easy to render via SWT:

gc.drawPath(new org.eclipse.swt.graphics.Path(Display.getCurrent(), Geometry2SWT.toSWTPathData(gef4Path));
gc.fillPath(new org.eclipse.swt.graphics.Path(Display.getCurrent(), Geometry2SWT.toSWTPathData(gef4Path));

Euclidean

  • package: org.eclipse.gef4.geometry.euclidean

The Euclidean package provides core abstractions (Vector, Straight, and Angle) to support calculations within 2-dimensional Euclidean space.

Angle, Vector, and Straight

Angle

Considering rotation and the angular relationship of two straight lines, Angle objects come into play. They abstract over the two commonly used angle units, degrees and radians. The user has to specify the unit of the value an Angle object is constructed from. Moreover, the user can read the value of an Angle object in either degrees or radians. Therefore, the use of Angle objects assures that correct values are used in calculations. This indirection is done due to an inconsistency of several APIs, for example, org.eclipse.swt.graphics.Transform vs. org.eclipse.draw2d.geometry.Transform.

// creates a 75% pie chart
Pie chart = new Pie(0, 0, 100, 100, Angle.fromDeg(15), Angle.fromDeg(270));

Vector

A Vector has two components x and y. It can be interpreted as a planar Point (toPoint()). The Vector class implements the common arithmetic operations for vectors: addition, multiplication with a scalar, dot product, cross product, and Angle calculation between two Vectors:

Vector u = new Vector(1, 0);
Vector v = new Vector(0, 1);
double zero = u.getDotProduct(v);

Straight

A Straight is an infinite planar line. You can build it up from either two Points which the Straight passes through, or by specifying a position and a direction Vector:

Straight diagonal = new Straight(new Point(1, 1), new Point(2, 2));
Straight diagonal = new Straight(new Vector(1, 1), new Vector(1, 1)); // exactly the same Straight

Projective

  • package: org.eclipse.gef4.geometry.projective

The Projective package provides classes (Vector3D, Straight3D) to represent euclidean elements in the projective plane.

Vector3D and Straight3D

Projective geometry is an interesting perspective to (planar) geometry. A point and a line can both be represented by a (x, y, z) triple. And, in fact, people speak of the duality between points and lines, because for any relation between points and lines, the inverse relation holds if you substitute point by line and vice versa. To retain the semantic distinction of points and lines, both concepts are separated in the Vector3D and Straight3D classes. You may wonder why 2-dimensional objects are specified by three components. This is the case, because a planar projective point is really a three dimensional euclidean line, that passes through the origin of the three dimensional coordinate system. The x, y, and z values are the components of the direction vector of that line. Similarly, a planar projective line is really a three dimensional euclidean plane, that contains the origin of the three dimensional coordinate system. The x, y, and z components specify that plane's normal vector. Notice that the z = 1 plane is considered to be the 2-dimensional plane. Therefore, a Vector3D with the components (x, y, z) represents a Point with components (x/z, y/z).

This approach leads to elegant mathematical solutions to some of the basic planar geometric operations. You want to know if a point lies on a line? Simply substitute its coordinate values into the Straight3D's formula. You want to know the distance of a point to a line? If the Straight3D's vector is normalized (a^2 + b^2 = 1), the formula evaluates to the signed distance of the point to the line, i.e. on one side of the line it is positive and on the other side it is negative. You want to know where two lines are intersecting? Simply compute the cross product of two (x, y, z) triples. These operations are packed in appropriately named methods.

Straight3D s = Straight3D.through(new Vector3D(s0), new Vector3D(s1)); // s0, s1 are Points
double signedDistance = s.getSignedDistanceCW(p); // Math.abs() => absolute distance
Straight3D r = Straight3D.through(new Vector3D(r0), new Vector3D(r1)); // r0, r1 are Points
Vector3D intersection = s.getIntersection(r);
Point poi = intersection.toPoint();

Vector3D

A Vector3D consists of three components (x, y, z). It represents the Point (x/z, y/z) in 2-dimensional space. You can use the common arithmetic operations for vectors on a Vector3D object:

Vector3D v = new Vector3D(1, 2, 3);
Vector3D u = v.getAdded(new Vector3D(3, -6, 1));
double zero = v.getDotProduct(u);

Straight3D

A Straight3D consists of three components (x, y, z). It represents the line ax + by + z = 0 in 2-dimensional space, where (a, b) is a planar Point. You can use a Straight3D to calculate the distance of a Vector3D to the Straight3D. Moreover, you can compute the point of intersection of two Straight3Ds:

Straight3D s = Straight3D.through(new Vector3D(1, 1, 1), new Vector3D(2, 1, 1));
double one = s.getSignedDistanceCW(new Vector3D(1.5, 2, 1));

Convert.AWT

  • package: org.eclipse.gef4.geometry.convert.awt

The Convert.AWT package contains helper classes to transfer data from AWT/Geometry to one another.

AWT/Geometry conversions

Geometry2AWT

Point, Line, Rectangle, RoundedRectangle, and AffineTransform objects can be transfered into their AWT pendants using the toAWT*() methods of the Geometry2AWT class:

RoundRectangle2D rr2d = Geometry2AWT.toAWTRoundedRectangle(rr);

AWT2Geometry

Correspondingly, the AWT2Geometry class provides methods to transfer AWT objects into GEF 4 Geometry objects:

RoundedRectangle rr = AWT2Geometry.toRoundedRectangle(rr2d);

Geometry.Convert.FX

  • feature: org.eclipse.gef4.geometry.convert.fx
  • bundle: org.eclipse.gef4.geometry.convert.fx

The Geometry.Convert.FX module of GEF4 Geometry contains helper classes for the conversion of JavaFX/Geometry objects to one another.


Convert.FX

  • package: org.eclipse.gef4.geometry.convert.fx

The Convert.FX package contains helper classes to transfer data from JavaFX/Geometry to one another.

JavaFX/Geometry conversions

Geometry2JavaFX

AffineTransform, Path, Point, and Rectangle objects can be transfered into their JavaFX pendants using the toFX*() methods provided by the Geometry2JavaFX class:

Rectangle rect = new Rectangle(10, 10, 100, 50);
javafx.geometry.Bounds rectFx = Geometry2JavaFX.toFXBounds(rect);

Additionally, the #toPathElements(Path) method allows the conversion of a Path object into JavaFX PathElements.

JavaFX2Geometry

JavaFX Bounds, Transform, Path, and Point2D can be transfered into their Geometry pendants using the to*() methods provided by the JavaFX2Geometry class.


Geometry.Convert.SWT

  • feature: org.eclipse.gef4.geometry.convert.swt
  • bundle: org.eclipse.gef4.geometry.convert.swt

The Geometry.Convert.SWT module of GEF4 Geometry contains helper classes for the conversion of SWT/Geometry objects to one another and SWT/AWT objects to one another.


Convert.SWT

  • package: org.eclipse.gef4.geometry.convert.swt

The Convert.SWT package contains helper classes to transfer data from SWT/Geometry to one another and from SWT/AWT to one another.

SWT/Geometry conversions

Geometry2SWT

Path, Point, Line, Polygon, Polyline, Rectangle, Region, and Ring objects can be transfered into their SWT pendants using the toSWT*() methods provided by the Geometry2SWT class:

Rectangle rect = new Rectangle(10, 10, 100, 50);
org.eclipse.swt.graphics.Rectangle rectSWT = Geometry2SWT.toSWTRectangle(rect);

Using the IGeometry#toPath() method, you can easily convert any GEF4 Geometry object into an SWT PathData representation:

CubicCurve curve = new CubicCurve(0, 0, 50, 0, 0, 50, 50, 50);
PathData pd = Geometry2SWT.toSWTPathData(curve.toPath());

SWT2Geometry

The SWT2Geometry class contains methods to transfer SWT objects into GEF4 Geometry representations.

SWT2AWT

The SWT2AWT class contains a toPathIterator() method which transfers an SWT PathData into an java.awt.geom.PathIterator.

PathIterator pathIterator = SWT2AWT.toPathIterator(pathData, windingRule);

AWT2SWT

The AWT2SWT class contains a toSWTPathData() method which transfers an java.awt.geom.PathIterator into an org.eclipse.swt.graphics.PathData. Consider that the winding rule of the AWT PathIterator is not kept in the SWT PathData, because the latter does not store this information. Instead, it is provided by an SWT Path or by the SWT GC that is used to draw the SWT PathData object:

PathData pathData = AWT2SWT.toSWTPathData(pathIterator);

Back to the top