 Allegro.cc Forums » Programming Questions » Splines for Math(s) deficient Coders.

 This thread is locked; no one can reply to it.  Splines for Math(s) deficient Coders.
 Thomas Fjellstrom Member #476 June 2000 I've been looking up the algorithms and stuff for calculating and drawing splines, and while I can understand some of the explanations, all the images of way too small and incomplete (math) functions, and formulas/equations.Is there someone here who can explain it for a coder? -- Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]"If you can't think of a better solution, don't try to make a better solution." -- weapon_S"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730
 miran Member #2,407 June 2002 Yes. You have a set of points. Your goal is to draw a curve that starts in the first point and ends in the last. You can do that by drawing short straight segments, short enough so that it's not obvious they are straight. So you take a parameter that goes from 0 to 1 with small increments in a for loop. You feed this parameter into some sort of formula to get the next point. You draw a straight line from the previous point to the one you just calculated and move on. When the parameter is 0, the point you get is p0 (the starting points) and when it is 1, the point is your last point. So by the time your for loop ends, you will have drawn a smooth curve between p0 and p3 (if you have 4 points). There are a few different formulas to draw splines for different purposes (you want the line to go through all points or not, the kind of connectivity you want, how changing one point affects the curve, etc.) Bezier is a really simple kind of spline (B-Spline for 4 points). I don't know the equations by heart, but google does... --sig used to be here
 Thomas Fjellstrom Member #476 June 2000 Thats the issue, I've read a few different explanations that had the equations in them, but I can't read any of them. English and code is fine, pictures of equations I can't do. -- Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]"If you can't think of a better solution, don't try to make a better solution." -- weapon_S"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730
 miran Member #2,407 June 2002 In that case I think you should forget about all that stuff...Here's pseudocode anyway: ```Point points; Point p0 = points; for (double p=0.0; p<=1.0; p+=0.01) { Point p1 = some_formula(points, p); draw_line(p0, p1); p0 = p1; } ``` --sig used to be here
Thomas Fjellstrom
Member #476
June 2000 Yeah, That I can understand, its the some_formula() that I need to understand.

I've already written a nice "arc" drawing routine:

 1 void draw_arc(BITMAP *bmp, point p1, point p2, double step) 2 { 3 point mp; 4 5 step = M_PI / step; 6 7 mp.x = ( p1.x + p2.x ) / 2.0; 8 mp.y = ( p1.y + p2.y ) / 2.0; 9 10 double w = fabs(p2.x - p1.x); 11 double h = fabs(p2.y - p1.y); 12 double ll = sqrt(w*w + h*h); 13 double r = ll / 2.0; 14 15 double start = atan2( p1.y - mp.y , p1.x - mp.x ); 16 double end = start+rad(180); //atan2( p2.y - mp.y , p2.x - mp.x ); 17 //start + M_PI; 18 19 double rw = ll; 20 double rh = ll; 21 22 // for testing only, real algo is below 23 arc(bmp, mp.x, mp.y, ftofix(deg(-start) * 256.0 / 360.0), ftofix(deg(-end) * 256.0 / 360.0), r, makecol(55,55,55)); 24 25 start -= rad(180); 26 double pheta = start; 27 end -= rad(180); 28 29 printf("\rstart: %f, end: %f", deg(start), deg(end)); 30 31 double lx = rw/2 * cos( pheta ) + mp.x; 32 double ly = rh/2 * sin( pheta ) + mp.y; 33 double fx = lx; 34 double fy = ly; 35 36 for(; pheta - ( end ) < 0.001; pheta += step) { 37 double x = rw/2 * cos( pheta ) + mp.x; 38 double y = rh/2 * sin( pheta ) + mp.y; 39 40 // if(lx && ly) { 41 line(bmp, lx, ly, x, y, makecol(255, 255, 255)); 42 putpixel(bmp, lx, ly, makecol(255,0,0)); 43 putpixel(bmp, x, y, makecol(255,0,0)); 44 // } 45 46 lx = x; 47 ly = y; 48 } 49 50 if(lx != p1.x && ly != p1.y) { 51 line(bmp, lx, ly, p1.x, p1.y, makecol(100, 255, 100)); 52 } 53 54 if(fx != p2.x && fy != p2.y) { 55 line(bmp, fx, fy, p2.x, p2.y, makecol(100, 255, 100)); 56 } 57 58 59 // rect(bmp, p1.x, p1.y, p2.x, p2.y, makecol(100,100,100)); 60 rect(bmp, mp.x-r, mp.y-r, mp.x+r, mp.y+r, makecol(150,150,150)); 61 line(bmp, p1.x, p1.y, p2.x, p2.y, makecol(0,0,255)); 62 putpixel(bmp, mp.x, mp.y, makecol(0,255,0)); 63 64 }

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

 CGamesPlay Member #2,559 July 2002 Hmm, I wouldn't bother with that function... It would work, I wager, but involves extra maths to compute the remaining splines.This is a very nice spline. I recommend you use it. To start, just go through and interpolate between two points, then you can move into more.That formula takes input as points (split into an x formula and a y formula). Take two arbitrary points, and for m1 use (1, 0) and for m2 use (-1, 0). -- Tomasu: Every time you read this: hugging!Ryan Patterson -
 Thomas Fjellstrom Member #476 June 2000 I was looking at a bunch of links, and one that really looked neat was the NURB spline. course it had all kinds of corner cases that it had to take care of using extra nodes and such.[quote]Hmm, I wouldn't bother with that function...[/quote]If you mean my arc function, its for arcs and it only does 180° arcs, due to the start + 180 but that was intentional. It wwasn'tintended for use as a spline helper, just something to say that I'm not a complete idiot, just I can't read most of the "theoretical" equations, [b]especially[/b] when the images used are about 2x too small to show any of the smaller var names legibly.But thank you, I should be able to do something with that one. -- Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]"If you can't think of a better solution, don't try to make a better solution." -- weapon_S"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730
 miran Member #2,407 June 2002 NURBs are the most generalized kind of splines. All other kinds of splines are basically NURBs with some parts fixed or simplified or ommited or something... --sig used to be here
 CGamesPlay Member #2,559 July 2002 Good. This is a bump so you can psot back here once you get that sorted, and we can go into using more than 4 control points. -- Tomasu: Every time you read this: hugging!Ryan Patterson -
 miran Member #2,407 June 2002 Here's a small test program I wrote when I was studying B-splines. The code is not well organized but the important part is nice and easy to read. The functions you're interested in are make_spline() and N() plus some global variables. #SelectExpand 1#include 2 3typedef struct Vertex { 4 int x; 5 int y; 6} Vertex; 7 8const int n = 12; // number of control points (4 for a Bezier) 9int k = 2; // degree of the spline polynomial 10int g = 16; // density of the interpolation 11Vertex R[n]; // the control grid 12Vertex *pbspline; // a periodic B-spline curve 13Vertex *bspline; // a B-spline curve 14int n_points_periodic; // number of vertices in pbspline 15int n_points; // number of vertices in bspline 16int *U; // the knot vector 17int u; // the number of knots 18 19int selectedPoint = -1; 20bool mouseDown = false; 21int grip_x, grip_y; 22 23bool handle_mouse() { 24 int mx = mouse_x; 25 int my = mouse_y; 26 int mb = mouse_b; 27 28 bool ret = false; 29 int oldSel = selectedPoint; 30 31 if (!mb) { 32 mouseDown = false; 33 selectedPoint = -1; 34 for (int i=0; i.x; 36 int dy = my - R.y; 37 int d2 = dx*dx + dy*dy; 38 if (d2 < 4*4) { 39 selectedPoint = i; 40 break; 41 } 42 } 43 } 44 else { 45 if (!mouseDown) { 46 mouseDown = true; 47 grip_x = mx; 48 grip_y = my; 49 } 50 51 if (selectedPoint != -1) { 52 int dx = mx - grip_x; 53 int dy = my - grip_y; 54 if (dx || dy) { 55 R[selectedPoint].x += dx; 56 R[selectedPoint].y += dy; 57 grip_x += dx; 58 grip_y += dy; 59 ret = true; 60 } 61 } 62 } 63 64 ret |= selectedPoint != oldSel; 65 return ret; 66} 67 68// make a predefined control line 69void initialize() { 70 R.x = 50; R.y = 100; 71 R.x = 100; R.y = 450; 72 R.x = 300; R.y = 550; 73 R.x = 200; R.y = 350; 74 R.x = 200; R.y = 150; 75 R.x = 300; R.y = 350; 76 R.x = 450; R.y = 500; 77 R.x = 400; R.y = 250; 78 R.x = 550; R.y = 50; 79 R.x = 700; R.y = 550; 80 R.x = 750; R.y = 300; 81 R.x = 700; R.y = 150; 82 83 bspline = NULL; 84 pbspline = NULL; 85 U = NULL; 86} 87 88 89void make_periodic_spline() { 90 int n_sections = n - 2; 91 int i, j, k; 92 float u; 93 float delta; 94 95 n_points_periodic = n_sections*g + 1; 96 if (pbspline) delete [] pbspline; 97 pbspline = new Vertex[n_points_periodic]; 98 99 delta = 1.0f/g; 100 k = 0; 101 for (i=0; i.x + (-2.0f*u*u + 2.0f*u + 1.0f)*R[i+1].x + u*u*R[i+2].x)); 105 pbspline[k].y = (int)(0.5f * ((1.0f-u)*(1.0f-u)*R.y + (-2.0f*u*u + 2.0f*u + 1.0f)*R[i+1].y + u*u*R[i+2].y)); 106 u += delta; 107 k++; 108 } 109 } 110 i--; 111 pbspline[k].x = (int)(0.5f * ((1.0f-u)*(1.0f-u)*R.x + (-2.0f*u*u + 2.0f*u + 1.0f)*R[i+1].x + u*u*R[i+2].x)); 112 pbspline[k].y = (int)(0.5f * ((1.0f-u)*(1.0f-u)*R.y + (-2.0f*u*u + 2.0f*u + 1.0f)*R[i+1].y + u*u*R[i+2].y)); 113} 114 115 116float N(int i, int k, float u) { 117 if (k==0) { 118 if (u >= U && u < U[i+1]) return 1.0f; 119 else return 0.0f; 120 } 121 122 float f1; 123 float f2; 124 125 if (U[i+k] - U == 0) 126 f1 = 0.0f; 127 else 128 f1 = ((u - U)*N(i,k-1,u))/(U[i+k] - U); 129 130 if (U[i+k+1] - U[i+1] == 0) 131 f2 = 0.0f; 132 else 133 f2 = ((U[i+k+1] - u)*N(i+1,k-1,u))/(U[i+k+1] - U[i+1]); 134 135 return f1 + f2; 136} 137 138 139void make_spline() { 140 int i, j, l; 141 int n_sections = n - k; 142 float delta; 143 float uf; 144 float tx, ty; 145 146 if (U) delete U; 147 u = k + n + 1; 148 U = new int; 149 150 for (i=0; i = 0; 151 for (i=k+1; i = i - k; 152 for (i=n; i = n - k; 153 154 n_points = n_sections*g + 1; 155 if (bspline) delete [] bspline; 156 bspline = new Vertex[n_points]; 157 158 delta = 1.0f/g; 159 uf = 0.0; 160 int count=0; 161 for (i=0; i.x, R.y, 3, makecol(240, 128, 128)); 206 circle(bmp, R.x, R.y, 3, makecol(200, 64, 64)); 207 } 208 else { 209 circle(bmp, R.x, R.y, 2, makecol(200, 64, 64)); 210 } 211 } 212 213 for (i=0; i.x, R.y, R[i+1].x, R[i+1].y, makecol(160, 120, 120)); 215 } 216} 217 218 219void draw_periodic_spline(BITMAP *bmp) { 220 int i; 221 for (i=0; i.x, pbspline.y, 1, makecol(200, 200, 64)); 222 for (i=0; i.x, pbspline.y, pbspline[i+1].x, pbspline[i+1].y, makecol(180, 180, 120)); 223} 224 225 226void draw_spline(BITMAP *bmp) { 227 int i; 228 for (i=0; i.x, bspline.y, 1, makecol(64, 200, 64)); 229 for (i=0; i.x, bspline.y, bspline[i+1].x, bspline[i+1].y, makecol(120, 180, 120)); 230 for (i=0; i); 231} 232 233 234void destroy() { 235 if (bspline) delete [] bspline; 236 if (pbspline) delete [] pbspline; 237 if (U) delete [] U; 238} 239 240 241int main() { 242 allegro_init(); 243 set_color_depth(32); 244 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0); 245 install_keyboard(); 246 install_mouse(); 247 enable_hardware_cursor(); 248 show_mouse(screen); 249 250 251 initialize(); 252 253 bool redraw = true; 254 bool done = false; 255 BITMAP *bmp = create_bitmap(SCREEN_W, SCREEN_H); 256 257 while (!done) { 258 while (keypressed()) { 259 switch (readkey()>>8) { 260 case KEY_LEFT: 261 g>>=1; 262 if (g<4) g = 4; 263 else redraw = true; 264 break; 265 case KEY_RIGHT: 266 g<<=1; 267 if (g>64) g = 64; 268 else redraw = true; 269 break; 270 case KEY_UP: 271 k++; 272 if (k>n-1) k=n-1; 273 else redraw = true; 274 break; 275 case KEY_DOWN: 276 k--; 277 if (k<0) k=0; 278 else redraw = true; 279 break; 280 case KEY_ESC: 281 done = true; 282 }; 283 } 284 285 redraw |= handle_mouse(); 286 287 if (redraw) { 288 make_periodic_spline(); 289 make_spline(); 290 draw_grid(bmp); 291 draw_control_grid(bmp); 292 //draw_periodic_spline(bmp); 293 draw_spline(bmp); 294 blit(bmp, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); 295 } 296 297 redraw = false; 298 } 299 300 destroy(); 301 destroy_bitmap(bmp); 302 return 0; 303} 304END_OF_MAIN(); --sig used to be here
 Go to: Allegro Development Installation, Setup & Configuration Allegro.cc Comments Off-Topic Ordeals The Depot Game Design & Concepts Programming Questions Recent Threads