/**************************************************************************** Build new library of graphics functions graph.h /****************************************************************************/ void putpixel(unsigned int, unsigned int, unsigned int); double scale; #define PI 3.141592653589793238462643383279502884197169399375105 int minx = 0, maxx = 1600, miny = 0, maxy = 800; // Get radian from vectors void getrad(double vxt, double vyt, double & rad){ double vx = vxt / sqrt(vxt * vxt + vyt * vyt); rad = acos(vx / vxt * vyt); if(vx > 0){ rad = 2 * PI - rad; } rad += PI / 2; } // Draw a line using vector distances void linevxy(double x, double y, double vx, double vy, int hue){ double vt = sqrt(vx * vx + vy * vy), xx = 0; vx /= vt; vy /= vt; while(xx++ < vt){ putpixel(x, y, hue); x -= vx; y -= vy; } } // Draw a line from x, y to x2, y2 void linexy(double x, double y, double x2, double y2, int hue){ double dx = x - x2, dy = y - y2, xx = 0; double vt = sqrt(dx * dx + dy * dy); double vx = dx / vt, vy = dy / vt; while(xx++ < vt){ putpixel(x2, y2, hue); x2 += vx; y2 += vy; } } // Draw a line from x, y in the direction of rad dist in length void linerad(double x, double y, double rad, double dist, int hue){ double vx = cos(rad); double vy = sin(rad), xx = 0; while(xx++ < dist){ putpixel(x, y, hue); x -= vx; y -= vy; } } struct ball { double x; double y; double r; double vx; double vy; double m; int hue; int id; int bumper; int control; double dtoc; void init(void){ x = (maxx - maxx / 4); y = (maxy / 2); r = rand() % 25 + 5; vx = 0; vy = -4.50436; // vy = -4.50436317398015217; control = 0; hue = rand() % 0xffffff; m = r * .55; bumper = -1; dtoc = x; } void showvec(void){ double rad; double amp = r + r + ((vx * vx + vy * vy * m) * .000001); getrad(vx, vy, rad); rad += PI; linerad(x, y, rad, amp, hue); } void ring(double radius){ double inc = 1.0 / radius * .5, xx = 0; while(xx < 2 * PI){ putpixel(x + cos(xx) * radius, y + sin(xx) * radius, hue); xx += inc; } } void show(void){ double xx = 0; while(xx++ < r){ ring(xx); } } void move(void){ // Uncomment to bounce off walls double loss = .50; /***********************************************/ if(x + r > maxx){ vx *= -loss; x = maxx - r; } if(x - r < minx){ vx *= -loss; x = minx + r; } if(y + r > maxy){ vy *= -loss; y = maxy - r; } if(y - r < miny){ vy *= -loss; y = miny + r; } /*********************************************** if(x > maxx){ vx = 0; vy = -2.5; x = maxx; } if(x < minx){ vx = 0; vy = 2.5; x = minx; } if(y > maxy){ vy = 0; vx = 2.5; y = maxy; } if(y < miny){ vy = 0; vx = -2.5; y = miny; } /*********************************************** if(x > maxx){ x = minx; } if(x < minx){ x = maxx; } if(y > maxy){ y = miny; } if(y < miny){ y = maxy; } /***********************************************/ x += vx; y += vy; } void bump(ball & sp){ double dx = x - sp.x; double dy = y - sp.y; double sr = r + sp.r; if(dx * dx + dy * dy > sr * sr){ if(sp.id == bumper){ bumper = -1; // reset bumper id sp.bumper = -1; } return; // no bump exit } if(sp.id == bumper){ vx *= 1.001; vy *= 1.001; return; // Same bumper exit } if(bumper > 0 || sp.bumper > 0){ return; } double dc = sqrt(dx * dx + dy * dy); double nx = dx / dc; double ny = dy / dc; double a1 = vx * nx + vy * ny; double a2 = sp.vx * nx + sp.vy * ny; double op = (2 * (a1 - a2)) / (m + sp.m); vx = vx - op * sp.m * nx; vy = vy - op * sp.m * ny; sp.vx = sp.vx + op * m * nx; sp.vy = sp.vy + op * m * ny; sp.control ++; control++; if(sp.bumper < 0 && bumper < 0){ sp.bumper = id; bumper = sp.id; } } }; struct halo { double x; // x location double y; // y location double z; // z location as size double vx; // velocity in x direction double vy; // velocity in y direction double trans; // transparancy double mx; // Direction in radians of maxima double rs; // Rotation speed of maxima double amp; // Amplitude of maxima double m; // Mass of halo int inner; // Polarity of inner field int outer; // Polarity of outer field void init(void){ // Self init for testing x = 200; // Using object should init y = 200; z = scale; vx = 2.01; vy = 3.05; trans = .2; mx = 1; rs = .1; // Rate of spin of maxima m = 1; amp = 15.0; inner = 0xff0000; outer = 0x0000ff; } void move(void){ if(x > maxx - z){x = maxx - z; vx *= -1; } if(x < minx + z){x = minx + z; vx *= -1; } if(y > maxy - z){y = maxy - z; vy *= -1; } if(y < miny + z){y = miny + z; vy *= -1; } x += vx; y += vy; } void show(void){ int ofst = 0; double c = 0; double inc = 1 / (z * trans); double minamp, maxamp; double xx, yy; mx += rs; if(mx > 2 * PI){ mx -= 2 * PI; } while(c < 2 * PI){ c += inc; minamp = sin(c + mx) * amp; maxamp = sin(c + mx) * amp; if(minamp > 0) minamp *= -1.0; if(maxamp < 0) maxamp *= -1.0; for(ofst = minamp; ofst < maxamp; ofst ++){ xx = x + cos(c) * (z + ofst); yy = y + sin(c) * (z + ofst); if(ofst < 0){ putpixel(xx, yy, inner); } else{ putpixel(xx, yy, outer); } } } } }; struct proton { halo s2; halo s3; halo s4; double x; double y; double z; double vx; double vy; double m; int bumper; int id; int vec; void sync(void){ s2.vx = vx; s2.x = x; s2.vy = vy; s2.y = y; s3.vx = vx; s3.x = x; s3.vy = vy; s3.y = y; s4.vx = vx; s4.x = x; s4.vy = vy; s4.y = y; } void init(void){ double vis = 1; bumper = -1; if(id == 0){ vec = 0xff0000; } else{ vec = 0xffff00; } vx = (float) (rand() % 15) / 2; vy = (float) (rand() % 15) / 2; z = scale; x = rand() % (maxx - 100) + 50; y = rand() % (maxy - 100) + 50; s2.x = x; // Shell 2 s2.y = y; s2.z = z * 274.89051; s2.vx = vx; s2.vy = vy; s2.trans = vis / 2; s2.mx = 1; s2.rs = .2; s2.amp = 10.0; s2.inner = 0x0000ff; s2.outer = 0xff0000; s2.m = 2.549920405 * 2.549920405; s3.x = x; // Shell 3 s3.y = y; s3.z = z * 42.27743; s3.vx = vx; s3.vy = vx; s3.trans = vis; s3.mx = 1; s3.rs = .4; s3.amp = 5.0; s3.inner = 0xff0000; s3.outer = 0x0000ff; s3.m = s2.m * s2.m; s4.x = maxx / 2; // Shell 4 s4.y = maxy / 2; s4.z = z; s4.vx = vx; s4.vy = vy; s4.trans = vis / 10; s4.mx = 1; s4.rs = .8; s4.amp = 3.0; s4.inner = 0x0000ff; s4.outer = 0xff0000; s4.m = s3.m * s3.m; m = s2.m + s3.m + s4.m; } void showvec(void){ double vt = sqrt(vx * vx + vy * vy), xx = 0; double ix = vx / vt, iy = vy / vt, i = x, j = y; vt += s2.z + vt; while(xx++ < vt){ putpixel(i, j, vec); i += ix; j += iy; } } void move(void){ sync(); s4.move(); s3.move(); s2.move(); vx = s2.vx; vy = s2.vy; x = s2.x; y = s2.y; } void show(void){ if(z < s4.z || z > s4.z){ s4.z = z; s3.z = z * 42.7743; s2.z = z * 274.89051; } s4.show(); s3.show(); s2.show(); showvec(); } void bump(proton & p){ double dx = x - p.x; double dy = y - p.y; double sr = z * 274.89051; sr += p.z * 274.89051; if(dx * dx + dy * dy > sr * sr){ if(bumper == p.id){ bumper = -1; p.bumper = -1; } return; } if(bumper >= 0 || p.bumper >= 0){ return; } double dc = sqrt(dx * dx + dy * dy); double nx = dx / dc; double ny = dy / dc; double a1 = vx * nx + vy * ny; double a2 = p.vx * nx + p.vy * ny; double op = (2 * (a1 - a2)) / (m + p.m); vx = vx - op * p.m * nx; vy = vy - op * p.m * ny; p.vx = p.vx + op * m * nx; p.vy = p.vy + op * m * ny; sync(); p.sync(); if(bumper < 0 ){ bumper = p.id; } if(p.bumper < 0){ p.bumper = id; } } }; struct neutron { proton p; halo n; double x; double y; double vx; double vy; double z; void init(){ p.init(); p.m += 2.549920405; // Proton adds mass of s1 x = p.x; y = p.y; z = scale; // Global scale vx = p.vx; vy = p.vy; n.x = x; n.y = y; n.z = z * 700.94892; // Size n.vx = vx; n.vy = vy; n.trans = .5; n.mx = 1; n.rs = .1; // Rate of spin of maxima n.m = 2.549920405; n.amp = 15.0; // Amplitude of maxima n.inner = 0xff0000; n.outer = 0x0000ff; } void sync(void){ x = n.x; y = n.y; vx = n.vx; vy = n.vy; p.x = x; p.y = y; p.vx = vx; p.vy = vy; p.sync(); } void show(void){ if(z < p.z || z > p.z){ z = p.z; n.z = z * 700.94892; } p.show(); n.show(); } void move(){ p.move(); n.move(); sync(); } void bump(proton & p1){ p.bump(p1); n.vx = p.vx; n.vy = p.vy; } }; struct electron { halo e1; double x; double y; double z; double vx; double vy; void init(void){ z = scale; // Global scale x = 500; y = 500; vx = rand() % 5; // x velocity vy = rand() % 5; // y velocity e1.x = x; e1.y = y; e1.z = z * 3497.78331; e1.vx = vx; e1.vy = vy; e1.trans = .3; // Distance between hash marks e1.mx = 1; e1.rs = .05; // Rate of spin of maxima e1.m = 1; e1.amp = 30.0; e1.inner = 0xff0000; e1.outer = 0x0000ff; } void show(void){ e1.z = z * 3497.78331; e1.show(); } void move(){ e1.move(); x = e1.x; y = e1.y; vx = e1.vx; vy = e1.vy; } }; void gravity(ball & sp, ball & sp0){ // g of 1920 with y = -2 double x = sp0.x, y = sp0.y, line = 0; double g = 10000.00; double dx = sp.x - sp0.x; int huec; double dy = sp.y - sp0.y; double dc = sqrt(dx * dx + dy * dy); double nx = dx / dc; double ny = dy / dc; huec = 0x000000; if(sp.dtoc < dc - .5){ huec = 0xff0000; } if(sp.dtoc > dc + .5){ huec = 0x00ff00; } sp.dtoc = dc; sp.vx = sp.vx - g * nx / (dc * dc); sp.vy = sp.vy - g * ny / (dc * dc); if(sp.control > 0){ while(line++ < dc){ putpixel(x, y, huec); x += nx; y += ny; } } } time_t seconds; SDL_Surface *screen; int init_screen(int srt, int sdn) { screen = SDL_SetVideoMode(srt, sdn, 32, SDL_SWSURFACE); if( screen == NULL ){ fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError()); exit(1); } } void putpixel(unsigned int x, unsigned int y, unsigned int color) { if(x >= minx && x < maxx && y >= miny && y < maxy){ unsigned int *ptr = (unsigned int*)screen->pixels; int lineoffset = y * (screen->pitch / 4); ptr[lineoffset + x] = color; } } int ColorFill(int color, int maxx, int maxy) { int x, y; for(y = 0; y < maxy; y++){ for(x=0; x < maxx; x++){ putpixel(x, y, color); } } } void Circle(int x, int y, double radius, int hue, double trans){ double inc = 1.0 / (radius * trans); // Amount of transparency double circle = 0; // smaller decimal is more while(circle < 2 * PI){ // transparency putpixel(x + cos(circle) * radius, y + sin(circle) * radius, hue); circle += inc; } } char keystroke(void) { int count = 0; char keydat = 0; SDL_Event event; while( SDL_PollEvent( &event ) ){ switch( event.type ){ case SDL_KEYDOWN: keydat = event.key.keysym.sym; count ++; break; case SDL_KEYUP: count ++; break; case SDL_QUIT: keydat = 'q'; break; case SDLK_KP_ENTER: keydat = 0x0a; break; case SDLK_RETURN: keydat = 0x0a; break; } } return keydat; }