CWSpaceVector spline::interpolatePoint(float u_val, CWSpaceVector p[4]) { //set up that matrix CWTMatrix<float> M(4, 4); M[0][0] = 0.0; M[0][1] = 1.0; M[0][2] = 0.0; M[0][3] = 0.0; M[1][0] = -SPLINE_SMOOTHNESS; M[1][1] = 0.0; M[1][2] =SPLINE_SMOOTHNESS; M[1][3] = 0.0; M[2][0] = 2*SPLINE_SMOOTHNESS; M[2][1] = SPLINE_SMOOTHNESS-3; M[2][2] = 3-2*SPLINE_SMOOTHNESS; M[2][3] = -SPLINE_SMOOTHNESS; M[3][0] = -SPLINE_SMOOTHNESS; M[3][1] = 2-SPLINE_SMOOTHNESS; M[3][2] = SPLINE_SMOOTHNESS-2; M[3][3] = SPLINE_SMOOTHNESS; //set up u vector CWTMatrix<float> u(1, 4); u[0][0] = 1.0; u[0][1] = u_val; u[0][2] = u_val * u_val; u[0][3] = u_val * u_val * u_val; //set up points matrix CWTMatrix<float> P(4, 4); for(int r=0; r<4; r++) { P[r][0] = p[r][0]; P[r][1] = p[r][1]; P[r][2] = p[r][2]; P[r][3] = 0.0; } CWTMatrix<float> uM(1, 4); CWTMatrix<float> r(1, 4); //do the math uM = u * M; r = uM * P; //return the found point CWSpaceVector ret(r[0][0], r[0][1], r[0][2]); return ret; }
int main(){ int i, N; char c, file[100]; // check that gnuplot is present on the system int gnupl = control(); if(gnupl == 1){ printf("\nYou need gnuplot to graph the results."); printf("\nInstall it with: sudo apt-get install gnuplot\n\n"); exit(2); } printf("Enter the name or path of file: "); fgets(file,sizeof(file),stdin); file[strlen(file)-1] = '\0'; // the file's lines number is the number of points to be saved N = linesFile(file); if(N <= 2){ printf("\nError: insufficient data number.\n"); exit(2); } // creating data's arrays double *x = calloc(N,sizeof(double)); double *y = calloc(N,sizeof(double)); double *errors = calloc(N,sizeof(double)); if(x == NULL || y == NULL || errors == NULL){ perror("\nerror"); printf("\n"); exit(1); } // reading from file FILE *inputFile = fopen(file,"r"); if(inputFile == NULL){ perror("\nError"); exit(1); } for(i=0; i<N; i++){ fscanf(inputFile,"%lf %lf %lf\n",&x[i],&y[i],&errors[i]); } fclose(inputFile); // determine linear coefficients double M = Mbest(N,x,y,errors); double Q = Qbest(N,x,y,errors,M); double sigmaM = fabs(uM(N,x,y,errors,M,Q)); double sigmaQ = fabs(uQ(N,x,errors,sigmaM)); // defining best sigma(Y) and correlation coefficient double sigmaY = fabs(bestSigma(N,x,y,M,Q)); // <-- residuals analysis double cov = covariance(N,x,y); double cor = correlation(N,x,y); double lCov = linearParamCovariance(N,mean(x,N),sigma(x,N,mean(x,N)),sigmaY,M,Q); double lCor = linearParamCorrelation(N,x); // Chi-square test int freedomDegrees = N - 2; // infer 2 parameters (): M and Q double chi2 = 0, rChi2; for(i=0; i<N; i++){ chi2 += pow(y[i] - ((M * x[i]) + Q),2) / pow(errors[i],2); } rChi2 = chi2 / freedomDegrees; printf("\nThe best linear fit Y = mX + q is:"); printf("\nm = %.3lf\tsigma(m) = %.3lf\nq = %.3lf\tsigma(q) = %.3lf",M,sigmaM,Q,sigmaQ); printf("\n\nBest sigma(Y) = %.3lf",sigmaY); printf("\nCov(X,Y) = %.3lf",cov); printf("\nCor(X,Y) = %.3lf",cor); printf("\nCov(m,c) = %.3lf",lCov); printf("\nCor(m,c) = %.3lf",lCor); //printf("\nChi square = %.3lf",chi2); printf("\nReduced Chi square = %.3lf",rChi2); // interpolation and extrapolation int choice; double pointX, pointY, sigmaPointY, alpha; printf("\n\nDo you want to extrapolate a point with the calculated linear regression? (1 = YES | 0 = NO): "); scanf("%d",&choice); if(choice == 1){ extrapolation(N,x,errors,sigmaY,M,Q); } // creating fit printf("\nPlotting fit...\n"); FILE *data = fopen("data.dat","w"); if(data == NULL){ perror("\nError"); exit(1); } // writing experimental datas for(i=0; i<N; i++){ fprintf(data,"%lf %lf %lf\n",x[i],y[i],errors[i]); } fclose(data); // creating fit points //fit(M,Q,x,N); // gnuplot feature free(x); free(y); return 0; }
int main() { sf::RenderWindow window(sf::VideoMode(1920, 1080), "Corgi Movement Simulator"); /*sf::CircleShape shape(100.f); shape.setFillColor(sf::Color::Green);*/ /*sf::Texture texture; texture.loadFromFile("Sprites/Scene10x10.png"); texture.setSmooth(true); sf::Sprite* sprite = (new sf::Sprite(texture));*/ //(*sprite).setColor(sf::Color(128, 128, 128)); //testlvlManager sf::Texture shibe; if (!shibe.loadFromFile("Sprites/Units/shibe_spread.png")) { std::cout << "Failed to load player spritesheet!" << std::endl; return 1; } Animation shibe_idle; shibe_idle.setSpriteSheet(shibe); shibe_idle.addFrame(sf::IntRect(0, 0, 128, 128)); shibe_idle.addFrame(sf::IntRect(128, 0, 128, 128)); shibe_idle.addFrame(sf::IntRect(256, 0, 128, 128)); shibe_idle.addFrame(sf::IntRect(384, 0, 128, 128)); AnimatedSprite animatedShibe(sf::seconds(0.2), false, true); animatedShibe.setPosition(0,0); animatedShibe.play(shibe_idle); TextureManager texMng; texMng.getFiles(); texMng.createTextures(); lvlManager lvl; Block b = lvl.generateTiles(lvl.buildKey(&texMng), &texMng); lvl.genMap(b, 0, 15); for (int i = 0; i < 50; i++){ Block b = lvl.generateTiles(lvl.buildKey(&texMng), &texMng); sf::Vector2<int> v = lvl.findNextSpot(b.x, b.y); if (v.x != -1) lvl.genMap(b, v.x, v.y); } SoundManager sM; sM.getFiles(); sM.createSounds(); Player p(&texMng, &(sM.soundTable.at("bark")), sf::Sprite()); //Unit test_unit(10, 10, *sprite); //Tile test(sprite, &test_unit); sf::Sprite output; lvl.genDrawable(&output); UIManager uimanager; uimanager.setTerrain(output); //uimanager.setTerrain(lvl.genDrawable()); int mousex=0, mousey=0; //lvl.testTileGen(&texMng, &window); //testTextureManager(&window); UnitManager uM(&lvl, &texMng, &p, &sM); uM.spawnUnit(doge, p.playerUnit->x + 1, p.playerUnit->y); uM.spawnUnit(turtle, p.playerUnit->x, p.playerUnit->y + 1); std::cout << p.playerUnit->x + 1 << p.playerUnit->y + 1 << std::endl; uM.gatherActions(); sf::Clock frameClock; while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) window.close(); if (event.type == sf::Event::MouseButtonPressed) { mousex = sf::Mouse::getPosition(window).x; mousey = sf::Mouse::getPosition(window).y; } if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && event.type == sf::Event::MouseMoved) { uimanager.x += event.mouseMove.x-mousex; uimanager.y += event.mouseMove.y-mousey; mousex = event.mouseMove.x; mousey = event.mouseMove.y; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::P)) { uM.gatherActions(); } if (event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Button::Right) { std::cout << "clicked at " << sf::Mouse::getPosition(window).x-uimanager.x << " " << sf::Mouse::getPosition(window).y-uimanager.y << std::endl; //std::cout << "is accessible: " << lvl.isAccessible(sf::Mouse::getPosition(window).x - uimanager.x, sf::Mouse::getPosition(window).y - uimanager.y) << std::endl; int xPos = sf::Mouse::getPosition(window).x - uimanager.x; int yPos = sf::Mouse::getPosition(window).y - uimanager.y; //std::cout << lvl.map[xPos / 128][yPos / 128].pawn << std::endl; if (lvl.isAccessible(xPos, yPos)) { uM.interact(xPos/128, yPos/128, &p); std::cout << xPos/128 << yPos/128 << std::endl; std::cout << "great success !" << std::endl; } else std::cout << "no" << std::endl; } /* Zooming - Experimental if (event.type == sf::Event::MouseWheelScrolled) { uimanager.scale = signbit(event.mouseWheelScroll.delta)?uimanager.scale*0.95:uimanager.scale*1.05; }*/ } //UI Movement if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) uimanager.y += 1; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) uimanager.y -= 1; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) uimanager.x += 1; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) uimanager.x -= 1; if (sf::Mouse::getPosition(window).x < 20) uimanager.x += 1; if (sf::Mouse::getPosition(window).x > 1900) uimanager.x -= 1; if (sf::Mouse::getPosition(window).y < 20) uimanager.y += 1; if (sf::Mouse::getPosition(window).y > 1060) uimanager.y -= 1; // lvl.genDrawable(&output); // UIManager uimanager; // uimanager.setTerrain(output); sf::Time frameTime = frameClock.restart(); animatedShibe.update(frameTime); window.clear(); //sf::Sprite output2; //lvl.drawUnits(&output); //uimanager.setUnits(output2); //window.draw(*(uimanager.getUI())); //window.draw(*(uimanager.getUnits())); uimanager.setUnits(uM.getUnits()); uimanager.drawEverything(&window); window.draw(animatedShibe); //window.draw(*sprite); //window.draw(output); /*sf::Texture texture; texture.loadFromFile("Sprites\\PlayerChar.png"); texture.setSmooth(true); sf::Sprite* sprite = (new sf::Sprite(texture)); (*sprite).setColor(sf::Color(128, 128, 128)); sprite.setTexture(texture);*/ //window.draw(*sprite); //std::cout << "Current player AP: " << p.playerUnit->currAP << ". Max player AP: " << p.playerUnit->maxAP << "." << std::endl; window.display(); } return 0; }