@johnlime wrote:
I am trying to make a block coloring software. The screen consists of a grid where the user can click an any slot and color it black. The user can only click on the right side of the screen. Whichever slots that are colored are recorded in a vector array, and duplicated to the left side of the screen. This helps the user create a symmetrical drawing.
Here is the bug. Whenever I try to color one slot, sometimes another completely unrelated slot is colored black. This means that a slot not recorded in the array is somehow colored.
Here is the code.
vector <ofVec2f> ver; //This array is the locations of all the slots that are colored
int r=256;
int sides=r/4;
bool addvertices=true;
void ofApp::draw(){
//This code draws a grid on the screen.
int i=0;
int j=0;
while(i<=1024){
ofSetColor(0);
ofDrawLine(i, 0, i, 768);
i+=sides;
}
while(j<=768){
ofSetColor(0);
ofDrawLine(0, j, 1024, j);
j+=sides;
}
ofDrawRectangle(1024/2-5, 0, 10, 768);
ofDrawCircle(1024/2, 768/2, 5);
ofDrawBitmapString(ofGetFrameRate(), 1024/2, 768/2);
//All the slots that the user clicked are colored by drawing a rectangle there.
if(ver.size()!=0){
for(int i=0; i<=ver.size(); i++){
if(ver[i]!=ofVec2f(0, 0)){
ofSetColor(0);
ofDrawRectangle(ver[i].x, ver[i].y, sides, sides);
//This code duplicates the slot that the player colored onto the left side.
ofDrawRectangle(1024/2-(ver[i].x-1024/2)-sides, ver[i].y, sides, sides);
}
}
}
}
void ofApp::mousePressed(int x, int y, int button){
if(addvertices==true){
//When the user clicks on a slot in the grid, this checks if it is already colored.
//The user can only click on the right side of the screen.
if(mouseX>=1024/2 && mouseX%sides<sides-2 && mouseY%sides<sides-2){
bool taken=false;
//check if the box is already colored
for(int i=0; i<ver.size(); i++){
if(mouseX-(mouseX%sides)==ver[i].x &&
mouseY-(mouseY%sides)==ver[i].y){
//If the slot is already colored, its location is removed from vector array, erasing the
color from the slot.
taken=true;
ver.erase(ver.begin()+i);
}
}
//If the slot is not taken, its location is recorded in the vector array.
if(taken==false){
if(mouseX-(mouseX%sides)>=1024/2){
ver.push_back(ofVec2f(mouseX-(mouseX%sides), mouseY-(mouseY%sides)));
}
}
}
//This removes the risk of recording the location of a slot more than twice.
addvertices=false;
}
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
if(addvertices==false){
addvertices=true;
}
}I hope you can understand what I'm trying to say...
Posts: 2
Participants: 2