Hey, what's going on?

Some simple Delphi graphic subroutines

Posted by Syeilendra Pramuditya on February 14, 2011

I just realized that Delphi does not have functions to draw circle and equilateral-triangle which fit my need. So I just wrote some subroutines to do this.

Subroutine to initialize drawing area

{Subroutine to initialize drawing area}
procedure bitmap_init(image1:TImage);
begin
Bitmap:=TBitmap.create;(*Create a bitmap object*)
Bitmap.width:=500;(*Assign dimensions*)
Bitmap.height:=500;
image1.Picture.Graphic:=Bitmap;
end;

Subroutine to draw circle

{Subroutine to draw circle}
procedure DrawCircle(x,y,radius:real;image1:TImage;LineColor:array3int;BrushColor:array3int);
{
Passed parameters:
x,y        = coordinate of circle's center [pixel]
radius     = circle radius [pixel]
LineColor  = line color in RGB format
BrushColor = brush color in RGB format
}
var
x1,y1,x2,y2:integer;
begin
x1:=round(x-radius);
y1:=round(y-radius);
x2:=round(x+radius);
y2:=round(y+radius);

with image1.Picture.Bitmap.Canvas do
begin
pen.Color:=rgb(LineColor[1],LineColor[2],LineColor[3]);
brush.Color:=rgb(BrushColor[1],BrushColor[2],BrushColor[3]);
ellipse(x1,y1,x2,y2);
end;

end;

Subroutine to draw equilateral triangle

{Subroutine to draw equilateral triangle}
procedure DrawEquiTri(x,y,LSide,angle:real;image1:TImage;LineColor:array3int;BrushColor:array3int);
{
Passed parameters:
x,y = coordinate of triangle's center
LSide = side length [pixel]
angle = triangle's direction [degree]
LineColor  = line color in RGB format
BrushColor = brush color in RGB format
}
var
radius:real;
x1,y1,x2,y2,x3,y3:integer;
begin
radius:=(LSide/2)/cos(pi/6);

x1:=round( x+radius*cos(0+angle*pi/180) );
y1:=round( y+radius*sin(0+angle*pi/180) );
x2:=round( x+radius*cos(2*pi/3+angle*pi/180) );
y2:=round( y+radius*sin(2*pi/3+angle*pi/180) );
x3:=round( x+radius*cos(4*pi/3+angle*pi/180) );
y3:=round( y+radius*sin(4*pi/3+angle*pi/180) );

with image1.Picture.Bitmap.Canvas do
begin
pen.Color:=rgb(LineColor[1],LineColor[2],LineColor[3]);
brush.Color:=rgb(BrushColor[1],BrushColor[2],BrushColor[3]);
Polygon([Point(x1,y1),Point(x2,y2),Point(x3,y3)]);
end;

end;

Subroutine to draw rectangle (with user-specified inclination angle)

{Subroutine to draw rectangle (with user-specified inclination angle)}
procedure DrawRect(x,y,lside,sside,theta:real;image1:TImage;LineColor:array3int;BrushColor:array3int);
{
Passed parameters:
x,y        = coordinate of rectangle’s center [pixel]
lside      = length of the longer side [pixel]
sside      = length of the shorter side [pixel]
theta      = angle
LineColor  = line color in RGB format
BrushColor = brush color in RGB format

The "Math" unit must be included in the main program
}
var
x1,y1,x2,y2,x3,y3,x4,y4:integer;
radius,theta2:real;
begin
theta:=theta*pi/180;
radius:=sqrt(sqr(lside/2)+sqr(sside/2));
theta2:=arctan(sside/lside);
DrawCircle(x,y,5,image1,LineColor,BrushColor);
x1:=round(x-radius*cos(theta2+theta));
y1:=round(y-radius*sin(theta2+theta));
x2:=round(x+radius*cos(theta2-theta));
y2:=round(y-radius*sin(theta2-theta));
x3:=round(x+radius*cos(theta2+theta));
y3:=round(y+radius*sin(theta2+theta));
x4:=round(x-radius*cos(theta2-theta));
y4:=round(y+radius*sin(theta2-theta));

with image1.Picture.Bitmap.Canvas do
begin
pen.Color:=rgb(LineColor[1],LineColor[2],LineColor[3]);
brush.Color:=rgb(BrushColor[1],BrushColor[2],BrushColor[3]);
polygon([point(x1,y1),point(x2,y2),point(x3,y3),point(x4,y4)]);
end;

end;

Subroutine to generate color bar

{Subroutine to generate color bar}
procedure DrawColorBar(x1,y1,xsize,ysize,bgcolor:integer;minval,maxval:real;image1:TImage);
{
x1,y1 = leftmost and topmost coordinate of the color bar
xsize = width of the color bar [pixel]
ysize = height of the color bar [pixel]
bgcolor = background color, 0=white and 1=black
minval = minimum value of the data
maxval = maximum value of the data
}
var
i,j,r,g,b,istep:integer;
freq,fstep:real;
stext:string;

begin
{
bgcolor=0, text will be written in black
bgcolor=1, text will be written in lime
}
fstep:=(maxval-minval)/5;
istep:=round(fstep*ysize/(maxval-minval));

freq:=2*pi/(2*ysize);

with image1.Picture.Bitmap.Canvas do
begin

for i:=0 to ysize do
begin
r:=round(127.5+(127.5*sin(freq*i+0.0*pi-0.50*pi)));
g:=round(0.000+(255.0*sin(freq*i+0.5*pi-0.50*pi)));
b:=round(127.5+(127.5*sin(freq*i+1.0*pi-0.50*pi)));
r:=255-r;
b:=255-b;
pen.Color:=rgb(r,g,b);
moveto(x1,y1+i);
lineto(x1+xsize,y1+i);
end;

if bgcolor=0 then
begin
pen.color:=clblack;
font.color:=clblack;
end
else if bgcolor=1 then
begin
pen.color:=cllime;
font.color:=cllime;
end;

for j:=0 to 5 do
begin
moveto(x1+xsize,y1+j*istep);
lineto(x1+xsize+5,y1+j*istep);
font.Size:=8;
stext:=FormatFloat('0.000', maxval-j*fstep);
textout(x1+xsize+10,y1+j*istep-5,stext);
end;

end;

end;

How to use these subroutines?

First define a global variable type:

type array3int=array[1..3]of integer; {you need this array to define colors in RGB format}

Then define  a global variable (I named it “Bitmap”):

var Bitmap:TBitmap; {you need this variable to identify drawing object}

Then from your main program, you first invoke the initialization subroutine:

bitmap_init(image1);

After this, you can directly invoke the drawing subroutines, it should be look like this:

DrawEquiTri(400,250,100,0,image1,LineColor,BrushColor);

A free sample code can be downloaded here.

So why bother with these stuffs? well, it’s kind of personal, currently my life is just full of circles and triangles!:mrgreen: My work is about thermal hydraulic analysis of Sodium-cooled Fast Reactor (SFR) subassembly by subchannel analysis method. Unfortunately, the subchannel code I am using now does not have any data visualization capability, it just keeps giving me bunch of numbers, quite a lot of it! By using the above simple subroutines, eventually now I can generate this kind of data plot:

Sodium Temperature Profile

Simply speaking, it’s all about DATA VISUALIZATION, man!

One Response to “Some simple Delphi graphic subroutines”

  1. cool

    Syeilendra said..
    thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: