Bresehham's Line Drawing Algorithm and PID - c

Does anyone know that if there is a modified version of Bresehham's line algorithm that uses feedback control with PID filters? Basically, the algorithm is just a P-Feedback Control for the error term amplified by half. I looked at the Graphics Gems series, Abrash's book,..etc can't find any yet

Here it is: From Here. This uses the P parameter (see steps in pseudo code shown at top of link)
The provided example code was written in an old environment (references to TurboC in code) and contains functions that will have to be written by you. eg. initgraph(), putpixel(), etc. But the algorithm appears to be complete.
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
#include <dos.h>
int main() {
/* request auto detection */
int gdriver = DETECT, gmode;
int x1 = 0, y1 = 0, x2, y2;
int err, x, y, dx, dy, dp, xEnd;
int twody, twodxdy;
/* initialize graphic driver */
initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");
err = graphresult();
if (err != grOk) {
/* error occurred */
printf("Graphics Error: %s\n",
grapherrormsg(err));
return 0;
}
/* max position in x and y axis */
x2 = getmaxx();
y2 = getmaxy();
/* draws line from (0, 0) to (x2, y2) */
dx = x2 - x1;
dy = y2 - y1;
twody = 2 * dy;
twodxdy = 2 * (dy - dx);
dp = twody - dx;
if (x1 > x2) {
x = x2;
y = y2;
xEnd = x1;
} else {
x = x1;
y = y1;
xEnd = x2;
}
/* put a dot at the position (x, y) */
putpixel(x, y, WHITE);
/* calculate x and y successor and plot the points */
while (x < xEnd) {
x = x + 1;
if (dp < 0) {
dp = dp + twody;
} else {
y = y + 1;
dp = dp + twodxdy;
}
/* put a dot at the given position(x, y) */
putpixel(x, y, WHITE);
/* sleep for 50 milliseconds */
delay(50);
}
getch();
/* deallocate memory allocated for graphic screen */
closegraph();
return 0;
}

Related

How to draw circles, arcs and vector graphics in SDL?

I'm using SDL2.
The only way I can find to draw a shape is with the line, rect and pixel functions, as explained here.
Apart from using trig or the "equation of a circle", how could I draw a curve? How about general vector graphics?
Is SDL an appropriate starting point or should I look elsewhere?
This is an example of the Midpoint Circle Algorithm as referenced above. It doesn't require a math library and is very fast. (Renders in about 500 microseconds) This is what Windows uses/used to rasterize circles.
void DrawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32_t radius)
{
const int32_t diameter = (radius * 2);
int32_t x = (radius - 1);
int32_t y = 0;
int32_t tx = 1;
int32_t ty = 1;
int32_t error = (tx - diameter);
while (x >= y)
{
// Each of the following renders an octant of the circle
SDL_RenderDrawPoint(renderer, centreX + x, centreY - y);
SDL_RenderDrawPoint(renderer, centreX + x, centreY + y);
SDL_RenderDrawPoint(renderer, centreX - x, centreY - y);
SDL_RenderDrawPoint(renderer, centreX - x, centreY + y);
SDL_RenderDrawPoint(renderer, centreX + y, centreY - x);
SDL_RenderDrawPoint(renderer, centreX + y, centreY + x);
SDL_RenderDrawPoint(renderer, centreX - y, centreY - x);
SDL_RenderDrawPoint(renderer, centreX - y, centreY + x);
if (error <= 0)
{
++y;
error += ty;
ty += 2;
}
if (error > 0)
{
--x;
tx += 2;
error += (tx - diameter);
}
}
}
If you want to write your own circle drawing function, then I'd suggest adapting the midpoint algorithm to SDL2 by drawing pixels.
Curves would be done similarly, but would use more of an ellipses drawing algorithm.
Actual vector graphics start to get much more complicated, and you'd probably have to find something that renders SVG files, which I'm not sure there are many options for SDL2.
However, if you would rather simply have functions that you can work with I'd suggest going straight to SDL2_gfx instead. It has many more functions already implemented for you to work with.
SDL allows for third party libs to draw on a texture. If cairo was desirable, it could be used in a function like this:
cairo_t*cb(cairo_t*cr)
{cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
cairo_rectangle(cr, 10, 20, 128, 128);
cairo_stroke(cr);
return cr;
}
then cb can be passed to this function:
cairo_t*cai(SDL_Window*w,SDL_Renderer*r,cairo_t*(*f)(cairo_t*))
{int width, height, pitch;void *pixels;
SDL_GetWindowSize(w, &width, &height);
SDL_Texture*t=SDL_CreateTexture(r,SDL_PIXELFORMAT_ARGB8888,SDL_TEXTUREACCESS_STREAMING,width,height);
SDL_LockTexture(t, NULL, &pixels, &pitch);
cairo_surface_t *cs=cairo_image_surface_create_for_data(pixels,CAIRO_FORMAT_ARGB32,width,height,pitch);
cairo_t*s=cairo_create(cs);
cairo_t*fr=f(s);SDL_UnlockTexture(t);SDL_RenderCopy(r,t,NULL,NULL);SDL_RenderPresent(r);
return fr;
}
If you want to do a circle or ellipse without 3rd party libraries, include math.h and use the function below I wrote. It will draw aliased ellipse or circles very well. Tested on SDL 2.0.2 and works. It draws one quadrant arc, and mirrors the other arcs, reducing calls to cosf and sinf.
//draw one quadrant arc, and mirror the other 4 quadrants
void sdl_ellipse(SDL_Renderer* r, int x0, int y0, int radiusX, int radiusY)
{
float pi = 3.14159265358979323846264338327950288419716939937510;
float pih = pi / 2.0; //half of pi
//drew 28 lines with 4x4 circle with precision of 150 0ms
//drew 132 lines with 25x14 circle with precision of 150 0ms
//drew 152 lines with 100x50 circle with precision of 150 3ms
const int prec = 27; // precision value; value of 1 will draw a diamond, 27 makes pretty smooth circles.
float theta = 0; // angle that will be increased each loop
//starting point
int x = (float)radiusX * cos(theta);//start point
int y = (float)radiusY * sin(theta);//start point
int x1 = x;
int y1 = y;
//repeat until theta >= 90;
float step = pih/(float)prec; // amount to add to theta each time (degrees)
for(theta=step; theta <= pih; theta+=step)//step through only a 90 arc (1 quadrant)
{
//get new point location
x1 = (float)radiusX * cosf(theta) + 0.5; //new point (+.5 is a quick rounding method)
y1 = (float)radiusY * sinf(theta) + 0.5; //new point (+.5 is a quick rounding method)
//draw line from previous point to new point, ONLY if point incremented
if( (x != x1) || (y != y1) )//only draw if coordinate changed
{
SDL_RenderDrawLine(r, x0 + x, y0 - y, x0 + x1, y0 - y1 );//quadrant TR
SDL_RenderDrawLine(r, x0 - x, y0 - y, x0 - x1, y0 - y1 );//quadrant TL
SDL_RenderDrawLine(r, x0 - x, y0 + y, x0 - x1, y0 + y1 );//quadrant BL
SDL_RenderDrawLine(r, x0 + x, y0 + y, x0 + x1, y0 + y1 );//quadrant BR
}
//save previous points
x = x1;//save new previous point
y = y1;//save new previous point
}
//arc did not finish because of rounding, so finish the arc
if(x!=0)
{
x=0;
SDL_RenderDrawLine(r, x0 + x, y0 - y, x0 + x1, y0 - y1 );//quadrant TR
SDL_RenderDrawLine(r, x0 - x, y0 - y, x0 - x1, y0 - y1 );//quadrant TL
SDL_RenderDrawLine(r, x0 - x, y0 + y, x0 - x1, y0 + y1 );//quadrant BL
SDL_RenderDrawLine(r, x0 + x, y0 + y, x0 + x1, y0 + y1 );//quadrant BR
}
}
My answer extends Scotty Stephens answer by making it a bunch more performant by reducing the API calls to a single one.
// rounding helper, simplified version of the function I use
int roundUpToMultipleOfEight( int v )
{
return (v + (8 - 1)) & -8;
}
void DrawCircle( SDL_Renderer * renderer, SDL_Point center, int radius )
{
// 35 / 49 is a slightly biased approximation of 1/sqrt(2)
const int arrSize = roundUpToMultipleOfEight( radius * 8 * 35 / 49 );
SDL_Point points[arrSize];
int drawCount = 0;
const int32_t diameter = (radius * 2);
int32_t x = (radius - 1);
int32_t y = 0;
int32_t tx = 1;
int32_t ty = 1;
int32_t error = (tx - diameter);
while( x >= y )
{
// Each of the following renders an octant of the circle
points[drawCount+0] = { center.x + x, center.y - y };
points[drawCount+1] = { center.x + x, center.y + y };
points[drawCount+2] = { center.x - x, center.y - y };
points[drawCount+3] = { center.x - x, center.y + y };
points[drawCount+4] = { center.x + y, center.y - x };
points[drawCount+5] = { center.x + y, center.y + x };
points[drawCount+6] = { center.x - y, center.y - x };
points[drawCount+7] = { center.x - y, center.y + x };
drawCount += 8;
if( error <= 0 )
{
++y;
error += ty;
ty += 2;
}
if( error > 0 )
{
--x;
tx += 2;
error += (tx - diameter);
}
}
SDL_RenderDrawPoints( renderer, points, drawCount );
}
A circle of radius 141 would have had 800 SDL_RenderDrawPoint calls in Scottys version, this new version does only execute one single SDL_RenderDrawPoints call, making it much more performant.
One could also strip the rendering portion out of this function, to allow the result to be cached and reused like shown below.
std::vector<SDL_Point> PixelizeCircle( SDL_Point center, int radius )
{
std::vector<SDL_Point> points;
// 35 / 49 is a slightly biased approximation of 1/sqrt(2)
const int arrSize = roundUpToMultipleOfEight( radius * 8 * 35 / 49 );
points.reserve( arrSize );
const int32_t diameter = (radius * 2);
int32_t x = (radius - 1);
int32_t y = 0;
int32_t tx = 1;
int32_t ty = 1;
int32_t error = (tx - diameter);
while( x >= y )
{
// Each of the following renders an octant of the circle
points.push_back( { center.x + x, center.y - y } );
points.push_back( { center.x + x, center.y + y } );
points.push_back( { center.x - x, center.y - y } );
points.push_back( { center.x - x, center.y + y } );
points.push_back( { center.x + y, center.y - x } );
points.push_back( { center.x + y, center.y + x } );
points.push_back( { center.x - y, center.y - x } );
points.push_back( { center.x - y, center.y + x } );
if( error <= 0 )
{
++y;
error += ty;
ty += 2;
}
if( error > 0 )
{
--x;
tx += 2;
error += (tx - diameter);
}
}
return points; // RVO FTW
}
int main()
{
std::vector<SDL_Point> circle = PixelizeCircle( SDL_Point{ 84, 72 }, 79 );
//...
while( true )
{
//...
SDL_RenderDrawPoints( renderer, circle.data(), circle.size() );
//...
}
}

SDL2 How to draw dotted line

Is is just possible to draw a simple dotted line using SDL2 (or with gfx) like
int drawDottedLine(SDL_Renderer *renderer,Sint16 x1,Sint16 y1, Sint16 x2, Sint16 y2, int r, int g, int b, int a);
found absolutely nothing on the web wtf is it so hard ?
Here is a working function, that uses the Bresenham algorithm:
void DrawDottedLine(SDL_Renderer* renderer, int x0, int y0, int x1, int y1) {
int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
int dy = -abs(y1-y0), sy = y0<y1 ? 1 : -1;
int err = dx+dy, e2;
int count = 0;
while (1) {
if (count < 10) {SDL_RenderDrawPoint(renderer,x0,y0);}
if (x0==x1 && y0==y1) break;
e2 = 2*err;
if (e2 > dy) { err += dy; x0 += sx; }
if (e2 < dx) { err += dx; y0 += sy; }
count = (count + 1) % 20;
}
}
You must consider that this function has terrible performance, because every point of the dashed line will call SDL_RenderDrawPoint() in order to get rendered.
Here is the code I used for my pong game:
SDL_SetRenderDrawColor(renderer, 155, 155, 155, 255);
for (line.y = 0; line.y < WINDOW_HEIGHT; line.y += 10)
{
SDL_RenderFillRect(renderer, &line);
}
Earlier in the code I initialized the line:
SDL_Rect line;
line.w = 2;
line.h = 8;
line.x = WINDOW_WIDTH / 2;
You can simply implement it yourself ...
Check the "Bresenham algorithm" for draw a line.
For a doted line, it's just many full line, so a pencil and paper with trigonometry should work out :)
Edit : For a dotted line, you even don't have the use to the "Bresenham algorithm", you just need trigonometry.
And by the way, for those who have downvoted, explain yourself ?

Can this line drawing algorithm be optimized? - SDL

For a project I have been working on, the ability to draw lines with a gradient (I.E. they change color over the interval they are drawn) would be very useful. I have an algorithm for this, as I will paste below, but it turns out to be DREADFULLY slow. I'm using the Bresenham algorithm to find each point, but I fear that I have reached the limits of software rendering. I've been using SDL2 thus far, and my line drawing algorithm appears 200x slower than SDL_RenderDrawLine. This is an estimate, and gathered from comparing the two functions' times to draw 10,000 lines. My function would take near 500ms, and SDL_RenderDrawLine did it in 2-3ms on my machine. I even tested the functions with horizontal lines to ensure it wasn't just a botched Bresenham algorithm, and similar slowness hatched. Unfortunately, SDL doesn't have an API for drawing lines with a gradient (or if it does, I'm blind). I knew that any software rendering would be significantly slower than hardware, but the shear magnitude of slowness caught me by surprise. Is there a method that can be used to speed this up? Have I just botched the drawing system beyond reason? I've considered saving an array of the pixels I wish to draw and then shoving them to the screen all at once, but I don't know how to do this with SDL2 and I can't seem to find the API in the wiki or documentation that allows for this. Would that even be faster?
Thanks for the consideration!
void DRW_LineGradient(SDL_Renderer* rend, SDL_Color c1, int x1, int y1, SDL_Color c2, int x2, int y2){
Uint8 tmpr, tmpg, tmpb, tmpa;
SDL_GetRenderDrawColor(rend, &tmpr, &tmpg, &tmpb, &tmpa);
int dy = y2 - y1;
int dx = x2 - x1;
/* Use doubles for a simple gradient */
double d = (abs(x1 - x2) > abs(y1 - y2) ? abs(x1 - x2) : abs(y1 - y2));
double dr = (c2.r - c1.r) / d;
double dg = (c2.g - c1.g) / d;
double db = (c2.b - c1.b) / d;
double da = (c2.a - c1.a) / d;
double r = c1.r, g = c1.g, b = c1.b, a = c1.a;
/* The line is vertical */
if (dx == 0) {
int y;
if (y2 >= y1) {
for (y = y1; y <= y2; y++) {
SDL_SetRenderDrawColor(rend, r, g, b, a);
SDL_RenderDrawPoint(rend, x1, y);
r += dr;
g += dg;
b += db;
a += da;
}
return;
}
else{
for (y = y1; y >= y2; y--) {
SDL_SetRenderDrawColor(rend, r, g, b, a);
SDL_RenderDrawPoint(rend, x1, y);
r += dr;
g += dg;
b += db;
a += da;
}
return;
}
}
/* The line is horizontal */
if (dy == 0) {
int x;
if (x2 >= x1) {
for (x = x1; x <= x2; x++) {
SDL_SetRenderDrawColor(rend, r, g, b, a);
SDL_RenderDrawPoint(rend, x, y1);
r += dr;
g += dg;
b += db;
a += da;
}
return;
}
else{
for (x = x1; x >= x2; x--) {
SDL_SetRenderDrawColor(rend, r, g, b, a);
SDL_RenderDrawPoint(rend, x, y1);
r += dr;
g += dg;
b += db;
a += da;
}
return;
}
}
/* The line has a slope of 1 or -1 */
if (abs(dy) == abs(dx)) {
int xmult = 1, ymult = 1;
if (dx < 0) {
xmult = -1;
}
if (dy < 0) {
ymult = -1;
}
int x = x1, y = y1;
do {
SDL_SetRenderDrawColor(rend, r, g, b, a);
SDL_RenderDrawPoint(rend, x, y);
x += xmult;
y += ymult;
r += dr;
g += dg;
b += db;
a += da;
} while (x != x2);
return;
}
/* Use bresenham's algorithm to render the line */
int checky = dx >> 1;
int octant = findOctant((Line){x1, y1, x2, y2, dx, dy});
dy = abs(dy);
dx = abs(dx);
x2 = abs(x2 - x1) + x1;
y2 = abs(y2 - y1) + y1;
if (octant == 1 || octant == 2 || octant == 5 || octant == 6) {
int tmp = dy;
dy = dx;
dx = tmp;
}
int x, y = 0;
for (x = 0; x <= dx; x++) {
SDL_SetRenderDrawColor(rend, r, g, b, a);
switch (octant) {
case 0:
SDL_RenderDrawPoint(rend, x + x1, y + y1);
break;
case 1:
SDL_RenderDrawPoint(rend, y + x1, x + y1);
break;
case 2:
SDL_RenderDrawPoint(rend, -y + x1, x + y1);
break;
case 3:
SDL_RenderDrawPoint(rend, -x + x1, y + y1);
break;
case 4:
SDL_RenderDrawPoint(rend, -x + x1, -y + y1);
break;
case 5:
SDL_RenderDrawPoint(rend, -y + x1, -x + y1);
break;
case 6:
SDL_RenderDrawPoint(rend, y + x1, -x + y1);
break;
case 7:
SDL_RenderDrawPoint(rend, x + x1, -y + y1);
break;
default:
break;
}
checky += dy;
if (checky >= dx) {
checky -= dx;
y++;
}
r += dr;
g += dg;
b += db;
a += da;
}
SDL_SetRenderDrawColor(rend, tmpr, tmpg, tmpb, tmpa);
}
SIDE NOTE:
I am reluctant to just move on to using OpenGL 3.0+ (Which I hear SDL2 has support for) because I don't know how to use it. Most tutorials I have found have explained the process of setting up the contexts with SDL and then coloring the screen one solid color, but then stop before explaining how to draw shapes and such. If someone could offer a good place to start learning about this, that would also be extremely helpful.
A lot of the overhead of your function is in repeated calls to SDL_RenderDrawPoint. This is (most likely) a generic function which needs to do the following operations:
check if x and y are in range for your current surface;
calculate the position inside the surface by multiplying y with surface->pitch and x with surface->format->BytesPerPixel;
check the current color model of the surface using SDL_PixelFormat;
convert the provided color to the correct format for this color model.
All of the above must be done for each single pixel. In addition, calling a function in itself is overhead -- small as it may be, it still needs to be done for each separate pixel, even if it is not visible.
You can:
omit x and y range checking if you are sure the line start and end points are always visible;
omit the convert-to-address step by calculating it once for the start of the line, then updating it by adding BytesPerPixel and pitch for a horizontal or vertical movement;
omit the convert-to-color model step by calculating the correct RGB values once (well, for a single color line, at least -- it's a bit harder for a gradient);
omit the function call by inlining the code to set a single pixel inside the line routine.
Another -- smaller -- issue: you call your own routine "Bresenham's ... but it isn't. Bresenham's optimization is actually that it avoids double calculations entirely (and its strongest point is that it still gives the mathematically correct output; something I would not count on when using double variables...).
The following routine does not check for range, color model, color values, or (indeed) if the surface should be locked. All of these operations should be ideally done outside the tight drawing loop. As it is, it assumes a 24-bit RGB color screen, with the Red byte first. [*]
I wrote this code for my current SDL environs, which is still SDL-1.0, but it should work for newer versions as well.
It is possible to use Bresenham's calculations for the delta-Red, delta-Green, and delta-Blue values as well, but I deliberately omitted them here :) They would add a lot of extra variables -- at a guess, three per color channel --, extra checks, and, not least of all, not really a visibly better quality. The difference between two successive values for Red, say 127 and 128, are usually too small to notice in a single pixel wide line. Besides, this small step would only occur if your line is at least 256 pixels long and you cover the entire range of Red from 0 to 255 in the gradient.
[*] If you are 100% sure you are targeting a specific screen model with your own program, you can use this (adjusted for that particular screen model, of course). It's feasible to target a couple of different screen models as well; write a customized routine for each, and use a function pointer to call the correct one.
Most likely this is how SDL_RenderDrawLine is able to squeeze out every millisecond of performance. Well worth writing all that code for a library (which will be used on a wide variety of screen set-ups), but most likely not for a single program such as yours. Notice I commented out a single range check, which falls back to a plain line routine if necessary. You could do the same for unusual or unexpected screen set-ups, and in that case simply call your own, slower, drawing routine. (Your routine is more robust as it uses SDL's native routines.)
The original line routine below was copied from The Internet more than a single decade ago, as I have been using it for ages. I'd gladly attribute it to someone; if anybody recognizes the comments (they are mostly as appeared in the original code), do post a comment.
void gradient_line (int x1,int y1,int x2,int y2,
int r1,int g1, int b1,
int r2,int g2, int b2)
{
int d; /* Decision variable */
int dx,dy; /* Dx and Dy values for the line */
int Eincr,NEincr; /* Decision variable increments */
int t; /* Counters etc. */
unsigned char *ScrPos;
int LineIncr;
int rd,gd,bd;
if (x1 < 0 || y1 < 0 || x2 < 0 || y2 < 0 ||
x1 >= SCREEN_WIDE || x2 >= SCREEN_WIDE ||
y1 >= SCREEN_HIGH || y2 >= SCREEN_HIGH)
{
line (x1,y1, x2,y2, (r1<<16)+(g1<<8)+b1);
return;
}
rd = (r2-r1)<<8;
gd = (g2-g1)<<8;
bd = (b2-b1)<<8;
dx = x2 - x1;
if (dx < 0)
dx = -dx;
dy = y2 - y1;
if (dy < 0)
dy = -dy;
if (dy <= dx)
{
/* We have a line with a slope between -1 and 1
*
* Ensure that we are always scan converting the line from left to
* right to ensure that we produce the same line from P1 to P0 as the
* line from P0 to P1.
*/
if (x2 < x1)
{
t = x2; x2 = x1; x1 = t; /* Swap X coordinates */
t = y2; y2 = y1; y1 = t; /* Swap Y coordinates */
/* Swap colors */
r1 = r2;
g1 = g2;
b1 = b2;
rd = -rd;
gd = -gd;
bd = -bd;
}
r1 <<= 8;
g1 <<= 8;
b1 <<= 8;
if (y2 > y1)
{
LineIncr = screen->pitch;
} else
{
LineIncr = -screen->pitch;
}
d = 2*dy - dx; /* Initial decision variable value */
Eincr = 2*dy; /* Increment to move to E pixel */
NEincr = 2*(dy - dx); /* Increment to move to NE pixel */
ScrPos = (unsigned char *)(screen->pixels+screen->pitch*y1+x1*screen->format->BytesPerPixel);
rd /= dx;
gd /= dx;
bd /= dx;
/* Draw the first point at (x1,y1) */
ScrPos[0] = r1 >> 8;
ScrPos[1] = g1 >> 8;
ScrPos[2] = b1 >> 8;
r1 += rd;
g1 += gd;
b1 += bd;
/* Incrementally determine the positions of the remaining pixels */
for (x1++; x1 <= x2; x1++)
{
if (d < 0)
{
d += Eincr; /* Choose the Eastern Pixel */
} else
{
d += NEincr; /* Choose the North Eastern Pixel */
ScrPos += LineIncr;
}
ScrPos[0] = r1>>8;
ScrPos[1] = g1>>8;
ScrPos[2] = b1>>8;
ScrPos += screen->format->BytesPerPixel;
r1 += rd;
g1 += gd;
b1 += bd;
}
} else
{
/* We have a line with a slope between -1 and 1 (ie: includes
* vertical lines). We must swap our x and y coordinates for this.
*
* Ensure that we are always scan converting the line from left to
* right to ensure that we produce the same line from P1 to P0 as the
* line from P0 to P1.
*/
if (y2 < y1)
{
t = x2; x2 = x1; x1 = t; /* Swap X coordinates */
t = y2; y2 = y1; y1 = t; /* Swap Y coordinates */
/* Swap colors */
r1 = r2;
g1 = g2;
b1 = b2;
rd = -rd;
gd = -gd;
bd = -bd;
}
r1 <<= 8;
g1 <<= 8;
b1 <<= 8;
if (x2 > x1)
{
LineIncr = screen->format->BytesPerPixel;
} else
{
LineIncr = -screen->format->BytesPerPixel;
}
d = 2*dx - dy; /* Initial decision variable value */
Eincr = 2*dx; /* Increment to move to E pixel */
NEincr = 2*(dx - dy); /* Increment to move to NE pixel */
rd /= dy;
gd /= dy;
bd /= dy;
/* Draw the first point at (x1,y1) */
ScrPos = (unsigned char *)(screen->pixels+screen->pitch*y1+x1*screen->format->BytesPerPixel);
ScrPos[0] = r1 >> 8;
ScrPos[1] = g1 >> 8;
ScrPos[2] = b1 >> 8;
r1 += rd;
g1 += gd;
b1 += bd;
/* Incrementally determine the positions of the remaining pixels
*/
for (y1++; y1 <= y2; y1++)
{
ScrPos += screen->pitch;
if (d < 0)
{
d += Eincr; /* Choose the Eastern Pixel */
} else
{
d += NEincr; /* Choose the North Eastern Pixel */
ScrPos += LineIncr; /* (or SE pixel for dx/dy < 0!) */
}
ScrPos[0] = r1 >> 8;
ScrPos[1] = g1 >> 8;
ScrPos[2] = b1 >> 8;
r1 += rd;
g1 += gd;
b1 += bd;
}
}
}
.. and this is a section of a screenful of random lines with random colors, with on the right a close-up:
I did not time the difference between "native" SDL line drawing, your naive method, a pure solid color Bresenham's implementation and this one; then again, this ought not be very much slower than an SDL native line.

opengl: how to keep objects in window when it's resized

I'm working a MS paint-like application on OpenGL using Bresenham's midpoint algorithm as homework. So far I can draw lines and ellipses. I lose them all when resizing the window. How can I keep them drawn?
Full code:
#include "GL/glut.h"
#include <stdio.h>
#include <math.h>
int i;
//int mainWindow, subWindow;
int X1, Y1, X2, Y2;
int modoDeDibujo;
int W = 1000, H = 1000;
/*void menuApp (int value)
{
if (value == 1) printf("Linea\n");
if (value == 2) printf("Circulo\n");
if (value == 3) printf("Elipsis\n");
if (value == 4) exit(0);
}
void crearMenu()
{
//inicio Creando el menu
int submenu;
submenu = glutCreateMenu(menuApp);
glutAddMenuEntry("Linea", 1);
glutAddMenuEntry("Elipse",3);
glutAddMenuEntry("Salir",4);
glutCreateMenu(menuApp);
glutAddSubMenu("SubMenu", submenu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
//fin Creando el menu
}*/
void renderPoint(void) /*REVISAR ESTO*/
{
glClear (GL_COLOR_BUFFER_BIT);
glBegin (GL_POINTS);
glVertex2f (-0.98, 0.98);
glEnd ();
glFlush ();
}
void renderPoint(double x, double y)
{
//printf("BEFORE TRANSFORM %f\t%f\t# renderPoint\n", x, y);
W = glutGet(GLUT_WINDOW_WIDTH);
H = glutGet(GLUT_WINDOW_HEIGHT);
float X;
float Y;
glBegin (GL_POINTS);
X = (2*x/W) - 1;
Y = (-2*y/H) + 1;
glVertex2f (X, Y);
//printf("TRANSFORMED POINT %f\t%f\t# renderPoint\n", X, Y);
glEnd ();
glFlush ();
}
/*wiki pseudo:
function line(x0, x1, y0, y1) //x1
boolean steep := abs(y1 - y0) > abs(x1 - x0)//x2
if steep then//x3
swap(x0, y0) //x4
swap(x1, y1) //x5
if x0 > x1 then //x6
swap(x0, x1) //x7
swap(y0, y1) //x8
int deltax := x1 - x0 //x9
int deltay := abs(y1 - y0) //x10
int error := deltax / 2 //x11
int ystep //x12
int y := y0 //x13
if y0 < y1 then ystep := 1 else ystep := -1 //x14
for x from x0 to x1 //x15
if steep then plot(y,x) else plot(x,y) //x16
error := error - deltay //x17
if error < 0 then //x18
y := y + ystep //x19
error := error + deltax //x20
*/
void bresenham1(GLint x0, GLint x1, GLint y0, GLint y1) //function line(x0, x1, y0, y1)
{
//double result1 = fabs((double)y1 - y0); //abs(y1 - y0)
//double result2 = fabs((double)x1 - x0); //abs(x1 - x0)
int result1 = abs(y1-y0);
int result2 = abs(x1-x0);
bool steep = (result1 > result2); //boolean steep := abs(y1 - y0) > abs(x1 - x0)
if (steep){ //if steep then
GLint aux1 = x0; //swap(x0, y0)
x0=y0;
y0 = aux1;
GLint aux2 = x1; // swap (x1,y1)
x1=y1;
y1=aux2;
}
if(x0>x1){ // if (x0>x1)
GLint aux3=x0; //swap(x0,x1)
x0=x1;
x1=aux3;
GLint aux4=y0;//swap(y0,y1)
y0=y1;
y1=aux4;
}
int deltax = x1-x0; // deltax = x1-x0
int deltay = abs(y1-y0); // int deltay := abs(y1 - y0) - revisar
int error = (deltax / 2); //int error := deltax / 2
int ystep; // int ystep
int y = y0; //int y := y0
if (y0<y1){ //if y0 < y1 then ystep := 1 else ystep := -1
ystep=1;
}
else {ystep=-1;}
for (int x=x0; x<=x1; x++){ //for x from x0 to x1
if (steep){ // if steep then plot(y,x) else plot(x,y)
renderPoint(y,x);
}
else {
renderPoint(x,y);
}
error = error - deltay; //error := error - deltay
if (error<0) { //if error < 0 then
y = y + ystep; // y := y + ystep
error = error + deltax; //error := error + deltax
} // end if (error<0)
}// end for from x0 to x1
}// end bresenham
void Plot4EllipsePoints(int X, int Y,int CX,int CY){
renderPoint(CX+X, CY+Y); //point in quadrant 1
renderPoint(CX-X, CY+Y); // point in quadrant 2
renderPoint(CX-X, CY-Y); // point in quadrant 3
renderPoint(CX+X, CY-Y); // point in quadrant 4
}
void PlotEllipse (int CX, int CY, int XRadius, int YRadius) {
int X, Y;
int XChange, YChange;
int EllipseError;
int TwoASquare, TwoBSquare;
int StoppingX, StoppingY;
TwoASquare = 2 * XRadius * XRadius;
TwoBSquare = 2 * YRadius * YRadius;
X = XRadius;
Y =0;
XChange = YRadius*YRadius*(1-(2*XRadius));
YChange = XRadius * XRadius;
EllipseError =0;
StoppingX = TwoBSquare*XRadius;
StoppingY = 0;
while(StoppingX >= StoppingY){
Plot4EllipsePoints(X,Y,CX,CY);
Y++;
StoppingY=StoppingY + TwoASquare;
EllipseError= EllipseError+ YChange;
YChange= YChange+ TwoASquare;
if( ((2*EllipseError) + XChange)>0)
{
X--;
StoppingX = StoppingX - TwoBSquare;
EllipseError= EllipseError + XChange;
XChange = XChange + TwoBSquare;
}
}
//1st set of points done, start second set
X=0;
Y= YRadius;
XChange= YRadius*YRadius;
YChange = XRadius*XRadius*(1-2*YRadius);
EllipseError=0;
StoppingX =0;
StoppingY= TwoASquare * YRadius;
while(StoppingX <= StoppingY){ // 2nd set of points, y'<-1
Plot4EllipsePoints(X,Y, CX,CY);
X++;
StoppingX = StoppingX + TwoBSquare;
EllipseError = EllipseError + XChange;
XChange = XChange + TwoBSquare;
if (((2*EllipseError) + YChange)>0){
Y--;
StoppingY = StoppingY - TwoASquare;
EllipseError = EllipseError + YChange;
YChange = YChange + TwoASquare;
}
}
}
void renderAll (void)
{
/*glutSetWindow(mainWindow);
glutPostRedisplay();
glutSetWindow(subWindow);
glutPostRedisplay();*/
}
void movimiento(int boton, int estado, int x, int y)
{
if((estado == GLUT_DOWN) && (boton == GLUT_LEFT_BUTTON))//mouse down
{
X1 = x; Y1 = y;
PlotEllipse (x, y, 200, 100);
renderPoint(x,y);
}
if((estado == GLUT_UP) && (boton == GLUT_LEFT_BUTTON))//mouse up
{
//printf(" Up|x:%d, y:%d\n",x,y);
X2 = x; Y2 = y;
//renderLine();
bresenham1(X1,X2,Y1,Y2);
//PRUEBA USANDO LA PRIMITIVA DE OPENGL
glBegin( GL_LINES );
glEnd();
//renderPoint(x, y);
}
}
void MouseMove(int x, int y)
{
//printf("x:%d | y:%d\n", x,y);
X2 = x; Y2 = y;
//renderLine();
//bresenham1(X1, Y1, X2, Y2);
}
void teclado(unsigned char key, int x, int y)
{
if(key==1){
modoDeDibujo=1; // dibuja lineas
printf("Modo de dibujo: linea");
}
if (key==2){
modoDeDibujo=2; //dibuja elipses
}
if(key == 27)exit(0);
}
void especiales(int key, int x, int y)
{
if(key == GLUT_KEY_F1) exit(0);
}
static void
key(unsigned char k, int x, int y)
{
switch (k) {
case 27: /* Escape */
exit(0);
break;
default:
return;
}
glutPostRedisplay();
}
int main (int argc, char *argv [])
{
i = 0;
//inicializa las operaciones de OpenGL/GLUT, db cr antes de usar funciones GLUT
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition (100, 100);
glutInitWindowSize (W, H);
//Crea una ventana de Opengl
glutCreateWindow ("tarea");
glutDisplayFunc (renderPoint);
glutMouseFunc(movimiento);
glutKeyboardFunc(teclado);//teclas ASCII
glutSpecialFunc(especiales);//captura las teclas [f1..f12]
//glutPassiveMotionFunc(pasivo);
glutKeyboardFunc(key);
glutMotionFunc(MouseMove);
//crearMenu();
glutMainLoop ();
}
First of all , you need to arrange the code. You must have one and only one display function, that clears the buffer,calls the other draw-functions and flushes them to the screen ( or swaps the buffer if you are using a double buffer).
On resizing the window,GLUT will call the Display Function which is renderPoint() as you know :
glutDisplayFunc (renderPoint);
renderPoint clears the buffer before redrawing the "points" , for example :
void renderPoint(void) /*REVISAR ESTO*/
{
glClear (GL_COLOR_BUFFER_BIT);
glBegin (GL_POINTS);
glVertex2f (-0.98, 0.98);
glEnd ();
glFlush ();
}
Since the buffer has been cleared , all the points that are drawn outside the function renderPoint ( the points of circles, the points of lines .. ) has no meaning , because you did not call them from the "main display function which is renderPoint.
How to keep the points on the screen ?
You must store the points ( any point you want to draw ) in a buffer such as an array , a dynamic array, std::vector or anything else. in the display function , write a loop statement to visit each point and extract the x and y .. then draw them.
for example , instead of the above function , replace it with :
class MyPoint {
public:
float x;
float y;
MyPoint(float x, float y)
{
this->x = x;
this->y = y;
} };
#include <vector>
std::vector<MyPoint> testPoints;
void renderPoint(void) /*REVISAR ESTO*/ {
testPoints.push_back(MyPoint(-0.58,0.58));
testPoints.push_back(MyPoint(0.58,0.58));
testPoints.push_back(MyPoint(0.58,-0.58));
glClear (GL_COLOR_BUFFER_BIT);
glPointSize(2);
glBegin (GL_POINTS);
for(int i=0;i<testPoints.size();i++)
{
glVertex2f (testPoints[i].x, testPoints[i].y);
}
glEnd ();
glFlush ();
}
as you can see , by using a dynamic array such as (std::vector) to store the points and by using a for loop statement , we able to keep the three points visible.
what else ?
do the same method with other shapes , so that for each "mouse click " event , you may add or push_back two points represents a line end points in an array or std::vector called lineArray . in the display function , make a for loop statement to draw each line after extracting the two line points.
you should use glutReshapeFunc and glViewport to make sure that the viewport has the same dimenstions as the window after the resizing event. and I think that gluOrtho2d is more elegant than trying to mapping from Windows coordinates space to OpenGL coordinates space
arrange your code , so that you just use one display function.
Your program could be Something like this:
void drawTestPoints()
{
for(int i=0;i<length;i++)
{
renderPoint(pointsArray[i].x,pointsArray[i].y);// vector of points/(MyPoint)
}
}
void drawLines()
{
for(int i=0;i<length;)
{
MyPoint startPoint = linesArray[i];
MyPoint endPoint = linesArray[i+1];
bresenham1(startPoint.x,endPoint.x,startPoint.y,endPoint.y);
i+=2;
}
}
void drawAll()
{
glClear (GL_COLOR_BUFFER_BIT);
drawTestPoints();
drawLines();
drawOtherShapes();
glFlush();
}
.
.
.
// in the main func:
glutDisplayFunc (drawAll);
==
Don't think of it as "keeping them drawn". Think instead in terms of events and "when do I need to redraw my objects".
For this you will need to handle the resize event and rerun your drawing code. I'm also guessing that your objects might not redraw themselves after another application is moved over them, or if it is moved off screen a little and then brought back. If this is the case you'll need to figure out what events to handle that will cause a redraw as well.
Here is a quick example for windows :
http://www.falloutsoftware.com/tutorials/gl/gl2.htm

How do I solve the following error in Linux Graphics C program?

I am trying to compile the following code,but it gives me a error message as listed below. I am a beginner in linux c graphics and cannot figure it out. Can anyone suggest a solution?
code:
#include<stdio.h>
#include<graphics.h>
void main()
{
int gd = DETECT, gm;
int dx, dy, p, end;
float x1, x2, y1, y2, x, y;
initgraph(&gd, &gm,NULL);
printf("Enter Value of X1: ");
scanf("%f", &x1);
printf("Enter Value of Y1: ");
scanf("%f", &y1);
printf("Enter Value of X2: ");
scanf("%f", &x2);
printf("Enter Value of Y2: ");
scanf("%f", &y2);
dx = abs(x1 - x2);
dy = abs(y1 - y2);
p = 2 * dy - dx;
if(x1 > x2)
{
x = x2;
y = y2;
end = x1;
}
else
{
x = x1;
y = y1;
end = x2;
}
putpixel(x, y, 10);
while(x < end)
{
x = x + 1;
if(p < 0)
{
p = p + 2 * dy;
}
else
{
y = y + 1;
p = p + 2 * (dy - dx);
}
putpixel(x, y, 10);
}
getch();
closegraph();
}
error message:
meshramsd#ubuntu:~/libgraph-1.0.2$ ./b
[xcb] Unknown sequence number while processing queue
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
b: ../../src/xcb_io.c:274: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed.
[xcb] Unknown sequence number while processing queue
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
b: ../../src/xcb_io.c:274: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed.
Aborted (core dumped)
I found this error, when I was doing graphics code in c on ubuntu 18.04. I searched a lot but there was no satisfactory answer for this Issue. finally I read this Error so many time and found some this first line of error.
"[xcb] Unknown sequence number while processing queue" .
Code With Error
#include <stdio.h>
#include <graphics.h>
int main() {
int gd = DETECT,gm;
int x1,y1,x2,y2;
float step,dx,dy;
initgraph(&gd,&gm,NULL); // I found Error here Initialized Graph before standard input
printf("enter the value of x1 and y1 : ");
scanf("%d %d",&x1,&y1);
printf("Enter the value of x2 and y2 : ");
scanf("%d %d",&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
if (dx >= dy)
step=dx;
else
step=dy;
dx=dx/step;
dy=dy/step;
int x=x1;
int y=y1;
int i=1;
while(i <= step)
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i++;
delay(100);
}
getchar();
closegraph();
return 0;
}
Error Free Code
#include <stdio.h>
#include <graphics.h>
int main() {
int gd = DETECT,gm;
int x1,y1,x2,y2;
float step,dx,dy;
printf("enter the value of x1 and y1 : ");
scanf("%d %d",&x1,&y1);
printf("Enter the value of x2 and y2 : ");
scanf("%d %d",&x2,&y2);
initgraph(&gd,&gm,NULL); //after correction I initialized graph after standard input
dx=abs(x2-x1);
dy=abs(y2-y1);
if (dx >= dy)
step=dx;
else
step=dy;
dx=dx/step;
dy=dy/step;
int x=x1;
int y=y1;
int i=1;
while(i <= step)
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i++;
delay(100);
}
getchar();
closegraph();
return 0;
}
After successful execution i got my desired output
You can try using libsvga which seems to run fine on Linux Mint - I ran Mint under Virtualbox on a Mac with no problems.
I installed the following packages:
sudo apt-get install svgalib-bin libsvga1 libsvga1-dev
And then I hacked your code into the following:
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <vga.h>
#include<stdio.h>
void main()
{
int dx, dy, p, end;
/* detect the chipset and give up supervisor rights */
if (vga_init() < 0)
return EXIT_FAILURE;
vga_setmode(G1024x768x256); /* some low resolution dont work */
vga_setcolor(14); /* color of pixel */
float x1, x2, y1, y2, x, y;
x1=10;
y1=40;
x2=800;
y2=500;
dx = abs(x1 - x2);
dy = abs(y1 - y2);
p = 2 * dy - dx;
if(x1 > x2)
{
x = x2;
y = y2;
end = x1;
} else {
x = x1;
y = y1;
end = x2;
}
vga_drawpixel(x, y);
while(x < end){
x = x + 1;
if(p < 0)
{
p = p + 2 * dy;
} else {
y = y + 1;
p = p + 2 * (dy - dx);
}
vga_drawpixel(x, y);
}
sleep(10);
/* restore textmode and fall back to ordinary text console handling */
vga_setmode(TEXT);
}
I compiled like this:
gcc graphics.c -lvga -lm -o graphics
and ran with:
sudo ./graphics
I got this output - you can change the numbers easily enough if you want a different colour or size.

Resources