IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Delphi Discussion :

tracer un joli cosinus


Sujet :

Delphi

  1. #1
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 2
    Points : 1
    Points
    1
    Par défaut tracer un joli cosinus
    salut a tous
    j'ai un petit défi c'est de tracer une fonction cosinus basique avec delphi....

    j'arrive a tracer une ligne, 2 lignes
    bon c'est vrai ca tient sur 5 lignes c'est pas dur....

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    procedure TForm2.Button1Click(Sender: TObject);
    begin
    Form2.Canvas.MoveTo(0,0) ;
    Form2.Canvas.LineTo(Form2.ClientWidth , Form2.ClientHeight) ;
    Form2.Canvas.MoveTo(Form2.ClientWidth,Form2.ClientHeight) ;
    Form2.Canvas.LineTo(850,0) ;
    je veux donc réussir a trouver l'algorithme qui permet de tracer des fonctions plus compliquées comme cosinus par exemple... ca fait 5h que je planche et rien... alors s'il vous plait aidezzzzzzzz moiiiiiiiiiiiiii !!!!!!!!!!!

  2. #2
    Rédacteur


    Profil pro
    Inscrit en
    Janvier 2003
    Messages
    7 171
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2003
    Messages : 7 171
    Points : 15 060
    Points
    15 060
    Billets dans le blog
    1
    Par défaut

  3. #3
    Expert éminent sénior

    Avatar de sjrd
    Homme Profil pro
    Directeur de projet
    Inscrit en
    Juin 2004
    Messages
    4 517
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : Suisse

    Informations professionnelles :
    Activité : Directeur de projet
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2004
    Messages : 4 517
    Points : 10 154
    Points
    10 154
    Par défaut
    De façon très basique tu peux faire comme ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Canvas.MoveTo(0, 30);
    for I := 1 to 200 do
      Canvas.LineTo(I, 30 - Round(cos(I / 3.0)*30.0));
    Avec une fenêtre de 200x60 pixels.
    sjrd, ancien rédacteur/modérateur Delphi.
    Auteur de Scala.js, le compilateur de Scala vers JavaScript, et directeur technique du Scala Center à l'EPFL.
    Découvrez Mes tutoriels.

  4. #4
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 2
    Points : 1
    Points
    1
    Par défaut
    je vous remercie ca marche au poil!
    par contre encore une petite question est ce qu'il est possible de voir la fonction se tracer sur l'écran??
    en faisant ainsi la fonction se trace tellement vite qu'on la voit pas.
    moi j'aurais besoin de la voir un peu comme les cardiographes dans les hopitaux....

    merci a vous

  5. #5
    Expert éminent sénior
    Avatar de Cl@udius
    Homme Profil pro
    Développeur Web
    Inscrit en
    Février 2006
    Messages
    4 878
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Février 2006
    Messages : 4 878
    Points : 10 008
    Points
    10 008
    Par défaut
    Salut

    Pour temporiser le tracé de ta courbe tu peux ajouter une instruction Sleep() dans la boucle.
    En reprenant l'exemple de sjrd:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    Canvas.MoveTo(0, 30);
    for I := 1 to 200 do
    begin
      Canvas.LineTo(I, 30 - Round(cos(I / 3.0)*30.0));
      Sleep(100); // Valeur à ajuster selon tes besoins;
    end;
    @+ Claudius

  6. #6
    Membre chevronné
    Avatar de Archimède
    Homme Profil pro
    Enseignant
    Inscrit en
    Avril 2005
    Messages
    1 644
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 58
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Avril 2005
    Messages : 1 644
    Points : 1 975
    Points
    1 975
    Par défaut
    ci-joint une solution plus complète:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
     
    unit Unit1;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls;
     
    type
      TForm1 = class(TForm)
        Timer1: TTimer;
        PaintBox1: TPaintBox;
        procedure PaintBox1Paint(Sender: TObject);
        procedure Timer1Timer(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Déclarations privées }
        w,h,t,periode,A:integer;
        graphique:TBitmap;
        procedure trace;
      public
        { Déclarations publiques }
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    {$R *.dfm}
     
    procedure TForm1.FormCreate(Sender: TObject);
    begin
    position:=poscreencenter;
    width:=screen.Width ;
    height:=screen.Height div 2;
    w:=form1.ClientWidth;
    h:=form1.ClientHeight;
     
    with paintbox1 do begin
    top:=0;
    left:=0;
    width:=w;
    height:=h;
    end;
     
    graphique:=tbitmap.Create;
    with graphique do begin
    width:=w;
    height:=h;
    end;
     
    A:=-h div 4;
    periode:=1000; // 1 seconde  60puls/min
    timer1.Interval:=10; //10 ms
    end;
     
    procedure Tform1.trace;
    begin
    if (t=0) or (t>4*periode) then
    begin
    with graphique.Canvas do begin
    pen.Color:=clred;
    pen.Width:=3;
    brush.Color:=clblack;
    fillrect(clientrect);
    moveto(0,h div 2);
    end;
    t:=0; //pour t=0,c'est redondant... à voir pas le temps
    end;
    // 4 périodes pour la largeur de la fenêtre par exemple
    graphique.Canvas.lineto(round(w*t/(4*periode)),round(h div 2+A*sin(2*pi*t/periode)));
    paintbox1.Canvas.Draw(0,0,graphique);
    caption:='t='+floattostr(t/1000)+'s';
    end;
     
    procedure TForm1.Timer1Timer(Sender: TObject);
    begin
    t:=t+periode div 100;
    trace;
    end;
     
    procedure TForm1.PaintBox1Paint(Sender: TObject);
    begin
    trace;
    end;
     
    end.
    voir aussi pour le timer qui n'est pas précis...

  7. #7
    Membre chevronné
    Avatar de Archimède
    Homme Profil pro
    Enseignant
    Inscrit en
    Avril 2005
    Messages
    1 644
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 58
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Avril 2005
    Messages : 1 644
    Points : 1 975
    Points
    1 975
    Par défaut
    en plus propre:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
     
    unit Unit1;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, ComCtrls;
     
    type
      TForm1 = class(TForm)
        Timer1: TTimer;
        PaintBox1: TPaintBox;
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure FormResize(Sender: TObject);
        procedure PaintBox1Paint(Sender: TObject);
        procedure Timer1Timer(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Déclarations privées }
        w,h,t,periode,nT,A:integer;
        graphique:TBitmap;
        procedure trace;
        procedure init;
      public
        { Déclarations publiques }
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    {$R *.dfm}
     
    procedure TForm1.FormCreate(Sender: TObject);
    begin
    position:=poscreencenter;
    width:=screen.Width ;
    height:=screen.Height div 2;
    periode:=1000; // 1 seconde  60puls/min
    timer1.Interval:=10; //10 ms
    nT:=4;//nb de périodes sur largeur fenêtre ici 4 ou plus selon vouloir
    end;
     
    procedure TfORM1.init;
    begin
    graphique.Canvas.fillrect(paintbox1.clientrect);
    graphique.Canvas.moveto(0,h div 2);
    end;
     
    procedure Tform1.trace;
    begin
     
    if t=0 then init;
    if t>nT*periode  then begin
    init;
    t:=0;
    end;
     
    // 4 périodes pour la largeur de la fenêtre =>nT=4
    graphique.Canvas.lineto(round(w*t/(nT*periode)),round(h div 2+A*sin(2*pi*t/periode)));
    //------------------------------------------------
    caption:='t='+floattostr(t/1000)+'s';
    paintbox1.Canvas.Draw(0,0,graphique);
    end;
     
     
    procedure TForm1.Timer1Timer(Sender: TObject);
    begin
    t:=t+timer1.interval;
    trace;
    end;
     
     
    procedure TForm1.PaintBox1Paint(Sender: TObject);
    begin
    with graphique.canvas do begin
    pen.Color:=clred;
    pen.Width:=3;
    brush.Color:=clblack;
    end;
    trace;
    end;
     
    procedure TForm1.FormResize(Sender: TObject);
    begin
    w:=form1.ClientWidth;
    h:=form1.ClientHeight;
    A:=-h div 4;
    with paintbox1 do begin
    top:=0;
    left:=0;
    width:=w;
    height:=h;
    end;
     
    graphique:=tbitmap.Create;
    with graphique do begin
    width:=w;
    height:=h;
    end;
     
    t:=0;
    end;
     
    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
    graphique.Free;
    end;
     
    end.

  8. #8
    Membre régulier Avatar de user 56gt8
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    86
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 86
    Points : 92
    Points
    92
    Par défaut
    Bonjour.
    Il y a aussi cette solution , plus basée sur l'audionumérique (utilisation d'une classe qui implémente un oscillo).
    Le code n'est pas complet il faut avoir créer la form avec une paintbox , mettre un timer etc , bref il faut l'adapter.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
     
    interface
     
    type
    TSinCos = class
    procedure process(Frq,SR:single);
    private
    inc,rmp : single ;
    public
    Outpsin , outpcos : single ;
    end;
     
     
    var 
    a1 : TSinCos ;
    step,speed : integer;
     
     
     
    implementation
     
    procedure TSinCos.process(Frq,SR:single);
    begin
    inc := (1/SR)*Frq ;
    rmp := rmp+inc ;
    if rmp > 1 then rmp := 0;
    outpsin := sin(rmp*2*pi) ;
    outpcos := -outpsin ;
    end ;
     
    procedure TForm1.FormCreate(Sender: TObject);
    begin
    a1 := TSinCos.Create ;
    end;
     
    procedure TForm1.Timer1Timer(Sender: TObject);
    begin
    step := step +1  ;
    if step > paintbox1.Width then begin  step := 0 ; paintbox1.Repaint ; end;
    a1.process(speed, 44100);
    paintbox1.Canvas.Pixels [step,round(a1.Outpcos*round(paintbox1.heigth/4)+round(paintbox1.heigth/2)]:=clred;
    end;
    j'ai mis un exemple avec plus d'oscillos en pièce jointe

  9. #9
    Membre chevronné
    Avatar de Archimède
    Homme Profil pro
    Enseignant
    Inscrit en
    Avril 2005
    Messages
    1 644
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 58
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Avril 2005
    Messages : 1 644
    Points : 1 975
    Points
    1 975
    Par défaut
    Interessant... je prends...

Discussions similaires

  1. [FP]Tracer Une ligne avec Dev-pascal
    Par yffick dans le forum Turbo Pascal
    Réponses: 9
    Dernier message: 17/12/2003, 16h33
  2. Tracer un carré de X cm
    Par mdel dans le forum Composants VCL
    Réponses: 6
    Dernier message: 06/01/2003, 16h17
  3. [VB6] [Graphisme] Tracer un cercle avec pset
    Par bleuerouge dans le forum VB 6 et antérieur
    Réponses: 5
    Dernier message: 09/12/2002, 17h12
  4. Tracer une ligne droite sans les interruptions
    Par Stef784ever dans le forum x86 16-bits
    Réponses: 4
    Dernier message: 25/11/2002, 01h22
  5. Des déformations pas jolie lors du deplacement de la cam
    Par scorpiwolf dans le forum OpenGL
    Réponses: 4
    Dernier message: 01/11/2002, 13h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo