unit Un_graph2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, GLU, GL, MathComponent;

type
  TGraph2 = class(TForm)
    MathControl1: TMathControl;
    procedure FormPaint(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    DC:HDC;
    hrc:HGLRC;
    procedure PixelFormat;
  public
    { Public declarations }
  end;

var
  Graph2: TGraph2;

implementation

uses Un_numbers, Un_main, Un_param;

{$R *.DFM}

procedure TGraph2.FormPaint(Sender: TObject);
 var
 ps:TPaintStruct;
 i,j1,i1,code,k:integer;
 j,x,y,z,t,t1,t2,U,W,F,x1,x2:real;
 s1,s2,s:string;
 a:array[1..2,1..100] of real;
 b:array[1..4,1..100] of real;
 c:array[1..2,1..7] of real;
 flag1,flag2,flag3,flag4:boolean;
begin

 glMatrixMode(GL_PROJECTION);
 glLoadIdentity( );

 if width <= height then
  glOrtho (-2, 2, -3*height/width, 3*height/width, -10.0, 10.0)
 else
  glOrtho (-2*width/height, 5.5*strtoint(Param.Edit3.Text)*width/height, -2, 2, -10.0, 10.0);

 glViewport(0, 0, width, height);

 code:=0;
 U:=0;W:=0;F:=0;

 BeginPaint(Handle,ps);
 glClearColor(0,0,0,1);
 glClear(GL_COLOR_BUFFER_BIT{ or GL_DEPTH_BUFFER_BIT});

// glEnable(GL_DEPTH_TEST);

 glPointSize(1);
// glRotated(130,0,0,1);

 j:=0.05;

 s:=Main.FuncEd.Text;

 t:=0;flag1:=true;flag2:=false;flag3:=false;x1:=0;x2:=0;k:=1;
 val(Param.Edit1.Text, t1, code);
 flag4:=true;

 for i1:=1 to strtoint(Main.ParamEd.Text) do
    begin
     s1:=s;
     j1:=1;
     val(Numbers.Parameters.Cells[2,i1], a[1,i1], code);
     while j1<=length(s) do
      begin
       if s1[j1]='x' then
        begin
         str(a[1,i1]:0:0,s2);
         s1:=copy(s1,1,j1-1)+s2+copy(s1,j1+1,length(s1)-j1);
        end;
       inc(j1);
      end;
     MathControl1.MathExpression:=s1;
     a[2,i1]:=MathControl1.MathResult;
    end;

 while j<=3.2 do
  begin

   x1:=0;x2:=0;

   for i:=1 to 800*strtoint(Param.Edit3.Text) do
    begin

     x:=i/80-2.6;
     y:=-7.3+3*j;
     for i1:=1 to strtoint(Main.ParamEd.Text) do
      begin
       val(Numbers.Parameters.Cells[1,i1],U,code);
       val(Numbers.Parameters.Cells[3,i1],F,code);
       y:=y+(cos(a[1,i1]*t-a[2,i1]*x+F))*U/(10*(strtoint(Param.Edit2.Text)));
      end;

     if (x>=-1) then
     begin
      if (y+7.3-3*j>-0.02)and(y+7.3-3*j<0.02)and(flag1=true)and(flag3=false) then   {flag2 - проверяем рост или падение}
       begin
        if k=1 then
         begin                                      {flag1 - всё OK}
          if x1<>0 then
           begin
            x2:=x;flag3:=true;b[1,k]:=x1;b[2,k]:=x2;
            b[3,k]:=t;b[4,k]:=y;inc(k);
           end                          {flag3 - горб уже нашли}
           else x1:=x;
         end
         else
         begin
          if x1<>0 then
           begin
            x2:=x;flag3:=true;b[1,k]:=x1;b[2,k]:=x2;
            b[3,k]:=t;b[4,k]:=y;inc(k);
           end
           else
           begin
            {!}      if ((x-b[1,k-1])<0.2)and((x-b[1,k-1])>0) then x1:=x;
           end;
         end;
        flag1:=false;
        flag2:=true;
       end;
      if (y+7.3-3*j<-0.02)and(flag2=true)and(flag3=false) then
       begin
        x1:=0;x2:=0;flag1:=true;flag2:=false;
       end;
      if (y+7.3-3*j>=0.02)and(flag2=true)and(flag3=false) then
       begin
        flag1:=true;flag2:=false;
       end;
     end;

     z:=1{-1.5+j};

     if (flag4=true)or(t=0) then            {flag4 - рисуем}
      begin
       glColor3f ({(i/200)*i}0, {(i/300)*5}1, {(i+100)/300}1);
       glEnable(GL_POINT_SMOOTH);
       glBegin(GL_POINTS);
        glVertex3f(x,y,z);
       glEnd;
      end;
    end;

   flag4:=false;
   t2:=t2+0.1;

   if ((t2-t1)<0.01)and((t2-t1)>-0.01) then
    begin
     j:=j+0.2;t2:=0;flag4:=true;
    end;

   t:=t+0.1;
   flag1:=true;
   flag2:=false;
   flag3:=false;
  end;

 i:=2;i1:=2;
 j1:=round(t1*10);
 c[1,1]:=b[1,1];
 c[2,1]:=b[4,1];
 for i:=1 to 7*j1-1 do
  if (i mod j1)=0 then
   begin
    c[1,i1]:=b[1,i+1];c[2,i1]:=b[4,i+1];inc(i1);
   end;
 glFlush();
 swapBuffers(dc);
 EndPaint(Handle,ps);

 BeginPaint(Handle,ps);
 glClearColor(0,0,0,1);
 glPointSize(4);
 i:=1;j:=0.05;y:=-7.3+3*j;
 glEnable(GL_POINT_SMOOTH);
 glColor3f (1, 1, 1);
 glBegin(GL_POINTS);
 while i<=7 do
  begin
   x:=c[1,i];
   y:=-7.3+3*j;
   glVertex3f(x-t1+0.1,y+5.4,1);
   inc(i);j:=j+0.2;
  end;
 glEnd;
// glDisable(GL_DEPTH_TEST);

 glFlush();
 swapBuffers(dc);
 EndPaint(Handle,ps);

end;

procedure TGraph2.FormResize(Sender: TObject);
begin
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity( );

 if width <= height then
  glOrtho (-2, 2, -3*height/width, 3*height/width, -10.0, 10.0)
 else
  glOrtho (-2*width/height, 5.5*strtoint(Param.Edit3.Text)*width/height, -2, 2, -10.0, 10.0);

 glViewport(0, 0, width, height);
end;

procedure TGraph2.FormDestroy(Sender: TObject);
begin
 wglMakeCurrent(0,0);
 wglDeleteContext(hrc);
 ReleaseDC(handle,DC);
end;

procedure TGraph2.PixelFormat;
var
 nPixelFormat:Integer;
 pfd:TPixelFormatDescriptor;
begin
 FillChar(pfd,Sizeof(pfd),0);

 pfd.nSize:=sizeof(pfd);
 pfd.nVersion:=1;
 pfd.dwFlags:=PFD_DOUBLEBUFFER+PFD_SUPPORT_OPENGL+PFD_DRAW_TO_WINDOW;
 pfd.iPixelType:=PFD_TYPE_RGBA;
 pfd.cColorBits:=24;
 pfd.cAlphaBits:=64;
 pfd.cAccumBits:=64;
 pfd.cDepthBits:=32;
 pfd.cStencilBits:=64;
 pfd.iLayerType:=PFD_MAIN_PLANE;

 nPixelFormat:=ChoosePixelFormat(DC,@pfd);
 if nPixelFormat<>0 then
  SetPixelFormat(DC,nPixelFormat,@pfd);
end;

procedure TGraph2.FormCreate(Sender: TObject);
begin
 DC:=GetDC(Handle);
 PixelFormat;
 hrc:=wglCreateContext(DC);
 wglMakeCurrent(DC,hrc);
end;

end.



