/** * Saves current snake state on an image to file * * @param output_file_path path to desired output file */ void Snake::print_and_save(std::string const &output_file_path) { Image img_copy = img.clone(); filled_circle(img_copy, xs[0], ys[0], 2); for (unsigned k = 1; k < xs.size(); ++k) { line(img_copy, xs[k - 1], ys[k - 1], xs[k], ys[k]); filled_circle(img_copy, xs[k], ys[k], 2); } cv::imwrite(output_file_path, img_copy); }
void draw() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); static float angle = 0; int i; angle += 0.2; glPushMatrix(); for (i=1; i<7; ++i) { glRotatef(angle*(i+3)/15.0, 0, 0, 1); glColor3f((i+2)/9.0, (i+2)/9.0, (i+2)/9.0); filled_circle(i/3.05, i/2.9, 80); gear(i/2.9, i/2.8, 18, 80); } glPopMatrix(); glPushMatrix(); glScalef(2, 2, 2); glBegin(GL_TRIANGLE_STRIP); glColor3f(0.2, 0.2, 0.8); glVertex2f(1, 0); glVertex2f(0, 1); glColor3f(0.05, 0.05, 0.2); glVertex2f(0.05, -0.05); glVertex2f(-0.05, 0.05); glColor3f(0.2, 0.2, 0.8); glVertex2f(0, -1); glVertex2f(-1, 0); glEnd(); glPopMatrix(); }
int main(void) { int count = 0; int G_pressed = 0, R_pressed = 1; char vstring[10], wstring[10]; double x1_old, y1_old; double x2_old, y2_old; double x1_new, y1_new; double x2_new, y2_new; double vx, vy, wx, wy; double v, w; posvel pv; x1_old = 100.0; y1_old = 100.0; vx = 10.0; vy = 5.0; x2_old = 250.0; y2_old = 250.0; wx = -8.0; wy = 4.0; v = sqrt(pow(wx,2)+pow(wy,2)); w = sqrt(pow(vx,2)+pow(vy,2)); /* open the graphics window */ initwindow(XWINDOW, YWINDOW); /* allow mouse operations */ initmouse(); /* allow keyboard operations */ initkeyboard(); /* create an event queue */ create_event_queue(); /* register display, mouse and keyboard as event sources */ reg_display_events(); reg_mouse_events(); reg_keyboard_events(); /* initialize the font */ initfont(); outtextxy(4,5,"To quit press left mouse button or close graphics window"); outtextxy(4,15,"To pause press right mouse button"); outtextxy(4,25,"To change ball speed use arrow keys (up/down = red, left/right = blue)"); outtextxy(4,35,"To make the red ball green press G or g "); do { if (check_if_event()) { /* wait for event */ wait_for_event(); if (event_close_display()) break; else if (event_mouse_button_down()) { if (event_mouse_left_button_down()) break; else if (event_mouse_right_button_down()) wait_for_event(); } else if (event_key_down()) { /* change speed of first ball */ if(event_key_up_arrow()) { vx = 1.25*vx; vy = 1.25*vy; } else if(event_key_down_arrow()) { vx = 0.75*vx; vy = 0.75*vy; } else if(event_key_left_arrow()) { wx = 0.75*wx; wy = 0.75*wy; } else if(event_key_right_arrow()) { wx = 1.25*wx; wy = 1.25*wy; } if (event_key('G')) { G_pressed = 1; R_pressed = 0; } else if (event_key('R')) { R_pressed = 1; G_pressed = 0; } } } v = sqrt(pow(wx,2)+pow(wy,2)); w = sqrt(pow(vx,2)+pow(vy,2)); /* calculate new ball positions */ x1_new = x1_old + vx*TICK; y1_new = y1_old + vy*TICK; x2_new = x2_old + wx*TICK; y2_new = y2_old + wy*TICK; /* handle what to do if balls hit boundaries */ pv = hit_boundary(x1_new, vx, 1); x1_new = pv.pos; vx = pv.vel; pv = hit_boundary(x2_new, wx, 1); x2_new = pv.pos; wx = pv.vel; pv = hit_boundary(y1_new, vy, 0); y1_new = pv.pos; vy = pv.vel; pv = hit_boundary(y2_new, wy, 0); y2_new = pv.pos; wy = pv.vel; /* draw balls on screen buffer in new positions */ filled_circle(x1_new, y1_new, RADIUS , BLUE); if (G_pressed) filled_circle(x2_new, y2_new, RADIUS , GREEN); else if (R_pressed) filled_circle(x2_new, y2_new, RADIUS , RED); /* make the balls visible on the screen display and remove the balls in the previous positions */ sprintf(vstring, "%4.2lf", v); sprintf(wstring, "%4.2lf", w); setcolor(RED); outtextxy(VEL_TEXT_X,VEL_TEXT_Y,"v = "); /* clear area for numeric output */ filled_rectangle(VEL_TEXT_X+40, VEL_TEXT_Y-10, VEL_TEXT_X+100, VEL_TEXT_Y+20, BLACK); outtextxy(VEL_TEXT_X+60,VEL_TEXT_Y,vstring); setcolor(BLUE); outtextxy(VEL_TEXT_X+120,VEL_TEXT_Y,"w = "); /* clear area for numeric output */ filled_rectangle(VEL_TEXT_X+160, VEL_TEXT_Y-10, VEL_TEXT_X+220, VEL_TEXT_Y+20, BLACK); outtextxy(VEL_TEXT_X+180,VEL_TEXT_Y,wstring); update_display(); /* remove the balls in the previous positions on the screen buffer */ filled_circle(x1_old, y1_old, RADIUS , BLACK); filled_circle(x2_old, y2_old, RADIUS , BLACK); /* update the old positions */ x1_old = x1_new; y1_old = y1_new; x2_old = x2_new; y2_old = y2_new; count++; pausefor(8); /* wait 8 miliseconds */ } while (count < MAXCOUNT); /* close the mouse */ closemouse(); /* remove the display */ closegraph(); return 0; }