TFT Library Improvement

General discussion on mikroPascal PRO for PIC32.
Post Reply
Author
Message
Sarman
Posts: 5
Joined: 30 Dec 2010 18:03

TFT Library Improvement

#1 Post by Sarman » 19 Apr 2013 16:45

It would be fine to include arc drawing in TFT Library. I'm please to provide one with Andres algorithm.

---------------------------------------------------------------------------------------
procedure Draw_Andres_Arc(x_center,y_center,radius,angle_start,angle_end, pen ,color : word);
var x,y,d,i :integer;
angle : real;
begin
For i := 0 to pen-1 do
begin
x:=0;
y:=radius+i;
d:= radius-1+i;
while y >= x do
begin
// Calculate the angle the current point makes with the circle center
angle := 360/2/pi*atan2(x,y);
// draw point in range 0 to 45 degrees
if ((90-angle) >= angle_start) and ((90-angle) <= angle_end) then TFT_Dot(y+x_center, -x+y_center, color);
// draw point in range 45 to 90 degrees
if ((angle) >= angle_start) and ((angle) <= angle_end) then TFT_Dot(x+x_center, -y+y_center, color);
// draw point in range 90 to 135 degrees
if ((180 - angle) >= angle_start) and ((180 - angle) <= angle_end) then TFT_Dot(-x+x_center, -y+y_center, color);
// draw point in range 135 to 180 degrees
if ((angle + 90) >= angle_start) and ((angle + 90) <= angle_end) then TFT_Dot(-y+x_center, -x+y_center, color);
// draw point in range 180 to 225 degrees
if ((270 - angle) >= angle_start) and ((270 - angle) <= angle_end) then TFT_Dot(-y+x_center, x+y_center, color);
// draw point in range 225 to 270 degrees
if ((angle + 180) >= angle_start) and ((angle + 180) <= angle_end) then TFT_Dot(-x+x_center, y+y_center, color);
// draw point in range 270 to 315 degrees
if ((360 - angle) >= angle_start) and ((360 - angle) <= angle_end) then TFT_Dot(x+x_center, y+y_center, color);
// draw point in range 315 to 360 degrees
if ((angle + 270) >= angle_start) and ((angle + 270) <= angle_end) then TFT_Dot(y+x_center, x+y_center, color);
If d >= 2*(x-1) then
begin
d:=d-2*x;
x:=x+1 ;
end
else
If d <= 2*(radius+i-y) then
begin
d:=d+2*y-1;
y:=y-1;
end
else
begin
d:=d+2*(y-x-1);
y:=y-1;
x:=x+1 ;
end;
end;
end;
end;
-------------------------------------------------------------------------------------------
Hope it would be helpfull for someone.

Sarman
Posts: 5
Joined: 30 Dec 2010 18:03

Re: TFT Library Improvement

#2 Post by Sarman » 20 Apr 2013 16:52

Hi,

As atan2 function is slow i update my arc drawing function (eg replace by atan2 array values). Ten times faster !

Code: Select all

  // value in degree x 10 of atan A x 100  ( 0<= A =< 1)
const    T : array [0..100] of integer = (0,6,11,17,23,29,34,40,46,51,57,63,68,74,80,85,91,96,102
,108,113,119,124,130,135,140,146,151,156,162,167,172,177,183,188,193,198,203,208,213,218
,223,228,233,237,242,247,252,256,261,266,270,275,279,284,288,292,297,301,305,310,314,318
,322,326,330,334,338,342,346,350,354,358,361,365,369,372,376,380,383,387,390,394,397,400
,404,407,410,413,417,420,423,426,429,432,435,438,441,444,447,450);

procedure Draw_Andres_Arc2(x_center,y_center,radius,angle_start,angle_end, pen ,color : word);
var x,y,d,i :integer;
    angle, angle_start_x10,angle_end_x10  : integer;
begin
  For i := 0 to pen-1 do
    begin
    angle_start_x10 :=angle_start*10;
    angle_end_x10 :=angle_end*10;
    x:=0;
    y:=radius+i;
    d:= radius-1+i;
    while y >= x do
      begin
        // Calculate the angle the current point makes with the circle center
      angle := 900-T[x*100 div y];
        // draw point in range 0 to 45 degrees
      if ((900-angle) >= angle_start_x10) and ((900-angle) <= angle_end_x10) then TFT_Dot(y+x_center, -x+y_center, color);
        // draw point in range 45 to 90 degrees
      if ((angle) >= angle_start_x10) and ((angle) <= angle_end_x10) then TFT_Dot(x+x_center, -y+y_center, color);
         // draw point in range 90 to 135 degrees
      if ((1800 - angle) >= angle_start_x10) and ((1800 - angle) <= angle_end_x10) then TFT_Dot(-x+x_center, -y+y_center, color);
         // draw point in range 135 to 180 degrees
      if ((angle + 900) >= angle_start_x10) and ((angle + 900) <= angle_end_x10) then TFT_Dot(-y+x_center, -x+y_center, color);
         // draw point in range 180 to 225 degrees
      if ((2700 - angle) >= angle_start_x10) and ((2700 - angle) <= angle_end_x10) then TFT_Dot(-y+x_center, x+y_center, color);
         // draw point in range 225 to 270 degrees
      if ((angle + 1800) >= angle_start_x10) and ((angle + 1800) <= angle_end_x10) then TFT_Dot(-x+x_center, y+y_center, color);
         // draw point in range 270 to 315 degrees
      if ((3600 - angle) >= angle_start_x10) and ((3600 - angle) <= angle_end_x10) then TFT_Dot(x+x_center, y+y_center, color);
         // draw point in range 315 to 360 degrees
      if ((angle + 2700) >= angle_start_x10) and ((angle + 2700) <= angle_end_x10) then TFT_Dot(y+x_center, x+y_center, color);
      If d >= 2*(x-1)  then
        begin
        d:=d-2*x;
        x:=x+1 ;
        end
        else
        If d <= 2*(radius+i-y)  then
          begin
          d:=d+2*y-1;
          y:=y-1;
          end
          else
            begin
            d:=d+2*(y-x-1);
            y:=y-1;
            x:=x+1 ;
            end;
      end;
  end;
end;

veljko.pavlovic
Posts: 59
Joined: 15 Apr 2013 11:19

Re: TFT Library Improvement

#3 Post by veljko.pavlovic » 22 Apr 2013 14:53

Hello Sarman,

Thank you for this sharing.
I believe it is a very useful code and could be used in fine purpose.

Best regards,
Veljko Pavlovic.

Post Reply

Return to “mikroPascal PRO for PIC32 General”