polarPath
elementIn SVG 1.1 path commands are only available in cartesian
coordinates. For several problems polar coordinates are more convenient for authors
and the number of list items in the values
attribute
of an animation of path data for such problems can be dramatically reduced.
Therefore the concept of Bézier curves is simply extended to polar coordinates
with this proposal for the polarPath
element or alternatively additional
commands for the path
element are introduced.
polarPath
element - general remarks
The polarPath
element corresponds to the path
elements in SVG 1.1
with the same attributes and behaviour, only the command glyphs and their
meaning is changed to provide polar coordinates. Other glyphs as for the path
d
attribute are used only to allow to join the commands for an alternative proposal.
Commands are available in small letters for relative coordinates and in large
letters for absolute coordinates as for the path
element. The polar coordinates
are given the radial component first, then the angular component in degrees.
In the following the radial component is called r and the angular component φ.
The run parameter for a single fragment is u from 0 to 1. With the cartesian coordinate
pair given by the P/p command (px,py) the transformation
to coordinates (x,y) in the user coordinate system is as follows:
x = r cos(φ) + px
y = r sin(φ)+ py
d
attribute of the polarPath
element
The P/p command provides the center point or origin for the polar coordinates following
after this command. This center is given in cartesian coordinates, for example P 100 200
means a center at (100,200) in the user coordinate system. The
P/p indicates the start of a new subpath. If a polarPath
is provided without a
P/p before other polar commands, the center is assumend to be (0,0).
The N/n 'next' commands correspond exactly to the M/m of the path
element and
means a move to the given position and the start of a new subpath. If multiple
coordinate pairs follow after an N/n command, the subsequent pairs shall be treated
as implicit I/i commands (see below). If the N/n command is missing before other polar coordinates,
the initial coordinate pair is assumed to be '0 0', but authors should always provide
an initial coordinate pair.
The I/i commands provide a possibility for a simple affine spiral fragment.
If (ra, φa) is the current point and
(re, φe) the coordinate pair after the I
command, this describes a curve with the following parametrisation
with the run parameter u from 0 to 1:
(r,φ)(u) = (1 - u) (ra, φa) + u (re, φe).
For the i command the current point ra, φa is added to
the relative coordinates as usual before this parametrisation.
If more than one coordinate pair is specified, a polyspiral shall be drawn.
At the end of the command, the new current point shall be set to the final set of
coordinates provided.
The J/j commands provide a possibility for a quadratic Bézier fragment.
Two coordinate pairs are expected to follow this command or a multiple set
of two coordinate pairs.
If (ra, φa) is a current point and
(rc, φc) the control point given directly after
the J/j, respectively the first coordinate pair of such a doublet and
(re, φe) the second coordinate pair,
this describes a curve with the following parametrisation
with the run parameter u from 0 to 1:
(r,φ)(u) = (1 - u)2 (ra, φa)
+ 2u(1 - u) (rc, φc) +
u2 (re, φe).
For the j command the current point ra, φa is added to
the relative coordinates as usual before this parametrisation.
If more than one coordinate pair set is specified, a polycurve shall be drawn.
At the end of the command, the new current point shall be set to the final set of
coordinates provided.
The K/k commands provide a possibility for a cubic Bézier fragment.
Three coordinate pairs are expected to follow this command or a multiple set
of three coordinate pairs.
If (ra, φa) is a current point and
(rc, φc) the first control point given directly after
the K/k, respectively the first coordinate pair of such a triplet,
(rd, φd) the second control point and
(re, φe) the third coordinate pair,
this describes a curve with the following parametrisation
with the run parameter u from 0 to 1:
(r,φ)(u) = (1 - u)3 (ra, φa)
+ 3u(1 - u)2 (rc, φc) +
+ 3u2(1 - u) (rd, φd) +
u3 (re, φe).
For the k command the current point ra, φa is added to
the relative coordinates as usual before this parametrisation.
If more than one coordinate pair set is specified, a polycurve shall be drawn.
At the end of the command, the new current point shall be set to the final set of
coordinates provided.
The F/f is the short form of the quadratic Bézier fragment corresponding to the T/t command for cartesian coordinates.
The G/g is the short form of the cubic Bézier fragment corresponding to the S/s command for cartesian coordinates.
The R/r corresponds to the Z/z command, but with this command the polar path is
closed with an affine spiral fragment using the first coordinate pair of the current subpath
as the final pair the spiral is drawn to.
Due to the plurivalences of the conversion from polar coordinates to cartesian coordinates,
there are several ways to draw a spiral to the first coordinate pair, therefore R/r have up
to four additional parameters, lets call them p0,
p1, p2, p3.
Not provided parameters are assumed to be 0. Authors are recommended to use only integers for the first two parameters
and only 0 or 1 for the other two, see formulas below, if something else is provided, this is converted into an integer
(floor is the next integer equal or smaller than the given number).
The command has the task to draw a spiral from the current point to the initial point of a subpath.
The current angle ψ for drawing is determined from the current angle φc
in the following way: ψ = φc + floor(p0)*360 degree.
The initial angle ι for drawing is determined from the initial angle φi in the
following way: ι = φi + floor(p1)*360 degree.
The third and fourth parameter are intended to change the sign of r before the spiral is drawn.
The current r is determined from the current rc in the following way:
r = - rc and φ = ψ -180 degree, if the third parameter is not 0, respectively
the initial rin = - ri and
φin = ι -180 degree, if the fourth parameter is not 0.
If the third respectively fourth parameter is 0, the related coordinate pair remains unchanged.
The following scripts show (random) samples for polar coordinate path fragments,
for affine, quadratic and cubic curves with an approximation with the current
cartesian path
element command for cubic Bézier curves. Due to the
typically friendly behaviour of Bézier curves such an approximation
is easy to calculate automatically with a good quality. If no other ideas for
a better rendering strategy is available, such an approximation can be used to display such
path fragments given in polar coordinates. The desc
elements of the script output give more details (in german and english)
about available GET parameters to generate specific non random samples.
The approximation is given in red and green, for a good approximation the
green curve covers the red curve. Gray text indicates the GET parameters, green
text the path command for the approximation in cartesian coordinates.
Affine spiral (source code of affine spiral).
Quadratic polar fragment (source code of quadratic polar fragment).
Cubic polar fragment (source code of cubic polar fragment).
The parsing of the attribute value and the combination of commands can be tested with the following script. It includes some more path samples: test polarPath (source code for the module to create the path approximation of the polarPath).
Alternatively to a new element polarPath
the current path
element
can be extended using the commands above. This requires sometimes a transformation of the current
point (x,y) from cartesian to polar coordinates. Because r is not restricted to be not negative and φ is
not limited to a restricted range, the P/p commands have to be expanded with another two parameters,
one is the number of turns to be added to the angle calculated below and one boolean flag to indicate, whether
r is to be taken positive or negative. Therefore the P/p commands have four parameters:
P px py t fr
fr is either 0 or 1. Everything else is converted to 1.
First (px,py) is substracted:
(sx,sy)= (x,y) - (px,py)
r = (2fr -1)*(sx2 + sy2)1/2
φ = 0 if r is 0 (yet another parameter could be used to define this by the author, if required), else
φ = v acos(sx/r) + t*360; v is 1, if (2fr -1)*sy is not negative, else v is -1.
A third approach could be to use no new commands at all, only the P/p commands to indicate a switch between
cartesian coordinates and polar coordinates. To be able to switch back to cartesian coordinates, one could add
another boolean flag fc to the P/p commands:
P px py t fr fc
fc is either 0 or 1. Everything else is converted to 1.
fc=1 indicates, that the following path data are in cartesian coordinates, fc=0 indicates, that they are in
polar coordinates. No P/p at all means cartesian coordinates.
Typically, if it is more convenient for the author to solve a problem in polar coordinates, there is no need to
switch between cartesian and polar coordinates, therefore it is no big problem to have two elements and
two elements are better for backward compatibility, because old user agent can at least display the
path
element. Using only one element leads to more flexibility for the author, even if it
might not happen often to switch between both systems. However, in polar coordinates are only radial lines
simple to note, but sometimes an author may need other lines too. Having only the P/p commands new
mainly to switch between the systems is a minimalistic and elegant solution, but authors have to pay attention, that
the meaning of the same commands and coordinate pairs depends strongly on the previous P/p command,
other commands or the coordinates themselves do not indicates the change.
With these commands, especially with I/i an Archimedes spiral can be directly specified, the I/i commands already cover a larger group of curves, however these new commandes do not cover other specific kinds of spirals without further calculation or approximation, for example Lituus, logarithmic spiral, hyperbolic spiral, a spiral following an arbitrary power law or a clothoid is not covered directly, but except for the clothoid a notation in polar coordinates simplifies already the problem.
Concerning animation of path data, it requires always many list items in the values
attribute
of an animation, if cartesian coordinates have to be used for a problem more related to polar coordinates, to get
a sufficient approximation. With this proposal such huge data sets can be avoided at least for this class of problems.
If the display in the user agent is generated with a conversion back to cartesian coordinates, it is of course important,
that this is done for each animation interpolation step to be displayed, not only for the values
list items, which are only the end points of the interpolation.
polar
, a simple method to specify regular and irregular polygons, stars, flowers, fancy polar objects, to explore two dimensional rotation symmetry and broken symmetrypath
s chapter of SVG 1.1