draw rectangle on LCD

unknown
hello,

this is modified part of camtest.cpp
this is for drawing rectangle on picture fetched from cmos sensor.
but only its horizontal lines, has plotted truly; vertical algorithm leads
to plotting slope line!!!
where is the problem?


//LineLen = 640 (pixels) x 2 (bytes)
//Height = 512 (pixels)
//count=Size=LineLen x Height (bytes)
bool FetchPicture() const {
  int count = ::read(fd, Addr, Size);
  if (count not_eq Size) {
     throw TError("error in fetching picture from video");
   }
   //new added code: draw rectangular
   int i=0;
   for(int c=0;c<count;c+=2){
      //horizontal lines : "truly draws horizontal line"
      if((c>=LineLen*Height/4+LineLen/3)&&
         (c<=LineLen*Height/4+LineLen*2/3)) 
              {*(Addr+c)=255;*(Addr+c+1)=255;} else
      if((c>=LineLen*Height*3/4+LineLen/3)&&
         (c<=LineLen*Height*3/4+LineLen*2/3)) 
              {*(Addr+c)=255;*(Addr+c+1)=255;}
      //vertical lines : "but only draws slope line !!!!!!!!!!!!!!"
      i+=2; if(i>LineLen){i=0;}
      if((i==LineLen*2/4)){*(Addr+c)=255;*(Addr+c+1)=255;}
    }
    return true;
 }

shankar
Hi
 Try bellow logic.....

static void Glib_FilledRectangle(int x1,int y1,int x2,int y2,int color)
{
    int i;

    for(i=y1;i<=y2;i++)
  Glib_Line(x1,i,x2,i,color);
}
static void Glib_Line(int x1,int y1,int x2,int y2,int color)
{
  int dx,dy,e;
  dx=x2-x1; 
  dy=y2-y1;
    
  if(dx>=0)
  {
    if(dy >= 0) // dy>=0
    {
      if(dx>=dy) // 1/8 octant
      {
        e=dy-dx/2;
        while(x1<=x2)
        {
          PutPixel(x1,y1,color);
          if(e>0){y1+=1;e-=dx;}  
          x1+=1;
          e+=dy;
        }
      }
      else    // 2/8 octant
      {
        e=dx-dy/2;
        while(y1<=y2)
        {
          PutPixel(x1,y1,color);
          if(e>0){x1+=1;e-=dy;}  
          y1+=1;
          e+=dx;
        }
      }
    }
    else       // dy<0
    {
      dy=-dy;   // dy=abs(dy)

      if(dx>=dy) // 8/8 octant
      {
        e=dy-dx/2;
        while(x1<=x2)
        {
          PutPixel(x1,y1,color);
          if(e>0){y1-=1;e-=dx;}  
          x1+=1;
          e+=dy;
        }
      }
      else    // 7/8 octant
      {
        e=dx-dy/2;
        while(y1>=y2)
        {
          PutPixel(x1,y1,color);
          if(e>0){x1+=1;e-=dy;}  
          y1-=1;
          e+=dx;
        }
      }
    }  
  }
  else //dx<0
  {
    dx=-dx;    //dx=abs(dx)
    if(dy >= 0) // dy>=0
    {
      if(dx>=dy) // 4/8 octant
      {
        e=dy-dx/2;
        while(x1>=x2)
        {
          PutPixel(x1,y1,color);
          if(e>0){y1+=1;e-=dx;}  
          x1-=1;
          e+=dy;
        }
      }
      else    // 3/8 octant
      {
        e=dx-dy/2;
        while(y1<=y2)
        {
          PutPixel(x1,y1,color);
          if(e>0){x1-=1;e-=dy;}  
          y1+=1;
          e+=dx;
        }
      }
    }
    else       // dy<0
    {
      dy=-dy;   // dy=abs(dy)

      if(dx>=dy) // 5/8 octant
      {
        e=dy-dx/2;
        while(x1>=x2)
        {
          PutPixel(x1,y1,color);
          if(e>0){y1-=1;e-=dx;}  
          x1-=1;
          e+=dy;
        }
      }
      else    // 6/8 octant
      {
        e=dx-dy/2;
        while(y1>=y2)
        {
          PutPixel(x1,y1,color);
          if(e>0){x1-=1;e-=dy;}  
          y1-=1;
          e+=dx;
        }
      }
    }  
  }
}
static void PutPixel(U32 x,U32 y,U32 c)
{
  if ( (x < SCR_XSIZE_TFT_320240) && (y < SCR_YSIZE_TFT_320240) )
  LCD_BUFER[(y)][(x)] = c;
}