void draw_filledCircleSlice( unsigned int x, unsigned int y, double rad, uint8_t r, uint8_t g, uint8_t b, uint16_t slice_begin, uint16_t slice_end) { uint8_t i,j; if(slice_begin > slice_end) { swap_(slice_begin,slice_end); } for(i=0;i<(rad*2);i++) { for(j=0;j<(rad*2);j++) { double dist = pythagoras( j,i ); if(dist <= rad-1) { setLedXY(y-j,x+i,r,g,b); }else if(dist < rad) { // dla_plot(y-j,x+i,r,g,b,1-(dist-rad+1)); } } } }
void draw_line_antialias_(GBitmap* img, int16_t x1, int16_t y1, int16_t x2, int16_t y2, GColor8 color) { uint8_t* img_pixels = gbitmap_get_data(img); int16_t w = gbitmap_get_bounds(img).size.w; int16_t h = gbitmap_get_bounds(img).size.h; fixed dx = int_to_fixed(abs_(x1 - x2)); fixed dy = int_to_fixed(abs_(y1 - y2)); bool steep = dy > dx; if(steep){ swap_(x1, y1); swap_(x2, y2); } if(x1 > x2){ swap_(x1, x2); swap_(y1, y2); } dx = x2 - x1; dy = y2 - y1; fixed intery; int x; for(x=x1; x <= x2; x++) { intery = int_to_fixed(y1) + (int_to_fixed(x - x1) * dy / dx); if(x>=0){ if(steep){ _plot(img_pixels, w, h, ipart_(intery) , x, color, rfpart_(intery)); _plot(img_pixels, w, h, ipart_(intery) + 1, x, color, fpart_(intery)); } else { _plot(img_pixels, w, h, x, ipart_(intery) , color, rfpart_(intery)); _plot(img_pixels, w, h, x, ipart_(intery) + 1, color, fpart_(intery)); } } } }
static void _heap_sort(heap_t *heap) { int i=1, j=2; /* gnome sort */ int *a = heap->h; while(i < heap->n) { /* smaller values are kept at the end */ if ( heap->f[a[i-1]] >= heap->f[a[i]] ) { i = j; j++; } else { swap_(i-1, i); i--; i = (i==0) ? j++ : i; } } }
static inline void forth_vm_execute_instruction(forth_context_type *fc, char cmd) { // printf("%c\n",cmd); // getchar(); switch(cmd) { case '0': push(fc,0); break; case '1': push(fc,1); break; case '2': push(fc,2); break; case '3': push(fc,3); break; case '4': push(fc,4); break; case '5': push(fc,5); break; case '6': push(fc,6); break; case '7': push(fc,7); break; case '8': push(fc,8); break; case '9': push(fc,9); break; case '@': at(fc); break; //@ case '!': to(fc); break; //! case 'd': fc->SP+=fc->cell; break; //drop case 'D': dup(fc); break; //dup case 's': swap_(fc); break; //swap case 'l': push(fc,next_cell(fc)); break; //lit case '+': add(fc); break; //+ case '-': sub(fc); break; //- case '*': mul(fc); break; //* case '/': div_(fc); break; // / case '%': mod(fc); break; //mod case '&': and(fc); break; // and case '|': or(fc); break; // or case '^': xor(fc); break; // xor case '>': more(fc); break; // > case '<': less(fc); break; // < case '=': eq(fc); break; // = case 'b': branch(fc); break; // branch case '?': cbranch(fc); break; // ?branch case 'c': call(fc); break; // call case 'r': ret(fc); break; // ret case 't': to_r(fc); break; // >R case 'f': from_r(fc); break; // R> case 'i': in(fc); break; // in case 'o': out(fc); break; // out case '_': fc->stop=1; break; // stop case 'A': adr0(fc); break; // @0 case 1: push(fc,fc->SP); break; // SP@ case 2: fc->SP=pop(fc); break; // SP! case 3: push(fc,fc->RP); break; // RP@ case 4: fc->RP=pop(fc); break; // RP! case 5: shl(fc); break; // << case 6: shr(fc); break; // >> case 7: push(fc,*(size_t *)(fc->mem+fc->RP)); break; // i case 8: cat(fc); break; // c@ case 9: cto(fc); break; // c! case 10: set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(1); break; // nop case 11: in_ready(fc); break; // ?in case 12: out_ready(fc); break; // ?out case 16: umod(fc); break; // umod case 17: udiv(fc); break; // u/ // kernel case 'K': kalsym_lookup(fc); break; // lookup kallsym address case 18: kcall(fc); break; // kcall } }
void ag_surface32__draw_line_aa(struct ag_surface32* img, struct ag_vec2i start, struct ag_vec2i end, struct ag_color32 color) { int x1 = start.x, x2 = end.x, y1 = start.y, y2 = end.y; double dx = (double)x2 - (double)x1; double dy = (double)y2 - (double)y1; if ( fabs(dx) > fabs(dy) ) { if ( x2 < x1 ) { swap_(x1, x2); swap_(y1, y2); } double gradient = dy / dx; double xend = round_(x1); double yend = y1 + gradient*(xend - x1); double xgap = rfpart_(x1 + 0.5); int xpxl1 = xend; int ypxl1 = ipart_(yend); plot_(xpxl1, ypxl1, rfpart_(yend)*xgap); plot_(xpxl1, ypxl1+1, fpart_(yend)*xgap); double intery = yend + gradient; xend = round_(x2); yend = y2 + gradient*(xend - x2); xgap = fpart_(x2+0.5); int xpxl2 = xend; int ypxl2 = ipart_(yend); plot_(xpxl2, ypxl2, rfpart_(yend) * xgap); plot_(xpxl2, ypxl2 + 1, fpart_(yend) * xgap); int x; for(x=xpxl1+1; x <= (xpxl2-1); x++) { plot_(x, ipart_(intery), rfpart_(intery)); plot_(x, ipart_(intery) + 1, fpart_(intery)); intery += gradient; } } else { if ( y2 < y1 ) { swap_(x1, x2); swap_(y1, y2); } double gradient = dx / dy; double yend = round_(y1); double xend = x1 + gradient*(yend - y1); double ygap = rfpart_(y1 + 0.5); int ypxl1 = yend; int xpxl1 = ipart_(xend); plot_(xpxl1, ypxl1, rfpart_(xend)*ygap); plot_(xpxl1, ypxl1+1, fpart_(xend)*ygap); double interx = xend + gradient; yend = round_(y2); xend = x2 + gradient*(yend - y2); ygap = fpart_(y2+0.5); int ypxl2 = yend; int xpxl2 = ipart_(xend); plot_(xpxl2, ypxl2, rfpart_(xend) * ygap); plot_(xpxl2, ypxl2 + 1, fpart_(xend) * ygap); int y; for(y=ypxl1+1; y <= (ypxl2-1); y++) { plot_(ipart_(interx), y, rfpart_(interx)); plot_(ipart_(interx) + 1, y, fpart_(interx)); interx += gradient; } } }
int dm_all_perm (matrix_t *r, matrix_t *mayw, matrix_t *mustw) { // http://www.freewebz.com/permute/soda_submit.html int len = dm_ncols (r); int perm[len]; int best_perm[len]; int min, last_min; min = cost_ (r, mayw); int i, j; for (i = 0; i < len; i++) { perm[i] = best_perm[i] = i; } while (1) { last_min = cost_ (r, mayw); if (last_min < min) { memcpy (best_perm, perm, len * sizeof (int)); min = last_min; } // debug DMDBG (current_all_perm_ (perm, len)); DMDBG (printf ("costs: %d\n", last_min)); int key = len - 1; int newkey = key; // The key value is the first value from the end which // is smaller than the value to its immediate right while (key > 0 && (perm[key] <= perm[key - 1])) key--; key--; // If key < 0 the data is in reverse sorted order, // which is the last permutation. if (key < 0) break; // perm[key+1] is greater than perm[key] because of how key // was found. If no other is greater, perm[key+1] is used while ((newkey > key) && (perm[newkey] <= perm[key])) newkey--; swap_ (perm, key, newkey); dm_swap_cols (r, key, newkey); dm_swap_cols (mayw, key, newkey); dm_swap_cols (mustw, key, newkey); i = len - 1; key++; // The tail must end in sorted order to produce the // next permutation. while (i > key) { swap_ (perm, i, key); dm_swap_cols (r, i, key); dm_swap_cols (mayw, i, key); dm_swap_cols (mustw, i, key); key++; i--; } } // permutation: DMDBG (printf ("best: %d = ", min)); DMDBG (current_all_perm_ (best_perm, len)); DMDBG (printf ("current:")); DMDBG (current_all_perm_ (perm, len)); // now iterate over best, find in current and swap for (i = 0; i < len - 1; i++) { for (j = i; j < len; j++) { if (best_perm[i] == perm[j]) { DMDBG (printf ("swap %d, %d\n", i, j)); swap_ (perm, i, j); dm_swap_cols (r, i, j); dm_swap_cols (mayw, i, j); dm_swap_cols (mustw, i, j); break; } } } DMDBG (printf ("current:")); DMDBG (current_all_perm_ (perm, len)); DMDBG (printf ("cost: %d ", cost_ (r, mayw))); return 0; }
void draw_line( unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, uint8_t r, uint8_t g, uint8_t b ) { double dx = (double)x2 - (double)x1; double dy = (double)y2 - (double)y1; if ( fabs(dx) > fabs(dy) ) { if ( x2 < x1 ) { swap_(x1, x2); swap_(y1, y2); } double gradient = dy / dx; double xend = round_(x1); double yend = y1 + gradient*(xend - x1); double xgap = rfpart_(x1 + 0.5); int xpxl1 = xend; int ypxl1 = ipart_(yend); dla_plot(xpxl1, ypxl1, r,g,b,rfpart_(yend)*xgap); dla_plot(xpxl1, ypxl1+1, r,g,b,fpart_(yend)*xgap); double intery = yend + gradient; xend = round_(x2); yend = y2 + gradient*(xend - x2); xgap = fpart_(x2+0.5); int xpxl2 = xend; int ypxl2 = ipart_(yend); dla_plot(xpxl2, ypxl2, r,g,b,rfpart_(yend) * xgap); dla_plot(xpxl2, ypxl2 + 1, r,g,b,fpart_(yend) * xgap); int x; for(x=xpxl1+1; x <= (xpxl2-1); x++) { dla_plot(x, ipart_(intery), r,g,b,rfpart_(intery)); dla_plot(x, ipart_(intery) + 1, r,g,b,fpart_(intery)); intery += gradient; } } else { if ( y2 < y1 ) { swap_(x1, x2); swap_(y1, y2); } double gradient = dx / dy; double yend = round_(y1); double xend = x1 + gradient*(yend - y1); double ygap = rfpart_(y1 + 0.5); int ypxl1 = yend; int xpxl1 = ipart_(xend); dla_plot(xpxl1, ypxl1, r,g,b,rfpart_(xend)*ygap); dla_plot(xpxl1, ypxl1+1, r,g,b,fpart_(xend)*ygap); double interx = xend + gradient; yend = round_(y2); xend = x2 + gradient*(yend - y2); ygap = fpart_(y2+0.5); int ypxl2 = yend; int xpxl2 = ipart_(xend); dla_plot(xpxl2, ypxl2, r,g,b,rfpart_(xend) * ygap); dla_plot(xpxl2, ypxl2 + 1, r,g,b,fpart_(xend) * ygap); int y; for(y=ypxl1+1; y <= (ypxl2-1); y++) { dla_plot(ipart_(interx), y, r,g,b,rfpart_(interx)); dla_plot(ipart_(interx) + 1, y, r,g,b,fpart_(interx)); interx += gradient; } } }