Inly integer numbers are allowed. It is only necessary to build the
sinusoid in the [0;π/2] range. The points in the [π/2;2π] range can
be ensued by symmetry.
Let
(x1, y1) be the 'current'
point position. To estimate the sin increment, we will also need
another point
(x2, y2), moving towards
the first one from the opposite side of the range, starting
from (π/2;1). That second point will give us the estimation of the
function's derivative,
cos
x. Let
dx
and
dy be
the scale, in pixels. We need therefore to plot the function
f(x)=dy*sin(x1/dx). The starting
values are (x
1, y
1):=(0,0)
and (x
2, y
2):=(dx*π/2,dy)
(note here that we can replace Pi/2 by a fraction 15708/10000). The two
points will move towards each other until they meet. At any moment of
time, the following condition will hold:
x1=dx*Pi/2-x2.
At each step, we should move the first point in one of the following
directions:
- several pixels up and right
- one pixel right
Which of the directions should we choose? Consider the coordinate plane:
It is obvious that the current point position
y1 will slightly
deviate from the actual sine value
f(x1), because we use
integers. Let
d1 be that deviation,
scaled from 0 to
dx:
d1=dx*(f(x1)-y1).
d1 is positive if
f(x1) is above
y1
(the case you see on picture). By moving the first point one pixel
right, we increase
x1 by one and
therefor increase
d1 by
y2. Indeed,
d1(x1+1)-d1(x1)
= d1'(x1)
+ e = (dx*(f(x1)-y1))' +
e = dx*dy*sin'(x1/dx) + e = f(dx*π/2-x1)
+e =
f(x2) +e = y2 + d2/dx
+ e
where
d1' is the first
derivative by
x, d2
is the deviation of
y2 from
f(x2) scaled by
dx, and
e is the error.
According to Taylor's series for
f(x),
|e| ≤
|½*d1''(x1)|=|½*dx*(f(x1)-y1)''|=|½*dx*dy*sin''(x
1/dx)
|
≤ ½*dy/dx. For large
scale (i.e. dx and dy), we can disregard
d2/dx + e and take
y2 as the
increment for
d1.
At each step we increase
d1
by
y2
and decide if we should move up. If
f(x1+1) is closer to
y1+1
than to
y1,
we increment
y1
and decrement
d1
by
dx
(remember that
d1
is scaled by
dx).
We increment
y1
until
|d1| ≤
dx/2,
thus choosing the point, closest to
f(x1+1). Using the same
principle, we move the second point,
(x2, y2),
one step left and zero or more steps down, relying on -
y1/dx
as the estimation of
f '(x2).
Hence we decrease
d2
by
y1
when moving one pixel left and increase it by
dx when moving one
pixel down..
We move points until
|x1-x2|+|y1-y2| ≤
2, after which the part of sinusoid between 0
and π/2
can be considered complete.