caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Question de Caml !
@ 2001-05-04 11:09 flexx
  2001-05-23 10:04 ` Sven LUTHER
  0 siblings, 1 reply; 3+ messages in thread
From: flexx @ 2001-05-04 11:09 UTC (permalink / raw)
  To: caml-list

Bonjour !

Je souhaite réaliser un programme de dessin en caml, dans le cadre scolaire.
Cependant, j'ai un probleme: je n'arrive pas à déclarer des variables globales.
Notre prof voudrait que l'on fasse quelques fonctions de dessin (agrandissement, rotation...), mais on doit utiliser Caml en mode fonctionnel.
Comment faire ?

Merci d'avance.

__________________________________________________
Voila vous propose une boite aux lettres gratuite sur Voila Mail:
http://mail.voila.fr
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Question de Caml !
  2001-05-04 11:09 [Caml-list] Question de Caml ! flexx
@ 2001-05-23 10:04 ` Sven LUTHER
  2001-05-23 10:19   ` [Caml-list] erreur de destination :((( Sven LUTHER
  0 siblings, 1 reply; 3+ messages in thread
From: Sven LUTHER @ 2001-05-23 10:04 UTC (permalink / raw)
  To: flexx; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 2375 bytes --]

On Fri, May 04, 2001 at 01:09:39PM +0200, flexx@voila.fr wrote:
> Bonjour !
> 
> Je souhaite r?aliser un programme de dessin en caml, dans le cadre scolaire.
> Cependant, j'ai un probleme: je n'arrive pas ? d?clarer des variables globales.
> Notre prof voudrait que l'on fasse quelques fonctions de dessin (agrandissement, rotation...), mais on doit utiliser Caml en mode fonctionnel.
> Comment faire ?

mmm, j'ai modifier un peu et tester le resultat.

Plutot sympa, mais il y a quelques problemes, dont j'imagine tu t'est rendu
compte :

  de temps a autres, cela plante, cela a a voir avec les bouton que l'on
  choisi, en particulier les 2 premiers (lignes et ligne brise), je pense que
  tu effectue une boucle qui ne s'arete pas comme il faut. A verifier donc.

  Et les valeurs par defaut du rectangle ne sont pas correcte : si tu veut
  dessiner un rectangle plein, et que tu passe la souris sous le premier
  point, alors le rectangle est dessiner depuis le bas du dessin. J'imagine
  que l'erreur vient d'une valeur qui devient negative ou quelque chose du
  genre.

  De meme pour les lignes brise, tu part de (0,0) c'est pas tres propre.

  Et il serait utile de 'clipper' les dessins pour ne pas sortir de la zone de
  dessin et ecrire sur les boutons.

  De plus, tu ne tient pas reellement compte de l'avantage d'avoir des
  fonctions comme valeurs.

  Tu peut tres bien faire :

  let icone_ligne_brise origin_x origin_y = 
    (* dessine l'icone ligne brisee a partir de l'offset origin_x, origin_y *)
  let icone_ligne origin_x origin_y = 
    (* dessine l'icone lignes a partir de l'offset origin_x, origin_y *)

  let bouton icone x y w h selectionner =
    let fg,bg =
      if selectionner
      then 0xFFFFFF, 0x000000 
      else 0x000000, 0xFFFFFF
      in
    set_color fg;
    moveto x y;lineto x (y+h);lineto (x+w) (y+h);
    set_color bg;
    lineto (x+w) y;
    lineto x y;
    icone x y;

  puis tu fait :

    bouton icone_ligne_brise 10 10 30 30 false;
    bouton icone_lignes 10 10 30 30 false;

ou quelque chose du genre.

Aussi, essaye de mieux separer les parties de ton programme, en particulier
pour la gestion du dessin et des clicks souris.

Finalement, voici un petit bout de code qui contient un peu de calcul
matriciel qui peut t'aider si tu veut (c'est du code ocaml).

Bonne chance pour tes exams, ...

Amicalement,

Sven Luther

[-- Attachment #2: embed.ml --]
[-- Type: text/plain, Size: 6119 bytes --]

module type T_Embed = sig
  type embed = float * float
  val string_of_embed : embed -> string
  val emb0 : embed
  val embed : float -> float -> embed 
  val emb_x : embed -> float
  val emb_y : embed -> float
  val embed_int : int -> int -> embed 
  val emb_x_int : embed -> int
  val emb_y_int : embed -> int
  val square : float -> float
  val scalaire : embed -> embed -> float
  val norm2 : embed -> embed -> float
  val norm : embed -> embed -> float
  val is_in_dist : embed -> embed -> float -> bool
  val middle : embed -> embed -> embed
  val move_alpha_1 : embed -> embed -> embed
  val move_alpha_2 : embed -> embed -> embed
  val alpha_0 : float -> embed -> embed -> embed
  val is_on : float -> float -> embed -> embed -> embed option -> float option
  val pi : float
  val degree_of_rad : float -> float
  val rad_of_degree : float -> float
  module Matrix : sig
    type vector = float array
    type matrix = float array array
    val vector : float -> float -> float -> vector
    val matrix : vector -> vector -> vector -> matrix
    val string_of_matrix : matrix -> string
    val vector_value : vector -> int -> float
    val matrix_line : matrix -> int -> vector
    val matrix_column : matrix -> int -> vector
    val matrix_transpose : matrix -> matrix
    val mult_add_2_vectors : vector -> vector -> float
    val mult_matrix_by_vector : matrix -> vector -> vector
    val mult_2_matrix : matrix -> matrix -> matrix
    val sample_vector : vector
    val sample_matrix : matrix
    val vector_from_embed : embed -> vector
    val vector_to_embed : vector -> embed
    val identity : matrix
    val translation : embed -> matrix
    val rotation_origin : float -> matrix
    val rotation : embed -> float -> matrix
    val homothetie_origin : float -> float -> matrix
    val homothetie : embed -> float -> float -> matrix
    val compose : matrix -> matrix -> matrix
    val apply : matrix -> embed -> embed
  end
end
module Embed : T_Embed = struct
  type embed = float * float
  let string_of_embed (x,y) = Printf.sprintf "<%f,%f>" x y
  let emb0 = (0.0,0.0)
  let embed x y = (x,y)
  let emb_x (x,y) = x
  let emb_y (x,y) = y
  let embed_int x y = (float_of_int x,float_of_int y)
  let emb_x_int (x,y) = int_of_float x
  let emb_y_int (x,y) = int_of_float y
  let square x = x *. x
  let scalaire (x,y) (x',y') = x *. x' +. y *. y'
  let scalaireT (x,y) (x',y') = x *. y' -. x' *. y
  let norm2 (x,y) (x',y') = square (x-.x') +. square (y-.y')
  let norm e e' = sqrt (norm2 e e')
  let is_in_dist e e' n = norm2 e e' < square n
  let middle (x,y) (x',y') = ((x+.x')/.2., (y+.y')/.2.)
  let move_alpha_1 (_,_) (x,y) = x,y
  let move_alpha_2 (_,_) (x,y) = x,y
  let alpha_0 d (x,y) (mx,my) =
    let dab = norm (x,y) (mx,my) in
    (mx+.(y-.my)*.d/.dab, my+.(mx-.x)*.d/.dab)
  let minmax seldist x y =
    if x>y then (y-.seldist, x+.seldist)
    else (x-.seldist, y+.seldist)
  let is_on min_dist sel_dist (x,y) (xa,ya) = function 
    | None ->
      let dist = norm2 (x,y) (xa,ya) in
      if dist < square min_dist then Some dist else None
    | Some (xb,yb) -> 
      let (xmin, xmax) = minmax sel_dist xa xb
      and (ymin, ymax) = minmax sel_dist ya yb in
      if x<xmin or x>xmax or y<ymin or y>ymax
      then None
      else let dx = (xb-.xa) and dy = (yb-.ya) in
      let dab = norm2 (xa,ya) (xb,yb)
      and f x y = x*.x/.y in
      let distT = f (scalaireT (x-.xa, y-.ya) (dx, dy)) dab
      and dist = f (scalaire (x-.xa, y-.ya) (dx, dy)) dab
      in if distT < square min_dist && dist < dab then Some dist else None
  let pi = 2. *. (acos 0.)
  let degree_of_rad a =
    let d = a *. 180. /. pi
    in if d < 0. then 360. +. d else d
  let rad_of_degree a = a *. pi /. 180.

  module Matrix = struct
    type vector = float array
    type matrix = float array array
    let vector x y z = [|x; y; z|]
    let matrix x y z = [|x; y; z|]
    let string_of_matrix m = 
      Printf.sprintf "{%f, %f, %f; %f, %f, %f; %f, %f, %f}"
        m.(0).(0) m.(0).(1) m.(0).(2)
        m.(1).(0) m.(1).(1) m.(1).(2)
        m.(2).(0) m.(2).(1) m.(2).(2)
    let vector_value v n = v.(n-1)
    let matrix_line m n = m.(n-1)
    let matrix_column m n =
      vector m.(0).(n-1) m.(1).(n-1) m.(2).(n-1)
    let matrix_transpose m = matrix
      (matrix_column m 1) (matrix_column m 2) (matrix_column m 3)
    let mult_add_2_vectors x y =
      x.(0) *. y.(0) +. x.(1) *. y.(1) +. x.(2) *. y.(2)
    let mult_matrix_by_vector m v = matrix
      (mult_add_2_vectors (matrix_line m 1) v)
      (mult_add_2_vectors (matrix_line m 2) v)
      (mult_add_2_vectors (matrix_line m 3) v)
    let mult_2_matrix m n = matrix_transpose (matrix
      (mult_matrix_by_vector m (matrix_column n 1))
      (mult_matrix_by_vector m (matrix_column n 2))
      (mult_matrix_by_vector m (matrix_column n 3)))
    let sample_vector = vector 1. 2. 3.
    let sample_matrix = matrix
      (vector 1. 2. 3.) (vector 4. 5. 6.) (vector 7. 8. 9.)
    let vector_from_embed (x, y) = vector x y 1.0
    let vector_to_embed v =
      (vector_value v 1) /. (vector_value v 3),
      (vector_value v 2) /. (vector_value v 3)
    let identity = matrix
      (vector 1.0 0.0 0.0)
      (vector 0.0 1.0 0.0)
      (vector 0.0 0.0 1.0)
    let translation (a,b) =
      Printf.printf "translation of %f, %f.\n" a b; flush stdout; matrix
      (vector 1.0 0.0 a)
      (vector 0.0 1.0 b)
      (vector 0.0 0.0 1.0)
    let rotation_origin a =
      let a = rad_of_degree a in matrix
      (vector (cos a) (-1.0 *. (sin a)) 0.0)
      (vector (sin a) (cos a) 0.0)
      (vector 0.0 0.0 1.0)
    let rotation (x,y) a = mult_2_matrix (translation (-.x, -.y))
        (mult_2_matrix (rotation_origin a) (translation (x, y)))
    let homothetie_origin a b = matrix
      (vector a 0.0 0.0)
      (vector 0.0 b 0.0)
      (vector 0.0 0.0 1.0)
    let homothetie (x,y) a b = mult_2_matrix (translation (-.x, -.y))
        (mult_2_matrix (homothetie_origin a b) (translation (x, y)))
    let compose m n = mult_2_matrix m n
    let apply m (x,y) = vector_to_embed
      (mult_matrix_by_vector m (vector_from_embed (x, y)))
  end
end

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Caml-list] erreur de destination :(((
  2001-05-23 10:04 ` Sven LUTHER
@ 2001-05-23 10:19   ` Sven LUTHER
  0 siblings, 0 replies; 3+ messages in thread
From: Sven LUTHER @ 2001-05-23 10:19 UTC (permalink / raw)
  To: Sven LUTHER; +Cc: caml-list

Desole pour le mail precedent, il n'etait pas destinee a la mailing liste
caml.

Amicalement,

Sven Luther
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2001-05-23 10:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-05-04 11:09 [Caml-list] Question de Caml ! flexx
2001-05-23 10:04 ` Sven LUTHER
2001-05-23 10:19   ` [Caml-list] erreur de destination :((( Sven LUTHER

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).