Server Graphics

Our library(vector) provides a couple of SVG primitives. To show a moon and its sunlit portion, we use the SVG path primitive and an Arc path element.

% moon(+Stream, +Float)
moon(S, N) :-
X is min(60,(N-2)*30), abs_flag(X, RX, SG),
X2 is min(60,(6-N)*30), abs_flag(X2, RX2, SG2),
svg_begin(S, [width(40),height(40)]),
svg_rect(S, 0, 0, 200, 160, 'empy'),
svg_circle(S, 100, 80, 60, 'dark'),
svg_path(S, ['M',100,20,'A',RX,60,0,0,SG,100,140,
'A',RX2,60,0,0,SG2,100,20,'Z'], 'light'),
svg_end(S).

% abs_flag(+Float, -Float, -Integer)
abs_flag(X, RX, 1) :- X < 0, !, RX is -X.
abs_flag(X, X, 0).

Thanks to the new library(markup) the SVG primitives can be automatically pretty printed to an output stream. So that we can use a Prolog query to inspect the generate SVG markup:

?- current_output(S), dom_output_new(S, T),
moon(T, 3.4556983476595633).
<svg style="width: 16.6667em; height: 13.3333em" viewBox="0 0 200 160">
<rect x="0" y="0" width="200" height="160" class="empy"/>
<circle cx="100" cy="80" r="60" class="dark"/>
<path d="M 100 20 A 43.671 60 0 0 0 100 140 A 60 60 0 0 0 100 20 Z" class="light"/>
</svg>

The SVG rendering is not discretized into 8 different pictures, instead the above code will gen-erate continuously varying graphics depending on the given floating point value.

Kommentare