Mega Code Archive

 
Categories / Delphi / Graphic
 

An eassy procedure to draw a Vector Field

Title: An eassy procedure to draw a Vector Field Question: This coded is useful is you have to draw a velocity field in a two dimensional mesh Answer: This procedure draw vectors on a Chart component. procedure TDatosInput.Vector(VArrayX,VArrayY: TArray2D; VArrayU,VArrayV: TArray3D; nx,ny,nt: DWord; Malla: TPointSeries; Chrt: TChart; ColorVector: TColor; Fact: DWord); var i,j,PosX: DWord; xi,yi,xf,yf: Longint; Xn,Yn: Extended; Dx,Dy: Extended; Ang: Extended; begin {Config Canvas} Chrt.Canvas.Pen.Color:=ColorVector; Chrt.Canvas.Brush.Color:=ColorVector; {Paint Color at each cell} for j:=0 to ny-1 do for i:=0 to nx-1 do begin {Calc Angle} PosX:=i; if (i = nx-1) then PosX:=nx-2; Dx:=VArrayX[PosX+1,j]-VArrayX[PosX,j]; Dy:=VArrayY[PosX+1,j]-VArrayY[PosX,j]; {Check Dx} Ang:=0; if (Dx = 0) or (Dy = 0) then begin if (Dy 0) and (Dx = 0) then Ang:=0.5*Pi; if (Dy if (Dy = 0) and (Dx 0) then Ang:=0; if (Dy = 0) and (Dx end else begin Ang:=ArcTan(Dy/Dx); {Check Position} if ((Dy 0) or (Dy Ang:=Ang+Pi; end; {New Coordinates - Rotating the Vector} xn:=VArrayU[i,j,nt]*Fact*Cos(Ang)-VArrayV[i,j,nt]*Fact*Sin(Ang); yn:=VArrayU[i,j,nt]*Fact*Sin(Ang)+VArrayV[i,j,nt]*Fact*Cos(Ang); {Drawing the Arrow} xi:=Malla.CalcXPosValue(VArrayX[i,j]); yi:=Malla.CalcYPosValue(VArrayY[i,j]); xf:=Malla.CalcXPosValue(xn+VArrayX[i,j]); yf:=Malla.CalcYPosValue(yn+VArrayY[i,j]); Chrt.Canvas.Arrow(False,Point(xi,yi),Point(xf,yf),5,10,0); end; end;