Section 3.11 Shape constructions and clipping
This section introduces shapes and some things you can do with them. A shape is any graphical component that defines a two-dimensional region, such as a
<circle>
, <rectangle>
, <polygon>
, <path>
, or <spline>
. The tags <graph>
or <parametric-curve>
also define shapes as, behind the scenes, PreFigure will close the paths they define.Subsection 3.11.1 Defining shapes
Shapes are defined within a special
<define-shapes>
tag, which make them available for later use. Figure 3.11.1, and the accompanying PreFigure source in Listing 3.11.2, provides a simple example.<shape>
tag.<diagram dimensions="(300,180)" margins="5">
<coordinates bbox="(0,0,10,6)">
<define-shapes>
<circle at="A" center="(4,3)" radius="2"/>
</define-shapes>
<shape shape="A" fill="magenta" stroke="black"/>
<rectangle lower-left="(0,0)" dimensions="(10,6)" stroke="black"/>
</coordinates>
</diagram>
Note the use of the
<define-shapes>
tag in lines 3 through 5. Within this tag, we define a circle with the handle @at="A"
. Any @stroke
or @fill
attributes will be ignored in the definition since we mean to focus solely on the geometric shape. Also, it is important that shapes be defined within a <coordinates>
tag as the definitions become invalid once we leave this coordinate system.We then recall the shape in line 7 and supply attributes for filling and stroking it within that element. Notice that the attribute
@shape="A"
serves as a reference to the previously defined shape. As we will see, we sometimes want to recall two or more shapes at once, which we could do with a comma-separated list. For this reason, PreFigure accepts either the attribute @shapes
(note the plural) or @shape
as a convenience.Subsection 3.11.2 Shape Constructions
Of course, we could easily have created the diagram in Figure 3.11.1 with a single
<circle>
tag. Shapes, however, provide us with access to some set operations, such as intersections, unions, and differences. For example, Figure 3.11.3 demonstrates how we can define the difference of two sets \(A\setminus B\text{.}\)
<diagram dimensions="(300,180)" margins="5">
<coordinates bbox="(0,0,10,6)">
<define-shapes>
<circle at="A" center="(4,3)" radius="2"/>
<circle at="B" center="(6,3)" radius="2"/>
</define-shapes>
<shape shapes="A,B" operation="difference"
fill="magenta" stroke="black"/>
<shape shape="A" stroke="black"/>
<shape shape="B" stroke="black"/>
<rectangle lower-left="(0,0)" dimensions="(10,6)" stroke="black"/>
<label anchor="(2,3)" alignment="nw"><m>A</m></label>
<label anchor="(8,3)" alignment="ne"><m>B</m></label>
<label anchor="(5,0.5)"><m>A\setminus B</m></label>
</coordinates>
</diagram>
In this example, we define two sets \(A\) and \(B\) in the
<define-shapes>
tag. We then construct and display their intersection using the <shape>
tag in lines 8 and 9. Notice that we use the @shapes
attribute to recall the two shapes (though @shape="A,B"
would also suffice). The attribute @operation="difference"
declares that this set should be constructed as the difference of the two sets. In this case, @shapes="B,A"
would yield the difference \(B\setminus A\text{.}\)
Other operations include
@operation="union"
, @operation="intersection"
, and @operation="symmetric-difference" or "sym-diff"
. These four operations are demonstrated in Figure 3.11.5.Intersections, unions, and symmetric differences can take any number of sets, while a difference requires exactly two.
A fifth operation is
@operation="convex-hull"
, which produces the convex hull of the union of the given shapes. Figure 3.11.6 includes a demonstration, with the convex hull of two sets outlined in red. This is not an especially realistic diagram, but it does demonstrate some of the possibilities when working with shapes.<diagram dimensions="(300,300)" margins="5">
<coordinates bbox="(0,0,10,10)">
<define-shapes>
<circle at="circle" center="(6,7)" radius="2"/>
<rectangle at="lower-rectangle"
center="(4,4)" dimensions="(5,4)"
corner-radius="10"/>
<rectangle at="upper-rectangle"
center="(4,7.5)" dimensions="(5,1)"
corner-radius="10"/>
<shape shapes="circle,lower-rectangle,upper-rectangle"
operation="sym-diff"
at="sym-diff"/>
<circle at="right-circle"
center="(8.5,8.5)" radius="1"/>
</define-shapes>
<grid-axes decorations="no"/>
<shape shapes="sym-diff,right-circle" operation="convex-hull"
stroke="red" thickness="8"/>
<shape shapes="sym-diff" fill="magenta" stroke="black"/>
<shape shapes="circle" stroke="black"/>
<shape shapes="upper-rectangle" stroke="black"/>
<shape shapes="lower-rectangle" stroke="black"/>
<shape shape="right-circle" fill="blue" stroke="black"/>
</coordinates>
</diagram>
Subsection 3.11.3 Clipping
A second use of shapes is to clip a diagram with the
<clip>
tag. Figure 3.11.8 provides a demonstration. Notice that the clipping shape is applied only to elements that are children of the <clip>
tag.<clip>
tag.<diagram dimensions="(300,300)" margins="5">
<coordinates bbox="(0,0,10,10)">
<define-shapes>
<rectangle at="square1"
center="(4,4)" dimensions="(3,3)"
corner-radius="10"/>
<rectangle at="square2"
center="(3,6)" dimensions="(2,2)"
corner-radius="5"/>
<shape at="clip" shapes="square1, square2"
operation="union"/>
<circle at="circle"
center="(6,6)" radius="3"/>
</define-shapes>
<shape shape="circle" fill="lightgray" stroke="gray"/>
<clip shape="clip">
<shape shape="circle" fill="blue" stroke="black"/>
</clip>
<shape shape="clip" stroke="black"/>
<rectangle lower-left="(0,0)" dimensions="(10,10)"
stroke="black"/>
</coordinates>
</diagram>