QuickDraw en Maple Le paquetage quickdraw est conçu pour offrir

Transcription

QuickDraw en Maple Le paquetage quickdraw est conçu pour offrir
QuickDraw en Maple
Le paquetage quickdraw est conçu pour offrir des fonctionnalités graphiques de base (genre Logo) en
Maple. Les fonctions et types disponibles sont lineTo, moveTo, setColor, flushPlot, point, type/Point. Chacune
des fonctions ou type discutés ci-dessous peut-être appelé soit individuellement par quickdraw[fonction], soit
en chargeant l’ensemble du paquetage par with(quickdraw) :, puis par un appel direct à la fonction.
Le type de base quickdraw est le type quickdraw[‘type/Point‘], ou Point une fois que quickdraw a été
chargé. Attention : il ne faut pas confondre le type Point de quickdraw avec le type point de Maple, qui est
très différent.
Un point est un tableau de deux éléments, créé par un appel à la fonction point(x, y), qui prend en
argument deux expressions qui s’évaluent en des nombres réels (par exemple sqrt(3)/2, 1/2, 1.0, mais pas
sqrt(-1)). Inversement, on accède à l’abscisse d’un point P par P [1], à son ordonnée par P [2].
Le dessin proprement dit se fait simplement en donnant des directives de déplacement de crayon en
traçant (lineTo) ou crayon levé (moveTo). La couleur du crayon peut être changée par la fonction setColor.
Le crayon est initialement placé au point point(0, 0). La couleur initiale est rouge.
- la fonction lineTo(P) prend en argument un point et trace un trait de la couleur courante entre le
point courant et P . Le nouveau point courant est P .
- la fonction moveTo(P) déplace le crayon en P, sans tracer de trait. Le nouveau point courant est P .
- la fonction setColor(c) change la couleur courante en c ; le point courant reste le même. Les couleurs
courantes sont définies par leur nom pour les plus usuelles (voir la liste dans ?plot,color ), ou par une expression
du type COLOR(RGB, x, y, z), ou x, y, z sont des nombres réels de [0, 1] indiquant la proportion de rouge,
vert et bleu.
- la fonction flushPlot() s’utilise une fois le tracé entièrement fait, et affiche le tracé courant.
Un exemple : tracé de la courbe du dragon d’ordre n entre les points P 1 et P 2 :
with(quickdraw):
dragon_rec := proc(P1::Point, P2::Point, n::integer)
local P3;
if n = 1 then
moveTo(P1);
lineTo(P2);
else
P3 := point((P1[1]
(P2[2]
dragon_rec(P1, P3,
dragon_rec(P2, P3,
fi
end:
+ P2[1] + P2[2] - P1[2])/2,
+ P1[2] - P2[1] + P1[1])/2);
n-1);
n-1);
dragon := proc(P1 :: Point, P2 :: Point, n ::integer)
dragon_rec(P1, P2, n);
flushPlot();
end:
Quickdraw 1.0 a été réalisé par Guillaume Hanrot (INRIA et X), 11/2001, avec l’aide et les commentaires
de Jean-Jacques Lévy et Bruno Salvy. Les commentaires, bug-reports, suggestions sont à adresser à Guillaume
Hanrot ([email protected]). Le paquetage quickdraw peut être librement redistribué et utilisé tant qu’il est
accompagné de ce fichier.
Implémentation de QuickDraw
####
# Bibliotheque de fonctionnalites graphiques de base en Maple pour le
# concours de l’Ecole polytechnique.
#
# G. Hanrot (INRIA-X) 11/2001, [email protected]
# http://www.loria.fr/~hanrot
#
# Cette bibliotheque peut etre redistribuee et modifiee librement des lors
# que les 8 lignes qui precedent l’accompagnent.
####
#
# Fonctionnalites exportees : quickdraw[lineTo](), quickdraw[moveTo](),
#
quickdraw[setColor](), quickdraw[flushPlot]().
#
quickdraw[point]() et type Point.
#
# Pour utiliser : with(quickdraw):
#
#
quickdraw[’version’] := 1.1:
#######
# Initialisations
#
# En toute rigueur, il faudrait definir ‘type/quickdraw/Point‘, mais c’est
# peu pratique, donc on cree un type global.
#
‘type/Point‘ := [numeric, numeric]:
‘quickdraw/__X_PLOT_BUFFERSIZE‘ := 10000:
‘quickdraw/__X_PLOT_LIST_POINTS‘ := []:
‘quickdraw/__X_PLOT_SET_PLOTS‘ := {}:
‘quickdraw/__X_PLOT_BUFFER_POINTS‘ :=
array(1..‘quickdraw/__X_PLOT_BUFFERSIZE‘):
‘quickdraw/__X_PLOT_BUFFER_PLOTS‘ := array(1..‘quickdraw/__X_PLOT_BUFFERSIZE‘):
‘quickdraw/__X_PLOT_DEFAULT_COLOR‘ := red:
‘quickdraw/__X_PLOT_POINTS_POSITION‘ := 1:
‘quickdraw/__X_PLOT_PLOTS_POSITION‘ := 1:
‘quickdraw/__X_PLOT_CURRENT_POINT‘ := [0.0, 0.0]:
############
# Bascule les plots deja effectues du buffer vers l’ensemble a tracer.
#
‘quickdraw/__x_plot_flush_plot_buffer‘ := proc()
local i;
global ‘quickdraw/__X_PLOT_SET_PLOTS‘,
‘quickdraw/__X_PLOT_BUFFER_PLOTS‘,
‘quickdraw/__X_PLOT_PLOTS_POSITION‘;
‘quickdraw/__X_PLOT_SET_PLOTS‘ := { op(‘quickdraw/__X_PLOT_SET_PLOTS‘),
seq(‘quickdraw/__X_PLOT_BUFFER_PLOTS‘[i],
i=1..(‘quickdraw/__X_PLOT_PLOTS_POSITION‘ - 1)) };
‘quickdraw/__X_PLOT_PLOTS_POSITION‘ := 1;
NULL;
end:
#############
# Bascule les points du buffer vers la liste de points.
#
‘quickdraw/__x_plot_flush_point_buffer‘ := proc()
global ‘quickdraw/__X_PLOT_LIST_POINTS‘,
‘quickdraw/__X_PLOT_BUFFER_POINTS‘,
‘quickdraw/__X_PLOT_POINTS_POSITION‘;
local i;
__x_plot_print_all();
‘quickdraw/__X_PLOT_LIST_POINTS‘ := [ op(‘quickdraw/__X_PLOT_LIST_POINTS‘),
seq(‘quickdraw/__X_PLOT_BUFFER_POINTS‘[i],
i = 1..(‘quickdraw/__X_PLOT_POINTS_POSITION‘-1))];
‘quickdraw/__X_PLOT_POINTS_POSITION‘ := 1;
NULL;
end:
#############
# Flushe le plot courant (quickdraw[moveTo] ou changement de couleur).
#
‘quickdraw/__x_plot_flush_current_plot‘ := proc()
global ‘quickdraw/__X_PLOT_POINTS_POSITION‘,
‘quickdraw/__X_PLOT_LIST_POINTS‘,
‘quickdraw/__X_PLOT_PLOTS_POSITION‘,
‘quickdraw/__X_PLOT_BUFFERSIZE‘,
‘quickdraw/__X_PLOT_BUFFER_PLOTS‘;
#Le buffer de quickdraw[point]s est-il vide ou non ?
if ‘quickdraw/__X_PLOT_POINTS_POSITION‘ <> 1
or nops(‘quickdraw/_X_PLOT_LIST_POINTS‘) = 0
then
‘quickdraw/__x_plot_flush_point_buffer‘();
if ‘quickdraw/__X_PLOT_PLOTS_POSITION‘ >= ‘quickdraw/__X_PLOT_BUFFERSIZE‘
then ‘quickdraw/__x_plot_flush_plot_buffer‘();
fi;
‘quickdraw/__X_PLOT_BUFFER_PLOTS‘[‘quickdraw/__X_PLOT_PLOTS_POSITION‘]
:= plots[listplot](‘quickdraw/__X_PLOT_LIST_POINTS‘,
color = ‘quickdraw/__X_PLOT_DEFAULT_COLOR‘,
axes = none);
‘quickdraw/__X_PLOT_LIST_POINTS‘ := [];
‘quickdraw/__X_PLOT_PLOTS_POSITION‘ :=
‘quickdraw/__X_PLOT_PLOTS_POSITION‘ + 1;
fi;
NULL
end:
##############
# Nettoyage global.
#
‘quickdraw/__x_plot_clear_all‘ := proc()
global ‘quickdraw/__X_PLOT_LIST_POINTS‘,
‘quickdraw/__X_PLOT_SET_PLOTS‘,
‘quickdraw/__X_PLOT_POINTS_POSITION‘,
‘quickdraw/__X_PLOT_PLOTS_POSITION‘,
‘quickdraw/__X_PLOT_CURRENT_POINT‘,
‘quickdraw/__X_PLOT_DEFAULT_COLOR‘;
‘quickdraw/__X_PLOT_LIST_POINTS‘ := [];
‘quickdraw/__X_PLOT_SET_PLOTS‘ := {};
‘quickdraw/__X_PLOT_POINTS_POSITION‘ := 1;
‘quickdraw/__X_PLOT_PLOTS_POSITION‘ := 1;
#
‘quickdraw/__X_PLOT_CURRENT_POINT‘ := [0, 0];
#
‘quickdraw/__X_PLOT_DEFAULT_COLOR‘ := red;
NULL;
end:
#################
# Se deplace vers le point P en tracant.
quickdraw[lineTo] := proc(P::Point)
global ‘quickdraw/__X_PLOT_POINTS_POSITION‘,
‘quickdraw/__X_PLOT_BUFFERSIZE‘,
‘quickdraw/__X_PLOT_BUFFER_POINTS‘,
‘quickdraw/__X_PLOT_LIST_POINTS‘,
‘quickdraw/__X_PLOT_CURRENT_POINT‘;
# Si le buffer *et* l’ensemble de quickdraw[point]s sont vides, on commence par
# y rentrer le quickdraw[point] courant.
if ‘quickdraw/__X_PLOT_POINTS_POSITION‘ = 1
and nops(‘quickdraw/__X_PLOT_LIST_POINTS‘) = 0
then
‘quickdraw/__X_PLOT_BUFFER_POINTS‘[1] :=
‘quickdraw/__X_PLOT_CURRENT_POINT‘;
‘quickdraw/__X_PLOT_POINTS_POSITION‘ := 2;
fi;
# Le buffer est-il plein ?
if ‘quickdraw/__X_PLOT_POINTS_POSITION‘ >= ‘quickdraw/__X_PLOT_BUFFERSIZE‘
then ‘quickdraw/__x_plot_flush_quickdraw[point]_buffer‘();
fi;
‘quickdraw/__X_PLOT_BUFFER_POINTS‘[‘quickdraw/__X_PLOT_POINTS_POSITION‘]
:= P;
‘quickdraw/__X_PLOT_CURRENT_POINT‘ := P;
‘quickdraw/__X_PLOT_POINTS_POSITION‘
:= ‘quickdraw/__X_PLOT_POINTS_POSITION‘ + 1;
NULL;
end:
#################
# On deplace le curseur au point P, ie. on flushe le plot courant.
#
quickdraw[moveTo] := proc(P::Point)
global ‘quickdraw/__X_PLOT_POINTS_POSITION‘,
‘quickdraw/__X_PLOT_BUFFER_POINTS‘,
‘quickdraw/__X_PLOT_CURRENT_POINT‘;
# Ne rien faire si on est deja en place.
if ‘quickdraw/__X_PLOT_CURRENT_POINT‘[1] <> P[1]
or ‘quickdraw/__X_PLOT_CURRENT_POINT‘[2] <> P[2]
then
‘quickdraw/__x_plot_flush_current_plot‘();
‘quickdraw/__X_PLOT_CURRENT_POINT‘ := P;
‘quickdraw/__X_PLOT_POINTS_POSITION‘ := 1;
fi;
NULL;
end:
################
# Change la couleur courante.
#
quickdraw[setColor] := proc(c)
global ‘quickdraw/__X_PLOT_DEFAULT_COLOR‘;
‘quickdraw/__x_plot_flush_current_plot‘();
‘quickdraw/__X_PLOT_DEFAULT_COLOR‘ := c:
NULL;
end:
################
# Trace final.
#
quickdraw[flushPlot] := proc()
global ‘quickdraw/__X_PLOT_SET_PLOTS‘;
local __X_PLOT_PLOT;
‘quickdraw/__x_plot_flush_current_plot‘();
‘quickdraw/__x_plot_flush_plot_buffer‘();
if nops(‘quickdraw/__X_PLOT_SET_PLOTS‘) <> 0
then
__X_PLOT_PLOT := plots[display](‘quickdraw/__X_PLOT_SET_PLOTS‘);
else
__X_PLOT_PLOT := NULL;
fi;
‘quickdraw/__x_plot_clear_all‘();
__X_PLOT_PLOT;
end:
quickdraw[point] := proc(x::realcons, y::realcons)
[evalf(x), evalf(y)];
end:

Documents pareils