Hello again, i am trying to write a function to draw a line from (x1,y1) to (x2,y2) on a GLCD. What I have so far only works if the slope of the line is 1 and x1,y1 values are less than x2,y2 values. Please be patient with me, lol.
Code:
void dline(unsigned char x1, unsigned char x2,unsigned char y1,unsigned char y2){
signed int dx,dy;
double slope,draw;
//plot start
plot(x1,y1);
//find delta and delta y
dx=(x2-x1);
dy=(y2-y1);
//find slope
slope = dx/dy;
//draw the line
while(dx--){
for(draw=0;draw<=(slope-1);draw++)
{
plot(x1,y1);
y1++;
}
x1++;
}
}
Google for Bresenham's algorithm. Even looking for just the basic one should get you a version that will handle any angle, and drawing from either end.
Then start digging for all the improved versions that have been made. The improved ones are MUCH faster.
If you can, get yourself a copy of this old book. I see there's some used copies available for cheap on Amazon. It goes over all the very basic graphic algo's in pretty good detail, progressing to improved versions as you learn how the simpler ones work. I bought it when it was new, for MUCH, MUCH more than you're going to pay.
Thanks for the replies. It looks like I have a lot more reading to do. For some reason, it does not seem like there is a lot of info on the net regarding glcd's and pics.
Thanks for the replies. It looks like I have a lot more reading to do. For some reason, it does not seem like there is a lot of info on the net regarding glcd's and pics.
There isn't much out there, which is why I'm posting my reckless adventures online. But graphic algorithms are the same whether your screen is 128x64 or 1680x1050.
I've got an old book somewhere, in 6502 assembler, which gave routines for all kinds of text and graphic manipulation - there are probably various old books from back then.
Hello again, i am trying to write a function to draw a line from (x1,y1) to (x2,y2) on a GLCD. What I have so far only works if the slope of the line is 1 and x1,y1 values are less than x2,y2 values. Please be patient with me, lol.
Hey superbrew. If you don't mind cheating, I borrowed and ported to C a piece of BlitzMax BASIC code I borrowed off the interweb. It works perfect on my GLCD. Just now got the last buglets out of it. It's pretty quick. All integers. If you want it, just say so.
Thanks guys for the offers. I don't actually need this code, I am just trying to learn how do some stuff with a GLCD. MikroC has some nice functions for drawing lines and circles, but you don't have access to how they do it. I think from the information given I should be able to fiure it out.
Easy. Just XOR the same bits again. See the **broken link removed** on my site. It's not clearing the screen between moves (though that might be quicker ). It's erasing (redrawing in background color) the line just before drawing the new one.
Easy. Just XOR the same bits again. See the **broken link removed** on my site. It's not clearing the screen between moves (though that might be quicker ). It's erasing (redrawing in background color) the line just before drawing the new one.