Graphical LCD Graphics functions

Status
Not open for further replies.

Gobbledok

Active Member
Hi guys,

At request I decided to write a thread with some basic library functions for any graphic LCD screen.


DRAW CIRCLE

First up will be a basic routine to draw a circle. Full and total credit for this routine goes to Pommie. I pinched it out of a thread here somewhere.

- Requirements: A PutPixel Function. The PutPixel function must check and ignore if the pixel is off-screen.

Code:
//Function Prototype
void DrawCircle(unsigned int xpos, unsigned char ypos, unsigned int R, unsigned int color);


// Routine
void DrawCircle(unsigned int xpos, unsigned char ypos, unsigned int R, unsigned int color){
unsigned int y,x;
signed int p;
    x=0;
    y=R;
    p= -(R/2);
    Plot8(xpos,ypos,x,y, color);
    while(x<y){
        x++;
        if(p<0)
            p=p+2*x+1;
        else{
            y--;
            p=p+2*(x-y)+1;
        }
        Plot8(xpos, ypos, x, y, color);
    }
}
void Plot8(unsigned char x,unsigned char y,unsigned char dx,unsigned char dy, unsigned int color){
    PutPixel(x + dx, y + dy, color);
    PutPixel(x + dx, y - dy, color);
    PutPixel(x - dx, y + dy, color);
    PutPixel(x - dx, y - dy, color);
    PutPixel(x + dy, y + dx, color);
    PutPixel(x + dy, y - dx, color);
    PutPixel(x - dy, y + dx, color);
    PutPixel(x - dy, y - dx, color);
}


// example
DrawCircle(63, 32, 20, Red);

If you only have a mono LCD, then remove all references to 'color' out of the prototype and out of the function.
 
Last edited:
DRAW LINE

The next function will draw a line from point (x,y) to point. From memory this came from somewhere in the interwebz (not here). If it is yours then let me know and I will credit you.

- Requirements: A PutPixel Function. The PutPixel function must check and ignore if the pixel is off-screen. abs() library math function.

Code:
//Function Prototype
void DrawLine(signed int x1, signed int y1, signed int x2, signed int y2, unsigned int color);


//Routine
void DrawLine(signed int x1, signed int y1, signed int x2, signed int y2, unsigned int color)
{
signed int x, y, xinc1, xinc2, yinc1, yinc2, den, num, numadd, numpixels, curpixel, deltax, deltay;

	deltax = abs(x2 - x1);        // The difference between the x's
	deltay = abs(y2 - y1);        // The difference between the y's
	x = x1;                       // Start x off at the first pixel
	y = y1;                       // Start y off at the first pixel
	
	if (x2 >= x1)                 // The x-values are increasing
	{
	  xinc1 = 1;
	  xinc2 = 1;
	}
	else                          // The x-values are decreasing
	{
	  xinc1 = -1;
	  xinc2 = -1;
	}
	
	if (y2 >= y1)                 // The y-values are increasing
	{
	  yinc1 = 1;
	  yinc2 = 1;
	}
	else                          // The y-values are decreasing
	{
	  yinc1 = -1;
	  yinc2 = -1;
	}
	
	if (deltax >= deltay)         // There is at least one x-value for every y-value
	{
	  xinc1 = 0;                  // Don't change the x when numerator >= denominator
	  yinc2 = 0;                  // Don't change the y for every iteration
	  den = deltax;
	  num = deltax / 2;
	  numadd = deltay;
	  numpixels = deltax;         // There are more x-values than y-values
	}
	else                          // There is at least one y-value for every x-value
	{
	  xinc2 = 0;                  // Don't change the x for every iteration
	  yinc1 = 0;                  // Don't change the y when numerator >= denominator
	  den = deltay;
	  num = deltay / 2;
	  numadd = deltax;
	  numpixels = deltay;         // There are more y-values than x-values
	}
	
	for (curpixel = 0; curpixel <= numpixels; curpixel++)
	{
	  PutPixel(x, y, color);	  // Draw the current pixel
	  num += numadd;              // Increase the numerator by the top of the fraction
	  if (num >= den)             // Check if numerator >= denominator
	  {
	    num -= den;               // Calculate the new numerator value
	    x += xinc1;               // Change the x as appropriate
	    y += yinc1;               // Change the y as appropriate
	  }
	  x += xinc2;                 // Change the x as appropriate
	  y += yinc2;                 // Change the y as appropriate
	}
}


// example
DrawLine(0, 0, 127, 63, Red);

Once again for a mono screen ignore all references to color.
 
Last edited:
DRAW LINE ANGLE

This function will draw a line from any point (x,y), and, given an angle and a length will draw the line.

- Requirements: The 'DrawLine' function above. cosf() and sinf library math functions.

May be slow on a slow chip.


Code:
// Function Prototype
void DrawLineAngle(signed int startx, signed int starty, unsigned int length, unsigned int angle, unsigned int color)


// Routine
void DrawLineAngle(signed int startx, signed int starty, unsigned int length, unsigned int angle, unsigned int color)
{
		DrawLine(startx, starty, (startx + length * cosf(angle * 0.017453292519)), (starty + length * sinf(angle * 0.017453292519)), color);
}


// example
DrawLineAngle(0, 0, 100, 45, Red);
 
DRAW BOX

This function draws a single-pixel width box on the screen.

- Requirements: The 'DrawLine' function above.

Code:
// Function Prototype
void DrawBox(unsigned int startx,unsigned int starty, unsigned int endx, unsigned int endy, unsigned int color);


// Routine
void DrawBox(unsigned int startx,unsigned int starty, unsigned int endx, unsigned int endy, unsigned int color)
{
	DrawLine(startx,starty,startx,endy,color);
	DrawLine(endx,endy,startx,endy, color);
	DrawLine(startx,starty,endx,starty, color);
	DrawLine(endx,endy,endx,starty, color);
}


// Example
DrawBox(0, 0, 127, 63, Red);



That's all I have for now, perhaps if someone has a routine for drawing filled-in boxes or circles (or any other graphic routine) they could chime in.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…