//******************************************************************************* //* e(x,y) = b^2*x^2 + a^2*y^2 - a^2*b^2 //* a = xRadius //* b = yRadius //******************************************************************************* void dispFillEllipse(int xCenter, int yCenter, int xRadius, int yRadius) { int xx = 0; int yy = yRadius; unsigned int width = 1; long a2 = (long)xRadius * xRadius; long b2 = (long)yRadius * yRadius; long crit1 = -(a2 / 4 + xRadius % 2 + b2); long crit2 = -(b2 / 4 + yRadius % 2 + a2); long crit3 = -(b2/4 + yRadius % 2); long t = -a2 * yy; /* e(x+1/2,y-1/2) - (a^2+b^2)/4 */ long dxt = 2 * b2 * xx; long dyt = -2 * a2 * yy; long d2xt = 2 * b2; long d2yt = 2 * a2; while (yy >= 0 && xx <= xRadius) { if (t + b2 * xx <= crit1 || /* e(x+1,y-1/2) <= 0 */ t + a2 * yy <= crit3) { /* e(x+1/2,y) <= 0 */ incx(); width += 2; } else if (t - a2 * yy > crit2) { /* e(x+1/2,y-1) > 0 */ //dispRectangle(xCenter - x, yCenter - y, width, 1); dispRectangle(xCenter - xx, yCenter - yy, width, 1); if (yy != 0) { //dispRectangle(xCenter- xx, yCenter + y, width, 1); dispRectangle(xCenter - xx, yCenter + yy, width, 1); } incy(); } else { //dispRectangle(xCenter - xx, yCenter - y, width, 1); dispRectangle(xCenter - xx, yCenter - yy, width, 1); if (yy != 0) { //dispRectangle(xCenter - xx, yCenter + y, width, 1); dispRectangle(xCenter - xx, yCenter + yy, width, 1); } incx(); incy(); width += 2; } } if (yRadius == 0) { //dispRectangle(xCenter - xRadius, yCenter, 2 * xRadius + 1, 1); dispRectangle(xCenter - xRadius, yCenter, 2 * xRadius + 1, 1); } }
void GeneticAlgorithm<EllipseData>::paint(uint individual,uint gene,Farbe ** renderbuffer) { #define incx() x++, dxt += d2xt, t += dxt #define incy() y--, dyt += d2yt, t += dyt int xc = m_individuals[individual].gene[gene].midx,yc = m_individuals[individual].gene[gene].midy; //cout <<"Ellipsenposition: "<<xc<<" "<<yc<<endl; int a = m_individuals[individual].gene[gene].radiusx,b = m_individuals[individual].gene[gene].radiusy; Farbe col = (Farbe){m_individuals[individual].gene[gene].colr,m_individuals[individual].gene[gene].colr,m_individuals[individual].gene[gene].colr}; if(!blackwhite) { col.b = m_individuals[individual].gene[gene].colb; col.g = m_individuals[individual].gene[gene].colg; } int x = 0, y = b; unsigned int width = 1; long a2 = (long)a*a, b2 = (long)b*b; long crit1 = -(a2/4 + a%2 + b2); long crit2 = -(b2/4 + b%2 + a2); long crit3 = -(b2/4 + b%2); long t = -a2*y; /* e(x+1/2,y-1/2) - (a^2+b^2)/4 */ long dxt = 2*b2*x, dyt = -2*a2*y; long d2xt = 2*b2, d2yt = 2*a2; while (y>=0 && x<=a) { if (t + b2*x <= crit1 || /* e(x+1,y-1/2) <= 0 */ t + a2*y <= crit3) { /* e(x+1/2,y) <= 0 */ incx(); width += 2; } else if (t - a2*y > crit2) { /* e(x+1/2,y-1) > 0 */ row(xc-x, yc-y, width,col,renderbuffer); if (y!=0) row(xc-x, yc+y, width,col,renderbuffer); incy(); } else { row(xc-x, yc-y, width,col,renderbuffer); if (y!=0) row(xc-x, yc+y, width,col,renderbuffer); incx(); incy(); width += 2; } } if (b == 0) row(xc-a, yc, 2*a+1,col,renderbuffer); }
//******************************************************************************* void dispOutlineEllipse( int xCenter, int yCenter, int xRadius, int yRadius) { int xx = 0; int yy = yRadius; long a2 = (long)xRadius * xRadius; long b2 = (long)yRadius * yRadius; long crit1 = -(a2 / 4 + xRadius % 2 + b2); long crit2 = -(b2 / 4 + yRadius % 2 + a2); long crit3 = -(b2 / 4 + yRadius % 2); long t = -a2 * yy; /* e(x+1/2,y-1/2) - (a^2+b^2)/4 */ long dxt = 2 * b2 * xx; long dyt = -2 * a2 * yy; long d2xt = 2 * b2; long d2yt = 2 * a2; while (yy >= 0 && xx <= xRadius) { dispPixel(xCenter + xx, yCenter + yy); if (xx != 0 || yy != 0) { dispPixel(xCenter - xx, yCenter - yy); } if (xx != 0 && yy != 0) { dispPixel(xCenter + xx, yCenter - yy); dispPixel(xCenter - xx, yCenter + yy); } if (t + b2 * xx <= crit1 || /* e(x+1,y-1/2) <= 0 */ t + a2 * yy <= crit3) /* e(x+1/2,y) <= 0 */ { incx(); } else if (t - a2 * yy > crit2) /* e(x+1/2,y-1) > 0 */ { incy(); } else { incx(); incy(); } } }
void ShapeEllipse::draw() { if (this->isSolid) { int x = 0, y = this->yrad; unsigned int width = 1; long a2 = (long)this->xrad * this->xrad, b2 = (long)this->yrad * this->yrad; long crit1 = -(a2 / 4 + this->xrad % 2 + b2); long crit2 = -(b2 / 4 + this->yrad % 2 + a2); long crit3 = -(b2 / 4 + this->yrad % 2); long t = -a2 * y; long dxt = 2 * b2 * x, dyt = -2 * a2 * y; long d2xt = 2 * b2, d2yt = 2 * a2; while (y >= 0 && x <= this->xrad) { if (t + b2*x <= crit1 || t + a2*y <= crit3) { incx(); width += 2; } else if (t - a2*y > crit2) { drawBox(this->x - x, this->y - y, width, 1, color, ctype); if (y != 0) drawBox(this->x - x, this->y + y, width, 1, color, ctype); incy(); } else { drawBox(this->x - x, this->y - y, width, 1, color, ctype); if (y != 0) drawBox(this->x - x, this->y + y, width, 1, color, ctype); incx(); incy(); width += 2; } } if (this->yrad == 0) drawBox(this->x - this->xrad, this->y, 2*this->xrad + 1, 1, color, ctype); } else { int x = this->xrad; int y = 0; int twoAsquare = 2 * this->xrad * this->xrad; int twoBsquare = 2 * this->yrad * this->yrad; int xchange = this->yrad * this->yrad * (1 - 2 * this->xrad); int ychange = this->xrad * this->xrad; int ellipseerror = 0; int stoppingX = twoBsquare * this->xrad; int stoppingY = 0; while (stoppingX >= stoppingY) { drawDot(this->x + x, this->y + y, color, ctype); drawDot(this->x - x, this->y + y, color, ctype); drawDot(this->x - x, this->y - y, color, ctype); drawDot(this->x + x, this->y - y, color, ctype); y++; stoppingY += twoAsquare; ellipseerror += ychange; ychange += twoAsquare; if ((2* ellipseerror + xchange) > 0) { x--; stoppingX -= twoBsquare; ellipseerror += xchange; xchange += twoBsquare; } } x = 0; y = this->yrad; xchange = this->yrad * this->yrad; ychange = this->xrad * this->xrad * (1 - 2 * this->yrad); ellipseerror = 0; stoppingX = 0; stoppingY = twoAsquare * this->yrad; while (stoppingX <= stoppingY) { drawDot(this->x + x, this->y + y, color, ctype); drawDot(this->x - x, this->y + y, color, ctype); drawDot(this->x - x, this->y - y, color, ctype); drawDot(this->x + x, this->y - y, color, ctype); x++; stoppingX += twoBsquare; ellipseerror += xchange; xchange += twoBsquare; if ((2* ellipseerror + ychange) > 0) { y--; stoppingY -= twoAsquare; ellipseerror += ychange; ychange += twoAsquare; } } } }
void drawEllipse(int posx, int posy, int xrad, int yrad, int color, bool isSolid) { if (isSolid) { int x = 0, y = yrad; unsigned int width = 1; long a2 = (long)xrad * (int)xrad, b2 = (long)yrad * yrad; long crit1 = -(a2 / 4 + xrad % 2 + b2); long crit2 = -(b2 / 4 + yrad % 2 + a2); long crit3 = -(b2 / 4 + yrad % 2); long t = -a2 * y; long dxt = 2 * b2 * x, dyt = -2 * a2 * y; long d2xt = 2 * b2, d2yt = 2 * a2; while (y >= 0 && x <= xrad) { if (t + b2*x <= crit1 || t + a2*y <= crit3) { incx(); width += 2; } else if (t - a2*y > crit2) { BW::drawScanLine(posx - x, posy - y, width, color); if (y != 0) BW::drawScanLine(posx - x, posy + y, width, color); incy(); } else { BW::drawScanLine(posx - x, posy - y, width, color); if (y != 0) BW::drawScanLine(posx - x, posy + y, width, color); incx(); incy(); width += 2; } } if (yrad == 0) BW::drawScanLine(posx - xrad, posy, 2*xrad + 1, color); } else { int x = xrad; int y = 0; int twoAsquare = 2 * xrad * xrad; int twoBsquare = 2 * yrad * yrad; int xchange = yrad * yrad * (1 - 2 * xrad); int ychange = xrad * xrad; int ellipseerror = 0; int stoppingX = twoBsquare * xrad; int stoppingY = 0; while (stoppingX >= stoppingY) { BW::drawDot(posx + x, posy + y, color); BW::drawDot(posx - x, posy + y, color); BW::drawDot(posx - x, posy - y, color); BW::drawDot(posx + x, posy - y, color); y++; stoppingY += twoAsquare; ellipseerror += ychange; ychange += twoAsquare; if ((2* ellipseerror + xchange) > 0) { x--; stoppingX -= twoBsquare; ellipseerror += xchange; xchange += twoBsquare; } } x = 0; y = yrad; xchange = yrad * yrad; ychange = xrad * xrad * (1 - 2 * yrad); ellipseerror = 0; stoppingX = 0; stoppingY = twoAsquare * yrad; while (stoppingX <= stoppingY) { BW::drawDot(posx + x, posy + y, color); BW::drawDot(posx - x, posy + y, color); BW::drawDot(posx - x, posy - y, color); BW::drawDot(posx + x, posy - y, color); x++; stoppingX += twoBsquare; ellipseerror += xchange; xchange += twoBsquare; if ((2* ellipseerror + ychange) > 0) { y--; stoppingY -= twoAsquare; ellipseerror += ychange; ychange += twoAsquare; } } } }
int main() { ofstream myfile; ifstream input; int filecnt = 0; std::default_random_engine gen; std::bernoulli_distribution incx(0.3); std::bernoulli_distribution incy(0.5); std::bernoulli_distribution decx(0.7); std::bernoulli_distribution decy(0.7); bool prod = false; // GRID Block2* grid = new Block2({0, 0, 0}, {1.0, 1.0, 0}, 5, 5); // Block2* grid2 = new Block2({0, 0, 0}, {1.0, 1.0, 0}, 3, 3); // grid->adaptCriteria(); auto u = grid->getVar("u"); auto v = grid->getVar("v"); for (auto pass = 0; pass < 4; ++pass) { std::string flname="op"+std::to_string(pass)+".dat"; if (prod) { myfile.open(flname); for (auto c : grid->listCell) { c->adapt[0] = 0; c->adapt[1] = 0; if (incx(gen) && c->level[0] != grid->levelHighBound[0]) { c->adapt[0]=1;} if (incy(gen) && c->level[1] != grid->levelHighBound[1]) { c->adapt[1]=1;} if (c->adapt[0] == 0 && c->adapt[1] == 0) { if (decx(gen) && c->level[0] != grid->levelLowBound[0]) { c->adapt[0]=-1;} if (decy(gen) && c->level[1] != grid->levelLowBound[1]) { c->adapt[1]=-1;} } myfile << c->adapt[0] << " " << c->adapt[1] << endl; } myfile.close(); } else { input.open(flname); if (input.is_open()) { for (auto c : grid->listCell) { input >> c->adapt[0]; input >> c->adapt[1]; // c->adapt[0] = max(short(0), c->adapt[0]); // c->adapt[1] = max(short(0), c->adapt[1]); } input.close(); } else { cout << "cannot read data"<< endl; exit(1); } } cout << filecnt << " pass: "******" random " << endl; flname="grid"+std::to_string(filecnt++)+".vtk"; myfile.open(flname); myfile << grid << endl; myfile.close(); for (auto i = 0; i < grid->listCell.size(); ++i) { grid->listCell[i]->checkNgbrLevel(0); grid->listCell[i]->checkNgbrLevel(1); grid->listCell[i]->checkNgbrLevel(2); } cout << filecnt << " pass: "******" corrected " << endl; flname="grid"+std::to_string(filecnt++)+".vtk"; myfile.open(flname); myfile << grid << endl; myfile.close(); grid->refine2(); // auto lmin = **(std::min_element(&(grid->levelMin), &(grid->levelMin)+3)); // auto lmax = **(std::max_element(&(grid->levelMax), &(grid->levelMax)+3)); // for (auto j = 0; j < 2; ++j) { // for (auto l = grid->levelMin[j]; l < grid->levelMax[j]+1; ++l) { // auto nCell = grid->listCell.size(); // for (auto i = 0; i < nCell; ++i) { // auto c = grid->listCell[i]; // if (c->level[j] != l) continue; // if (c->adapt[j] == 0) continue; // c->refine(j); // // if (j == 0) // // c->convertToSimpleBlock({2,1}); // // else // // c->convertToSimpleBlock({1,2}); // // c->adapt[j]--; c->level[j]++; // // for (auto k=0; k< 2; ++k) { // // grid->listCell[k]->adapt.assign(c->adapt.begin(), c->adapt.end()); // // grid->listCell[k]->level.assign(c->level.begin(), c->level.end()); // // } // } // grid->setCurrentLevels(); // cout << "("<<grid->levelMin[0] << "-"<< grid->levelMax[0] <<")-"; // cout << "("<<grid->levelMin[1] << "-"<< grid->levelMax[1] <<")"; // cout << filecnt << " pass: "******" l: " << l << " dir: "<< j << endl; // for (auto i = 0; i<grid->listCell.size(); ++i) { // u->set(i, grid->listCell[i]->adapt[0]); // v->set(i, grid->listCell[i]->adapt[1]); // } // flname="grid"+std::to_string(filecnt++)+".vtk"; // myfile.open(flname); // myfile << grid << endl; // myfile.close(); // } // } flname="grid"+std::to_string(filecnt++)+".vtk"; myfile.open(flname); myfile << grid << endl; myfile.close(); }