11Levels
YaBB Newbies
Offline
Posts: 14
Clicking and Dragging Shape Corners
Jan 28th , 2009, 4:47am
Hey guys, I'm very new to processing - I found about it in January, saw great potential and started experimenting and learning. I was hoping someone can help me out with a code I'm having trouble with. I'm importing a set of coordinates from an external txt file and putting the resulting shape on the drawing window. This much I was able to figure out. What I wanna do next - and which has been killing me for over a weeks now, and I seem to be giving up - is that I want to be able to drag any corner of the shape and change this point's position and consequently dragging the connecting lines with it resulting in a new shape. Say if the mouse is at least 5 pixels away from the corner, I can click and drag it to its new location. I tried using classes, which confused me, some if else statements, for structures.., I tried adding transparent circles (diameter=10) around each corner, so that if the mouse is over the circle, I can click and drag my point, but I didn't know how to go about controlling the mouseOver... I just don't seem to be getting the hang of it. Can anyone PLEASE help me out with this one! Here's the code that I have (importing the coordinates). int x1, y1, x2, y2, firstX, firstY; String[] coords; void setup(){ smooth(); strokeWeight(2); coords = loadStrings("coordinates.txt"); } void draw(){ for (int i=0; i<coords.length; i++){ //turn the data in the file coordinates.txt into an array called coords. then split this array into pieces, each having two positions [0] and [1], one for the x-position the other for the //y-position, place these coordinates into two variables x1 and y1 respectively (tell them apart by the TAB between each coordinate). String[] pieces = split(coords[i], '\t'); if (pieces.length == 2){ x1 = int(pieces[0]); y1 = int(pieces[1]); if(i==0){//when we're in the first round, draw a point on the screen, and place the coords of the point into different variables (x2,y2) to be able to use them in drawing a line when i>0 point(x1,y1); x2=x1; y2=y1; //keep the coords of the first point in separate variables to close the shape at the end firstX=x1; firstY=y1; } //after the first point, draw the lines connecting the coordinates if(i>0){ //when i=1 draw a line between the first coordinate and the second coordinate, turn the second coordinate into the first one and so on. line(x1,y1,x2,y2); if(i==(coords.length-1)){//when we reach the last item in the coords array, draw a line between the last point and the very first point line(x1,y1,firstX,firstY); } x2=x1; y2=y1; }//end if(i>0) }//end if (pieces.length == 2) }//end for() /* //if the mouse is pressed and it is near the first point by ±5pixels, allow dragging the first point with the mouse by setting it's coordinates to mouseX mouseY. if(mousePressed){ if( (mouseX==firstX+5) || (mouseX==firstX-5) || (mouseY==firstY+5) || (mouseY==firstY-5) ){ firstX=mouseX; firstY=mouseY; } }*/ }//end draw and the set of coordinates that i'm using is 15.0 21.0 58.0 14.0 90.0 63.0 50.0 97.0 37.0 52.0 11.0 62.0 and it's stored in coordinates.txt and are separated by tabs but it should work with any set of coordinates. Thank you infinitely! Marianne