void Particle::draw() { // console() << "draw"; gl::color(1.0f, 0.0f, 0.0f, 1.0f); Vec2f rv = m_vel.normalized(); rv.rotate(M_PI_2); gl::drawSolidTriangle(m_loc + m_vel.normalized() * m_radius * 2.0f, m_loc + rv * m_radius/2.0f, m_loc - rv * m_radius/2.0f); gl::color(1.0, 1.0, 1.0, 1.0f); //CHKGLERR(0) gl::lineWidth(voidnoise::Settings::get().stemThickness); gl::draw(m_vboMesh); // gl::begin(GL_LINE_STRIP); // gl::vertex(m_loc); // int i = 0; // for (Vec2f &vec : m_history) // { // gl::color(1.0, 1.0, 1.0, (10.0-(i+m_frameCtr/20.0f))/10.0f); // gl::vertex(vec); // i++; // } // gl::end(); //CHKGLERR(1) // if (m_history.size() > 0) // gl::drawSolidCircle(m_history.back(), m_radius); // gl::color(1.0f, 1.0f, 1.0f, 1.0f); }
// Rotate branch around vertex v1 void RefinementState::rotateBranch (const Filter &branch, const RefinementState &state, int v_idx, float angle) { int i; float co, si; const Vec2f &v = state.layout[v_idx]; Vec2f d; angle = DEG2RAD(angle); co = cos(angle); si = sin(angle); layout.clear_resize(state.layout.size()); for (i = _graph.vertexBegin(); i < _graph.vertexEnd(); i = _graph.vertexNext(i)) { if (!branch.valid(i)) { d.diff(state.layout[i], v); d.rotate(si, co); layout[i].sum(d, v); } else layout[i] = state.layout[i]; } }
void Vec2f::rotateAroundSegmentEnd (const Vec2f &a, const Vec2f &b, float angle) { Vec2f c; c.diff(a, b); c.rotate(angle); sum(b, c); }
PeerCircle::PeerCircle( int id, std::string address, float bearing, std::string location, TorrentRef tr ) : Peer( id, address, bearing, location, tr) { Vec2f bv = sWorldRect.getSize() * Vec2f( .3f, .0f ); bv.rotate( bearing ); mOldPos = mPos = sWorldRect.getCenter() + bv; //mDecay = Rand::randFloat( .95f, .99f ); mAttractorPos = sWorldRect.getCenter(); }
void SimpleParticlesApp::peerReceived( PeerRef cr ) { //console() << "added " << *cr << endl; Vec2f bv = Vec2f( getWindowSize() ) * Vec2f( .1f, .0f ); bv.rotate( cr->getBearing() ); Vec2f pos = getWindowCenter() + bv; app::App::get()->dispatchSync( [&] { mEmitterController.addEmitter( Vec3f( Vec2f( pos ), 0.f ), Vec3f::zero() ); } ); }
void draw() { glPushMatrix(); gl::translate(pos); gl::color(TILECOLOR); gl::draw(*hex); gl::color(TILECOLOR2); glBegin(GL_TRIANGLE_FAN); PolyLine<Vec2f>::iterator pt; for(pt = hex->begin(); pt < hex->end(); pt++) { gl::vertex(*pt); } glEnd(); /* gl::color(Color(1.0f, .0f, .0f)); for(pt = hex->begin(); pt < hex->end() - 1; pt++) { gl::vertex(*pt); } */ for(int i = 0; i < 6; i++) { if(connections[i]) { Vec2f v = (connections[i]->pos - pos)/2.0f; gl::color(Color(.0f, 1.0f, .0f)); gl::drawLine(Vec2f(.0f, .0f), v); } if(state[i]) { Vec2f p = cart(TILERAD_MIN - 2.0f, -M_PI/2 - i * M_PI/3.0f); gl::color(Color(1.0f, .0f, .0f)); // gl::drawSolidCircle(p, 5.0f, 32); Vec2f pnorm = p.normalized(); pnorm.rotate(-M_PI/2.0f); Vec2f p0 = p - 22.0f * pnorm; Vec2f p1 = p + 22.0f * pnorm; gl::drawLine(p0, p1); } } glPopMatrix(); }
void Shape2D::add_rounded_line(const Pointf &start, const Pointf &end, float line_width, float cap_rounding, bool reverse) { float distance = start.distance(end); Sizef size(distance, line_width); Angle angle(start.angle_line(end)); Pointf center(start.x + size.width / 2.0f, start.y + size.height / 2.0f ); Vec2f rotated = start; rotated.rotate(center, angle); Vec2f origin( start.x + (start.x - rotated.x), start.y + (start.y - rotated.y)); rotated = Vec2f(0.0f, line_width/2.0f); rotated.rotate(Vec2f(), angle); origin.x -= rotated.x; origin.y -= rotated.y; add_rounded_rect(origin, size, cap_rounding, angle, reverse); }
void addParkingSpaces(Vec2f from, Vec2f direction, size_t count, bool entryFromLeft = true, float width = 30.f, float length = 75.f) { direction.normalize(); Vec2f entryVec = direction; entryVec.rotate(static_cast<float>(entryFromLeft ? M_PI / 2.f : -M_PI / 2.f)); for (size_t i = 0; i < count; ++i) { addSingleParkingSpace(ParkingSpace(width, length, from, entryVec)); from += direction * width; } }
void update(float dt) { Vec2f middle = Vec2f(getWindowWidth()/2.0f, getWindowHeight()/2.0f); vel = pos - middle; vel.normalize(); vel.rotate(M_PI/1.97f); vel *= 25.0f; pos += vel * dt; }
Planet(Vec2f _p, float _r) { pos = _p; radius = _r; Vec2f middle = Vec2f(getWindowWidth()/2.0f, getWindowHeight()/2.0f); vel = pos - middle; vel.normalize(); vel.rotate(M_PI/1.97f); vel *= 25.0f; }
void ParticleL::update(const Listener& list, const ci::Vec2f pos){ mAge++; if (mAge > mLifespan) mIsDead = true; float ageMap = 1.0f - (mAge / (float)mLifespan); mRadius = constrain(list.getVolume() * 2.f, .5f, 2.f ); Vec2f expander = mAnchorPosition; expander.rotate(mAge/20.f); expander *= mRadius; addPosition( expander ); }
void ParkingSpace::draw() { gl::color(1.0f, 1.0f, 1.0f); Vec2f perpVec = _entryDir * (_width / 2.f); perpVec.rotate(static_cast<float>(M_PI / 2.)); Vec2f leftStart = _location + perpVec; Vec2f leftEnd = leftStart - (_entryDir * _length); Vec2f rightStart = _location - perpVec; Vec2f rightEnd = rightStart - (_entryDir * _length); gl::drawLine(leftStart, leftEnd); gl::drawLine(rightStart, rightEnd); gl::drawLine(leftStart, rightStart); }
Vec2f MoleculeLayoutSmoothingSegment::_getPosition(Vec2f p) { Vec2f point; point.copy(p); point.rotate(_finish - _start); return point + _start; }
void cApp::update(){ /* add particle */ int numPs = ps.size(); if( frame%100 != 0 ){ int num = randInt(10, 30); for( int i=0; i<num; i++ ){ // pick up color int cid = numPs + i; int cidx = cid % mColorSample1.size(); int cidy = cid / mColorSample1.size(); cidx %= mColorSample1.size(); cidy %= mColorSample1[0].size(); ColorAf col = mColorSample1[cidx][cidy]; float angle = mPlns[1].fBm( cid*0.01 ) * 10.0; Vec2f vel = Vec2f(1,1) * randFloat(10, 20); vel.rotate(angle); ps.push_back( Vec2f(0,0) ); cs.push_back( col ); vs.push_back( vel ); } }else{ int num = randInt(20000, 30000); for( int i=0; i<num; i++ ){ // pick up color int cid = numPs + i; int cidx = cid % mColorSample1.size(); int cidy = cid / mColorSample1.size(); cidx %= mColorSample1.size(); cidy %= mColorSample1[0].size(); ColorAf col = mColorSample1[cidx][cidy]; Vec3f n = mPlns[0].dfBm( frame, col.r*col.g, col.b ); Vec2f vel; vel.x = n.x * n.y * 20; vel.y = n.y * n.z * 20; if(vel.length() <= 20){ vel = vel.normalized() * lmap(abs(n.z), 0.0f, 1.0f, 0.7f, 1.0f) *20; } ps.push_back( Vec2f(0,0) ); cs.push_back( col ); vs.push_back( vel ); } } /* update particle */ for( int i=0; i<ps.size(); i++ ){ Vec2f & p = ps[i]; Vec2f & v = vs[i]; ColorAf & c = cs[i]; Vec3f n = mPlns[1].dfBm( frame*0.01, i*0.01, c.b*0.5 ); c.r += n.x * 0.02; c.g += n.y * 0.02; c.b += n.z * 0.02; Vec2f new_vel; new_vel.x = n.x; new_vel.y = n.y; v += new_vel*2; p += v; v *= 0.999; c.a *= 0.999; } /* remove */ vector<Vec2f>::iterator pit = ps.begin(); vector<Vec2f>::iterator vit = vs.begin(); vector<ColorAf>::iterator cit = cs.begin(); int xmin = -mExp.mFbo.getWidth()/2; int xmax = mExp.mFbo.getWidth()/2; int ymin = -mExp.mFbo.getHeight()/2; int ymax = mExp.mFbo.getHeight()/2; while ( pit!=ps.end() ) { Vec2f & p = *pit; if( (p.x<xmin || xmax<p.x) || p.y<ymin || ymax<p.y ){ pit = ps.erase(pit); vit = vs.erase(vit); cit = cs.erase(cit); }else{ pit++; vit++; cit++; } } frame++; }
int improvement(int ind, int molSize, int *rotateAngle, int *edgeLenght, int *vertexNumber, Vec2f *p, bool profi, bool do_dist, float multiplier, int worstVertex) { Vec2f p1 = p[(worstVertex - 1 + ind) % ind]; Vec2f p2 = p[(worstVertex + 1 + ind) % ind]; float r1 = edgeLenght[(ind + worstVertex - 1) % ind]; float r2 = edgeLenght[(ind + worstVertex) % ind]; float len1 = Vec2f::dist(p1, p[worstVertex]); float len2 = Vec2f::dist(p2, p[worstVertex]); float r3 = Vec2f::dist(p1, p2) / sqrt(3.0); Vec2f p3 = (p1 + p2) / 2; if (rotateAngle[worstVertex] != 0) { Vec2f a = (p2 - p1) / sqrt(12.0f); a.rotate(PI / 2 * rotateAngle[worstVertex]); p3 += a; } else { p3 = (p1*r1 + p2*r2) / (r1 + r2); } float len3 = Vec2f::dist(p3, p[worstVertex]); if (rotateAngle[worstVertex] == 0) r3 = 0; //printf("%5.5f %5.5f %5.5f %5.5f\n", len1, len2, len3, r3); Vec2f newPoint; float eps = 1e-4; float eps2 = eps * eps; if (len1 < eps || len2 < eps || len3 < eps) { p[worstVertex] = (p1 + p2) / 2.0; } else { float coef1 = (r1 / len1 - 1); float coef2 = (r2 / len2 - 1); float coef3 = (r3 / len3 - 1); if (rotateAngle[worstVertex] != 0) { float angle = acos(Vec2f::cross(p1 - p[worstVertex], p2 - p[worstVertex]) / (Vec2f::dist(p1, p[worstVertex])*Vec2f::dist(p2, p[worstVertex]))); //if (angle < 2 * PI / 3) coef3 /= 10; } //if (!isIntersec(x[worstVertex], y[worstVertex], x3, y3, x1, y1, x2, y2)) coef3 *= 10; if (rotateAngle[worstVertex] == 0) coef3 = -1; //printf("%5.5f %5.5f %5.5f\n", coef1, coef2, coef3); newPoint += (p[worstVertex] - p1)*coef1; newPoint += (p[worstVertex] - p2)*coef2; newPoint += (p[worstVertex] - p3)*coef3; if (do_dist) { if (profi) { for (int i = 0; i < ind; i++) if (i != worstVertex && (i + 1) % ind != worstVertex) { float dist = Vec2f::distPointSegment(p[worstVertex], p[i], p[(i + 1) % ind]); if (dist < 1 && dist > eps) { Vec2f normal = (p[(i + 1) % ind] - p[i]); normal.rotate(PI / 2); float c = Vec2f::cross(p[i], p[(i + 1) % ind]); float s = normal.length(); normal /= s; c /= s; float t = -c - Vec2f::dot(p[worstVertex], normal); Vec2f pp; if (s < eps) { pp = p[i]; } else { pp = p[worstVertex] + normal * t; if (Vec2f::dist(p[worstVertex], p[i]) < Vec2f::dist(p[worstVertex], pp)) { pp = p[i]; } if (Vec2f::dist(p[worstVertex], p[(i + 1) % ind]) < Vec2f::dist(p[worstVertex], pp)) { pp = p[(i + 1) % ind]; } } float coef = (1 - dist) / dist; newPoint += (p[worstVertex] - pp) * coef; } } } else { float good_distance = 1; for (int j = 0; j < ind; j++) { int nextj = (j + 1) % ind; Vec2f pp = p[j]; Vec2f dpp = (p[nextj] - p[j]) / edgeLenght[j]; for (int t = vertexNumber[j], s = 0; t != vertexNumber[nextj]; t = (t + 1) % molSize, s++) { if (t != vertexNumber[worstVertex] && (t + 1) % molSize != vertexNumber[worstVertex] && t != (vertexNumber[worstVertex] + 1) % molSize) { float distSqr = Vec2f::distSqr(pp, p[worstVertex]); if (distSqr < good_distance && distSqr > eps2) { float dist = sqrt(distSqr); float coef = (good_distance - dist) / dist; //printf("%5.5f \n", dist); newPoint += (p[worstVertex] - pp)*coef; } } pp += dpp; } } } } newPoint *= multiplier; p[worstVertex] += newPoint; } return worstVertex; }
void update(float dt, vector<Planet*>& planets, paramStruct& params) { if(charging && charge < MAXCHARGE) charge += dt * 1.5f; pars = params; if(jumpInert > .0f) jumpInert-=dt; if(jumpInert < .0f) jumpInert = .0f; if(turn != params.turn) { charging = false; charge = .0f; } turn = params.turn; pos += vel * dt; if(pos.x > getWindowWidth()) { pos.x = pos.x-getWindowWidth(); } if(pos.x < 0) { pos.x = getWindowWidth()+pos.x; } if(pos.y > getWindowHeight()) { pos.y = pos.y-getWindowHeight(); } if(pos.y < 0) { pos.y = getWindowHeight()+pos.y; } Planet* closest = planets[0]; float dst = 999999999.0f; vector<Planet*>::iterator it; for(it = planets.begin(); it < planets.end(); it++) { Vec2f moonToPlanet = ((*it)->pos - pos); if(moonToPlanet.length() < dst) { closest = (*it); dst = moonToPlanet.length(); } // vel += moonToPlanet.normalized() * 1.0f * (*it)->radius * (*it)->radius * 3.14 / (math<float>::max(moonToPlanet.length() * moonToPlanet.length(), 80.0f)); // vel += moonToPlanet.normalized() * params.distFactor * moonToPlanet.length();//20000.0f/(math<float>::max(moonToPlanet.length(), 100.0f)); // vel += (-params.repulse)* moonToPlanet.normalized() / moonToPlanet.length(); } nearest = closest; Vec2f moonToPlanet = closest->pos - pos; if(!closestPlanet && moonToPlanet.length() > closest->radius * 2.5f) { vel += moonToPlanet.normalized() * 36.5f * pars.speed * closest->radius * closest->radius * closest->radius * (2.0f/3.0f) * M_PI / (math<float>::max(moonToPlanet.length() * moonToPlanet.length(), 80.0f)); } if(!jumpInert && !closestPlanet && moonToPlanet.length() < closest->radius * 1.7f && losing.size() == 0) { closestPlanet = closest; } if(closestPlanet && !jumpInert && losing.size() == 0) { Vec2f m2cp = (closestPlanet->pos - pos); Vec2f r = pos - closestPlanet->pos; r.normalize(); r.rotate(M_PI/1.97f); vel = r * 300.0f * pars.speed; if(m2cp.length() < closestPlanet->radius) { pos += -m2cp.normalized() * (5.0f + closestPlanet->radius - m2cp.length()); } } //vel /= planets.size(); // // Vec2f newpos = (pos + vel * dt); // for(it = planets.begin(); it < planets.end(); it++) // { // if(newpos.distance((*it)->pos) < (*it)->radius * 1.5f) // vel -= ((*it)->pos - pos); // } closest = 0; delete closest; // particles vector<Particle*>::iterator pit; for(pit = losing.begin(); pit < losing.end(); pit++) { (*pit)->update(dt); if((*pit)->time > (*pit)->lt) losing.erase(pit); } }
void Branch::draw() { //gl::color( Color::white() ); //gl::color( Color( 0, 1, 0 ) ); //gl::draw( mPath ); /* glEnableClientState( GL_VERTEX_ARRAY ); glVertexPointer( 2, GL_FLOAT, 0, &( mPoints[ 0 ] ) ); glDrawArrays( GL_LINE_STRIP, 0, mGrowPosition ); glDisableClientState( GL_VERTEX_ARRAY ); */ if ( mPoints.size() < 2 ) return; // create a new vector that can contain 3D vertices std::vector< Vec3f > vertices; // to improve performance, make room for the vertices + 2 adjacency vertices vertices.reserve( mGrowPosition + 2 ); // first, add an adjacency vertex at the beginning vertices.push_back( 2.0f * Vec3f( mPoints[ 0 ] ) - Vec3f( mPoints[ 1 ] ) ); // next, add all 2D points as 3D vertices size_t endIndex = 0; float currentLength = 0; for ( size_t i = 0; i < mGrowPosition; i++ ) { float d = 0; if ( !vertices.empty() ) d = vertices.back().distance( Vec3f( mPoints[ i ] ) ); // vertices.push_back( Vec3f( mPoints[ i ] ) ); if ( vertices.empty() || d > 5.f ) { vertices.push_back( Vec3f( mPoints[ i ] ) ); endIndex = i; currentLength += d; } } // next, add an adjacency vertex at the end size_t n = mGrowPosition; vertices.push_back( 2.0f * Vec3f( mPoints[ n - 1 ] ) - Vec3f( mPoints[ n - 2 ] ) ); // now that we have a list of vertices, create the index buffer n = vertices.size() - 2; std::vector< uint32_t > indices; indices.reserve( n * 4 ); for ( size_t i = 1 ; i < vertices.size() - 2; ++i ) { indices.push_back( i - 1 ); indices.push_back( i ); indices.push_back( i + 1 ); indices.push_back( i + 2 ); } std::vector< Vec2f > texCoords; n = vertices.size(); texCoords.reserve( n ); Vec2f uv( Vec2f::zero() ); Vec2f step( 0.0f, 1.f / n ); for ( size_t i = 0 ; i < n; ++i ) { texCoords.push_back( uv ); uv += step; } // finally, create the mesh gl::VboMesh::Layout layout; layout.setStaticPositions(); layout.setStaticIndices(); layout.setStaticTexCoords2d(); gl::VboMesh mVboMesh = gl::VboMesh( vertices.size(), indices.size(), layout, GL_LINES_ADJACENCY_EXT ); mVboMesh.bufferPositions( &(vertices.front()), vertices.size() ); mVboMesh.bufferIndices( indices ); mVboMesh.bufferTexCoords2d( 0, texCoords ); // FIXME: the first line segment is not drawn if ( sShader ) { sShader.bind(); sShader.uniform( "WIN_SCALE", mWindowSize ); sShader.uniform( "MITER_LIMIT", mLimit ); sShader.uniform( "THICKNESS", mThickness ); sShader.uniform( "tex0", 0 ); } gl::color( mColor ); if ( mStemTexture ) mStemTexture.bind(); gl::draw( mVboMesh ); if ( mStemTexture ) mStemTexture.unbind(); if ( sShader ) sShader.unbind(); // current direction Vec2f dir; if ( endIndex == 0 ) { dir = mPoints[ 1 ] - mPoints[ 0 ]; } else if ( endIndex < mPoints.size() - 1 ) { dir = mPoints[ endIndex ] - mPoints[ endIndex - 1 ]; } else { size_t n = mPoints.size() - 1; dir = mPoints[ n ] - mPoints[ n - 1 ]; } /* dir.normalize(); gl::color( Color( 1, 0, 0 ) ); gl::drawLine( mPoints[ endIndex ], mPoints[ endIndex ] + 20 * dir ); */ if ( ( currentLength - mLastItemLength ) > mSpawnInterval ) { mLastItemLength = currentLength; dir.rotate( mBranchAngle * mCurrentItemSide ); if ( Rand::randInt( 2 ) == 0 ) mBranchItems.push_back( BranchItem( mLeafTextures[ Rand::randInt( mLeafTextures.size() ) ], mPoints[ endIndex ], dir ) ); else mBranchItems.push_back( BranchItem( mFlowerTextures[ Rand::randInt( mFlowerTextures.size() ) ], mPoints[ endIndex ], dir ) ); mBranchItems.back().mScale = mItemScale; mCurrentItemSide *= -1.f; } gl::color( mColor ); for ( auto &item : mBranchItems ) { item.draw(); } #if 0 //const std::vector< Vec2f > &points = mPath.subdivide( .001f ); const std::vector< Vec2f > &points = mPath.getPoints(); gl::color( Color( 1, 0, 0 ) ); for ( auto p : points ) { gl::drawSolidCircle( p, 2 ); } #endif }
Vec2f C() { Vec2f rot = ((end - start) / 3); rot.rotate(-M_PI / 3); return start + ((end - start) / 3) + rot; }