[R] to draw a smooth arc
(Ted Harding)
ted.harding at nessie.mcc.ac.uk
Tue May 1 15:50:48 CEST 2007
This thread prompts me to ask about something I've
been pondering for a while, as to whether there's an
implementation somewhere ticked away in the R resources.
So far, people have been responding to the original query
in terms of increasing the numbers of points, and joining
these by lines.
However, if you're using PostScript output, you can draw
really smooth curves by exploiting PS's "curveto" operator.
This draws a cubic-curve segment in the following way:
The two points you want to join with a curve will be denoted
by (X0,Y0) and (X3,Y3) in the following (for reasons which
will appear). The PS command is of the form
x1 y1 x2 y2 X3 Y3 curevto
At (X0,Y0) the tangent to the curve (as it departs from (X0,Y0)
is in the direction of the directed line from (X0,Y0) to (x1,y1),
and at (X3,Y3) (as it arrives) the tangent to the curve is
in the direction of the directed line from (x2,y3) to (X3,Y3).
The location of (X0,Y0) is not part of the command, since
it is implicit in the PS "currentpoint" which is the starting
point of the curve.
The result is (in theory, and in practice to within the resolution
of the output device) a perfectly smooth curve, provided the
consecutive cubic segments have the same tangent at each of
the points being joined. This can be achieved by appropriate
choice of the "intermediate" points -- (x1,y2), (x2,y2) above.
So far, when I've done this myself (including when using the
output from R to give the points being joined), I've done the
computation of the "intermediate" points "by hand". This basically
involves deciding, at each of the points being joined, what the
tangent to the smooth curve shouold be.
Of course, there is an element of arbitrariness in this, unless
there is an analytic representation of the curve on which the
points lie (e.g. you're plotting sin(x)/x every pi/8, and
want to join them smoothly), when all you need is the derivatives
at the points.
Crudely, you might evaluate the direction at a point in terms
os a weighted average of the directions to its two immediate
neighbours (the nearer meghbour ges the greater weight); less
crudely, you might fit a quadratic through the point and its
2 neighbours and use the gradient at the middle point; and so on.
Once you've decided on the tangent at each point, it's then
straightforward to compute suitable "intermediate points"
to serve as (x1,y2) and (x2,y2).
(One application where this sort of approach is needed is in
joining computed points on iso-contours, where the individual
points have been determined by interpolation of spot-measurements
at nearby measuring stations).
Anyway. The Question: is there a general function for the
above kind of smooth curve-drawing?
With thanks,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 01-May-07 Time: 14:50:38
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list