Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do you draw sine waves?

81 views
Skip to first unread message

StanleyTakata

unread,
Mar 17, 2001, 2:13:26 PM3/17/01
to
Hi there.
Is there an elegant way to create sine waves in PostScript?
I have tried to approximate them with "arct" but the results were not nice. My
final, unattractive, solution was to break the curve into line segments:
=======================================
0 0 moveto
0 .001 1
{
/x exch def
x 360 mul
sin
/y exch def
x 500 mul
y 100 mul
lineto
}
for
stroke
=======================================

Please let me know if there's a better way.
Thanks,
Steve

Spehro Pefhany

unread,
Mar 17, 2001, 2:36:22 PM3/17/01
to
The renowned StanleyTakata <stanle...@cs.com> wrote:
> Hi there.
> Is there an elegant way to create sine waves in PostScript?


Well, Bezier curves (curveto) are the obvious way, and you won't need many
segments. I've forgotten how I did it the last time, I *think* I created a
sine wave as below, imported it into Illustrator and used a built-in
function (or was it streamline?) to fit it, then fiddled that into
a Postscript function. That was expedient but inelegant. You could
probably do this numerically as well.

Best regards,
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Spehro Pefhany --"it's the network..." "The Journey is the reward"
sp...@interlog.com Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog Info for designers: http://www.speff.com
Contributions invited->The AVR-gcc FAQ is at: http://www.BlueCollarLinux.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Dave Blake

unread,
Mar 17, 2001, 4:48:36 PM3/17/01
to
StanleyTakata <stanle...@cs.com> wrote:
> Hi there.
> Is there an elegant way to create sine waves in PostScript?

No.

Use the following for high quality results
0. 1 setlinecap 1 setlinejoin
1. Make a digitized array of the x and y coords.
2. Use curveto to connect them

The rest, as they say, can be obtained in the PLRM.

--
Dave Blake
dbl...@phy.ucsf.edu

Toby Thain

unread,
Mar 18, 2001, 4:46:12 AM3/18/01
to
Dave Blake wrote:
>
> StanleyTakata <stanle...@cs.com> wrote:
> > Hi there.
> > Is there an elegant way to create sine waves in PostScript?
>
> No.

This answer so patently lacks utility that I was inspired to hack up the
attached Bezier approximation, which uses the first derivative and the
"thirds" rule-of-thumb to position Bezier anchors. There are much more
elegant approximations available, in fact you should be able to
approximate the first quadrant of a sine wave with just one Bezier with
sufficient accuracy for many purposes, but this was much quicker to
hack.

You should even be able to open it in Illustrator. Any questions e-mail
me.

regards
Toby

sine.ps

Toby Thain

unread,
Mar 18, 2001, 5:10:44 AM3/18/01
to
Toby Thain wrote:
>
> Dave Blake wrote:
> >
> > StanleyTakata <stanle...@cs.com> wrote:
> > > Hi there.
> > > Is there an elegant way to create sine waves in PostScript?
> >
> > No.
>
> ...I was inspired to hack up the
> attached Bezier approximation...

>
> You should even be able to open it in Illustrator.

After testing "sine.ps" in Illustrator 8 - it works - I found that the
approximation is very accurate indeed. A step size of 1.0 radian is
almost indistinguishable from a step size of 0.01 radian, using the
Beziers. I would therefore describe this as an "elegant" solution. Note
that making the step size an integral fraction of a radian (e.g. pi/8)
will result in a path which can be joined end-to-end to make a sine wave
of any length.

If you redistribute or use "sine.ps", please acknowledge my authorship
(see revised file attached).

The code should also be adaptable to any similar function for which the
first derivative is known analytically (f'x).

regards
Toby

sine.ps

Toby Thain

unread,
Mar 18, 2001, 10:51:32 AM3/18/01
to
Toby Thain wrote:
>
> Toby Thain wrote:
Further improvements yield the attached.

Step size of pi/4 seems a decent tradeoff, try pi/8 for more accuracy.

>
> regards
> Toby

sine.ps

Alexei Kournossenko

unread,
Mar 19, 2001, 10:57:51 AM3/19/01
to
%!
% Here are 2 versions.
% In the first one the whole period is
% approximated by 1 Bezier curve.

% In the second --- exact sine with 6 degrees step.
% The demo shows coincidence.


/Swave1 % N Period Amp Swave1 +currentpoint context
{ 12 sqrt mul 2 copy exch 0.4673 mul exch % 0.4673=(8-3*sqrt(3))/6
3 index 2 index sub 4 -1 roll neg 5 -1 roll 0 7 -1 roll
1 sub {6 copy rcurveto} repeat rcurveto stroke
} def

/Swave2 % N Period Amp Swave2 +currentpoint context
{ exch 60 div 0 6 dup 6 -1 roll 360 mul
{sin 3 index mul dup 3 -1 roll sub 2 index exch rlineto
} for pop pop pop stroke
} def

% Demo

/Helvetica findfont 12 scalefont setfont
10 400 80. 100. rectstroke

1 0 0 setrgbcolor [7 7] 0 setdash
10 510 moveto (False (Bezier) sine wave) show
10 400 moveto 10 80. 100. Swave1

0 0 1 setrgbcolor [7 7] 7 setdash
10 270 moveto (True sine wave) show
10 400 moveto 10 80. 100. Swave2

% Alexei Kournossenko, E-mail: Alexei.Ko...@cern.ch
% CERN, Division EST Tel: +41.22.767-4326
%----------------------------------------------------------------

Alexei Kournossenko

unread,
Mar 19, 2001, 12:07:44 PM3/19/01
to
% (continued)
%
% or, wothout extra PS-showing-off, just
%
/Swave0 % N(integer) Period Amp Swave0 +currentpoint context
{ 2 copy scale 1 exch div exch 1 exch div exch 3 -1 roll
{0.4673 3.4641 0.5327 -3.4641 1 0 rcurveto} repeat scale stroke
} def
%

--
Alexei Kournossenko, E-mail: Alexei.Ko...@cern.ch

CERN, Division EST Tel: +41.22.767-4326

CH-1211 GENEVE 23 SUISSE/SWITZERLAND Fax: +41.22.767-8890

0 new messages