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.
Difference between revisions of "GEF/GEF4/Geometry/Tutorial"
Line 51: | Line 51: | ||
//... | //... | ||
− | Rotation and scaling do always use a relative point to rotate around and scale away from, respectively. If you omit it, the transformation will use the mid-point of the shape as the relative point for the transformation. This behaviour is considered to be least surprising, although some people might expect the scaling and the rotation to be relative to (0, 0). | + | Rotation and scaling do always use a relative point to rotate around and scale away from, respectively. If you omit it, the transformation will use the mid-point of the shape as the relative point for the transformation. This behaviour is considered to be least surprising, although some people might expect the scaling and the rotation to be relative to (0, 0) which is true for Points and Vectors. |
+ | |||
+ | === Interaction of shapes === | ||
+ | |||
+ | In addition to shape construction and shape transformations, figures can interact with each other: You can test them for intersection or overlap, and you can compute the points of intersection of two shapes. | ||
+ | |||
+ | === REPL === |
Revision as of 07:18, 20 February 2012
Contents
Description
This is a small tutorial for the GEF4 Geometry API.
1. General behaviour
You can find all the shapes in the org.eclipse.gef4.geometry.shapes package. In the majority of cases, you will get along with those predeclared shapes. If you feel the need of another one that is not provided there, you can draw the joker (Path) which can be used to work with complicated composed shapes. But we will begin by demonstrating the other shapes first.
Shape construction
You are able to create a shape via its constructor:
Point p1 = new Point(100, 100); Point p2 = new Point(200, 200); Line line = new Line(p1, p2);
Alternatively, every constructor is able to handle primitive data types:
Line line = new Line(100, 100, 200, 200); Ellipse e = new Ellipse(100, 100, 100, 100); // x, y, w, h
Besides this constructor contract, each shape can bake a copy of its own via its getCopy()
method.
Line line2 = line.getCopy();
Transformations
If you want to transform a shape, there are several ways to do this. You may use...
- ...the general AffineTransform class using the
shape.getTransformed(transformation)
method - ...short-cut methods for the individual tranformations
You can easily rotate a Rectangle by calling its getRotated(angle)
method:
Rectangle quad = new Rectangle(100, 100, 100, 100); Polygon rhomb = quad.getRotated(Angle.fromDeg(45));
As you can see, the resulting shape is not a Rectangle anymore. This is due to the fact, that Rectangles are always parallel to the x- and y-axis.
If you want to rotate a Rectangle by an integer multiple of 90° and get a Rectangle back, you have to use the polygon.getBounds()
method on the Polygon that the getRotated(angle)
method returns:
Rectangle rect = new Rectangle(100, 100, 200, 50); Rectangle rotated = rect.getRotated(Angle.fromDeg(90)).getBounds();
Translation and scaling are directly available on each shape, too:
Rectangle rect = new Rectangle(100, 100, 200, 50); Rectangle big = rect.getScaled(2); Rectangle small = rect.getScaled(0.5); Rectangle translated = rect.getTranslated(-50, 50); //...
Rotation and scaling do always use a relative point to rotate around and scale away from, respectively. If you omit it, the transformation will use the mid-point of the shape as the relative point for the transformation. This behaviour is considered to be least surprising, although some people might expect the scaling and the rotation to be relative to (0, 0) which is true for Points and Vectors.
Interaction of shapes
In addition to shape construction and shape transformations, figures can interact with each other: You can test them for intersection or overlap, and you can compute the points of intersection of two shapes.