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

GEF/GEF4/Geometry

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

This is the reference documentation of the GEF 4 Geometry API. You can use this component independent of the other GEF 4 components. The Geometry API provides classes to store geometric objects and to do geometric computations on those objects. It is developed to be intuitively usable. Nonetheless, it is of no harm to know the underlying design decisions which are explained in the following chapters.

Geometric Primitives

Angle, Dimension, and Point

The API uses a small number of "primitives" which are globally used to build up, introspect and modify complex objects. These primitives are the Point, Angle and Dimension classes. Presumably, the most important class of the three is the Point class, because you will encounter it very often while working with the API. Point objects represent a point in two dimensional space.(1) From a list of Point objects, you can build up most of the planar geometry 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);

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. Keep in mind that the GEF 4 Geometry API is not yet finished. Maybe, this indirection will be eliminated in a future version.

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

The Dimension class is the pendant of the draw2d.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)
);

(1) 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.

Planar Geometry

Interface hierarchy

This diagram depicts the interface hierarchy which underlies the individual geometry classes. It classifies the geometry classes mainly into either being ICurves or IShapes. 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 Bézier curves. On the other hand, an IShape is a two dimensional geometry, i.e. it continuously encloses a region on the drawing area, without holes. You can retrieve the outline of an IShape, which is an IPolyCurve, a special case of an ICurve. It defines a curve that is composed of multiple connected ICurves and its purpose is to be able to operate on them as a whole. Similarly and especially important for clipping is another set of planar geometries, the IPolyShapes. Other than the relationship between ICurve and IPolyCurve, an IPolyShape is not an IShape. An IPolyShape is a (possibly) non-continuous set of IShapes. An example for an IPolyShape is the Region. A Region is the area that results from composing multiple Rectangles. It corresponds to the SWT Region.

This interface hierarchy structures the geometry API. It describes how the individual classes are related to each other.

Important functionality

The most important part of the geometry API is about the relations between the different classes and how to transfer objects of one type to a number of objects of other types. The most general type in the hierarchy is the Path, because every geometric object can be transformed into it. But 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. So the most basic type in the hierarchy is the BezierCurve, because every geometric object can be transformed into a number of BezierCurves and from a list of BezierCurves you can construct every geometric object (excluding the Path).

An important part of a geometry API is the possibility to test the relationship of two geometry objects. The GEF 4 Geometry API provides four methods that perform relation tests. Universally usable is the touches() method for planar geometry objects. It tests if two objects have at least one point in common. Additionally, ICurves can be tested for intersections 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 geometry object. Moreover, the point test is available for arbitrary planar geometry objects. It tests if a given Point is incidental to the particular geometry object.

Supplementary to the intersects() test, a getIntersections() method is offered among ICurves. BézierCurves do also facilitate the extraction of overlapping segments via the getOverlap() method.

To achieve the full functionality of the API, you have to be aware of how the different planar geometry objects and their implemented interfaces are linked together. The fundamental ICurve is the BezierCurve, because every ICurve can be approximated by a set of continuous BezierCurves. This approximation can be received using the toBezier() method. The outline of an IShape always is an ICurve. You can retrieve it by using the getOutline() method on the particular shape. Furthermore, any planar geometry object can be transformed into a Path object by using its toPath() method.

The Path is special. It can hold any number of curves and shapes. But it does not implement the related interfaces. Therefore, you should only use the Path in situations where you really need it, because it does not provide such a rich interface as the other parts of the API.

So, let us consider a few examples.

  1. Compute the points of intersection between a Line l1 and another Line l2:
    Point[] intersections = l1.getIntersections(l2);
  2. Compute the points of intersection between a Line l and a Polygon p:
    Point[] intersections = l.getIntersections(p.getOutline());
  3. Compute the points of intersection between a Polygon p1 and another Polygon p2:
    Point[] intersections = p1.getOutline().getIntersections(p2.getOutline());

Inheritance hierarchy

This diagram depicts the inheritance hierarchy which underlies the individual geometry classes. It classifies the classes by their construction type, so that many operations are generalized in a few abstract classes. Additionally, the different planar geometry objects provide extra functionality, for example, computing the area of a Polygon, or unifying two Rectangles.

IGeometry

ICurve

IPolyCurve

IShape

IPolyShape

Line

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

Rectangle

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

Polyline

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

Polygon

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

Ellipse

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

Arc

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

Pie

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

RoundedRectangle

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

QuadraticCurve

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

CubicCurve

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

BezierCurve

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

PolyBezier

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

Region

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

Ring

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

Path

  • extends: AbstractGeometry

Euclidean Geometry

Vector and Straight

Projective Geometry

Vector3D and Straight3D

Affine transformations

Affine transformations

As you can see in the overview diagram, you can either transform your geometrical objects via instances of the AffineTransform class, or by using the short-cut methods provided by the ITranslatable, IScalable, IRotatable and IRotatableInPlace 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.

Translation

Translating an object means moving the object. You can move an object in x- and y-direction. The associated distances can be passed to the translate(...)/getTranslated(...) methods either via two double precision floating point numbers, or via a Point object.

Scaling

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 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

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 a Rectangle does only provide the getRotated() short-cut methods for rotation. 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.

Shearing

The short-cut methods for shearing are not yet implemented.

Conversions

Conversions

From Geometry to SWT

via the toSWT...() methods

AWT2Geometry

Geometry2AWT

SWT2AWT

Tutorial

This is the documentation of the GEF4 Geometry API. You can find a small tutorial for the API here.

Back to the top