stdvector - C++: Dealing with 'Vector subscript out of range' -
using openframeworks 0.7.4, vs2012, v100 platform toolset.
i'm making family of circles, each spawning children circles if circles.size() < 200 && rand() % 20 == 0
.
each circle pushed std::vector
using push_back()
function.
the lines of polygon constructed children circles drawn according std::vector<std::vector<int>>
each of elements contain 6 integer values, each being previous child , current child circle's x , y positions, , index of previous , current child circle in mentioned std::vector
.
and @ random times error.
i tried using cout
check how many elements 2 vectors hold , results not constant:
1: circles.size() = 57, lines.size() = 56; 2: circles.size() = 42, lines.size() = 41; 3: circles.size() = 72, lines.size() = 71; 4: circles.size() = 78, lines.size() = 77;
the error mentioned pressing 'retry' debug.
the problem must in circles
.
these top parts , bottom parts when circles
collapsed.
i read questions asked on stack overflow don't think problem on using =
instead of push_back()
or 'stack corruption'.
the source codes:
testapp.cpp
#include "testapp.h" void testapp::setup(){ ofsetwindowshape(1200, 800); ofbackground(0, 0, 0); lcircles = llines = 0; circle rc = circle(600, 400); circles.push_back(rc); ofenablealphablending(); } void testapp::update(){ cout << circles.size() << ", " << lines.size() << endl; if (circles.size() < 200) { if (rand() % 20 == 0) { int ci = rand() % circles.size(); detectindex(ci, true); if (!circles[ci].isnear) { circles[ci].isnear = true; int iterant = rand() % 7 + 3; int actual = 0; (int cci = 0; cci < iterant; cci++) { circle nc = circle(circles[ci].x + cos(360 / iterant * cci * pi / 180) * spawnradius, circles[ci].y + sin(360 / iterant * cci * pi / 180) * spawnradius); if (!detectcircle(nc, true)) { circles.push_back(nc); actual++; circles[ci].radius++; } } (int sli = circles.size() - actual + 1; sli < circles.size(); sli++) { std::vector<int> nline; nline.push_back(circles[sli - 1].x); nline.push_back(circles[sli - 1].y); nline.push_back(circles[sli].x); nline.push_back(circles[sli].y); nline.push_back(sli - 1); nline.push_back(sli); lines.push_back(nline); } std::vector<int> nline; nline.push_back(circles[circles.size() - actual].x); nline.push_back(circles[circles.size() - actual].y); nline.push_back(circles[circles.size() - 1].x); nline.push_back(circles[circles.size() - 1].y); nline.push_back(circles.size() - actual); nline.push_back(circles.size() - 1); lines.push_back(nline); } } } } bool testapp::detectindex(int ci, bool change = false) { bool res = false; (int chi = 0; chi < circles.size(); chi++) { if (ci != chi) { if (ofdist(circles[ci].x, circles[ci].y, circles[chi].x, circles[chi].y) < circles[ci].aradar * circles[ci].radius) { res = true; if (change) circles[ci].isnear = circles[chi].isnear = true; } } } return res; } bool testapp::detectcircle(circle ci, bool change = false) { bool res = false; (int chi = 0; chi < circles.size(); chi++) { if (ofdist(ci.x, ci.y, circles[chi].x, circles[chi].y) < ci.aradar * ci.radius) { res = true; if (change) ci.isnear = circles[chi].isnear = true; } } return res; } void testapp::draw(){ (int dci = 0; dci < circles.size(); dci++) { ofsetcolor(circles[dci].color); ofcircle(circles[dci].x, circles[dci].y, circles[dci].radius); ofnofill(); ofsetcolor(circles[dci].color, 128); ofcircle(circles[dci].x, circles[dci].y, circles[dci].aradar * circles[dci].radius); offill(); } (int dli = 0; dli < lines.size(); dli++) { ofsetcolor(circles[lines[dli][4]].color); ofline(lines[dli][0], lines[dli][1], (lines[dli][2] + lines[dli][0]) / 2, (lines[dli][3] + lines[dli][1]) / 2); ofsetcolor(circles[lines[dli][5]].color); ofline(lines[dli][2], lines[dli][3], (lines[dli][2] + lines[dli][0]) / 2, (lines[dli][3] + lines[dli][1]) / 2); } } void testapp::keypressed(int key){ ofsaveframe(); }
non-default part of testapp.h
#define spawnradius 50 #include "circle.h" class testapp : public ofbaseapp { public: ... std::vector<circle> circles; std::vector<std::vector<int>> lines; bool detectindex(int ci, bool change); bool detectcircle(circle ci, bool change); int lcircles; int llines;
circle.h
#include "ofmain.h" class circle { public: int x; int y; int radius; int aradar; ofcolor color; bool isnear; circle(int cx, int cy) { x = cx; y = cy; radius = 5; aradar = 6; color = ofcolor(ofrandom(255)); isnear = false; } };
i don't know how use debugger in vs , code or debugger.
Comments
Post a Comment