Draggable curdrag = null; Draggable[] corners = new Draggable[5]; Draggable las = new Draggable(60,60); float curx,cury; float tryscaley,tryscalex,lastscalex,lastscaley; void setup(){ tryscaley = 0.5; tryscalex = 0.5; lastscalex = 1; lastscaley = 1; size(500,500); frameRate(30); smooth(); corners[0] = new Draggable(50,50); corners[1] = new Draggable(450,50); corners[2] = new Draggable(400,450); corners[3] = new Draggable(100,450); } void draw(){ fill(255); background(120); las.x = mouseX; las.y = mouseY; lines(); for (int i=0; i<4; i++){ corners[i].update(); } fill(255,0,0); las.update(); ellipse(500*tryscalex-5,500*tryscaley-5,10,10); } void lines(){ line(corners[0].x,corners[0].y, corners[1].x, corners[1].y); line(corners[1].x,corners[1].y, corners[2].x, corners[2].y); line(corners[2].x,corners[2].y, corners[3].x, corners[3].y); line(corners[3].x,corners[3].y, corners[0].x, corners[0].y); float mx = mouseX; float my = mouseY; //dotx(corners[0].x,corners[0].y,corners[1].x,corners[1].y,mx/500); //dotx(corners[3].x,corners[3].y,corners[2].x,corners[2].y,mx/500); //doty(corners[0].x,corners[0].y,corners[3].x,corners[3].y,tryscale); //doty(corners[1].x,corners[1].y,corners[2].x,corners[2].y,tryscale); tryscaley = 0.5; tryscalex = 0.5; lastscalex = 1; lastscaley = 1; for(int i=0; i<12; i++){ tryfory(corners[0].x,corners[0].y,corners[3].x,corners[3].y,corners[1].x,corners[1].y,corners[2].x,corners[2].y,tryscaley); tryforx(corners[0].x,corners[0].y,corners[1].x,corners[1].y,corners[3].x,corners[3].y,corners[2].x,corners[2].y,tryscalex); } } void tryfory(float ax1, float ay1, float ax2, float ay2, float bx1, float by1, float bx2, float by2,float ascale){ float y1 = ay1+ascale*(ay2-ay1); float x1 = ax1+(y1-ay1)/(ay2-ay1)*(ax2-ax1); float y2 = by1+ascale*(by2-by1); float x2 = bx1+(y1-by1)/(by2-by1)*(bx2-bx1); //result: two points that have the same virtual y //Now, find where cursor y would be at a given x float tcy = (y2-y1)/(x2-x1)*(las.x-x1)+y1; line(x1,y1,x2,y2); if(tcy < las.y){ float newsy = tryscaley + Math.abs((lastscaley-tryscaley)/2); lastscaley = tryscaley; tryscaley = newsy; } else if(tcy > las.y){ float newsy = tryscaley - Math.abs((lastscaley-tryscaley)/2); lastscaley = tryscaley; tryscaley = newsy; } } void tryforx(float ax1, float ay1, float ax2, float ay2, float bx1, float by1, float bx2, float by2,float ascale){ float x1 = ax1+ascale*(ax2-ax1); float y1 = ay1+(x1-ax1)/(ax2-ax1)*(ay2-ay1); float x2 = bx1+ascale*(bx2-bx1); float y2 = by1+(x1-bx1)/(bx2-bx1)*(by2-by1); //result: two points that have the same virtual y //Now, find where cursor y would be at a given x float tcx = (x2-x1)/(y2-y1)*(las.y-y1)+x1; line(x1,y1,x2,y2); if(tcx < las.x){ float newsx = tryscalex + Math.abs((lastscalex-tryscalex)/2); lastscalex = tryscalex; tryscalex = newsx; } else if(tcx > las.x){ float newsx = tryscalex - Math.abs((lastscalex-tryscalex)/2); lastscalex = tryscalex; tryscalex = newsx; } } void dotx(float ax1, float ay1, float ax2, float ay2, float ascale){ float x = ax1+ascale*(ax2-ax1); float y = ay1+(x-ax1)/(ax2-ax1)*(ay2-ay1); ellipse(x,y,6,6); } void doty(float ax1, float ay1, float ax2, float ay2, float ascale){ float y = ay1+ascale*(ay2-ay1); float x = ax1+(y-ay1)/(ay2-ay1)*(ax2-ax1); ellipse(x,y,6,6); } void mousePressed(){ for (int i=0; i<4; i++){ Draggable d = corners[i]; if(mouseX > d.x-5 && mouseX < d.x+d.wid+5){ if(mouseY > d.y-5 && mouseY < d.y+d.wid+5){ d.dragging = true; curdrag = d; } } } } void mouseReleased(){ if(curdrag != null){ curdrag.dragging = false; curdrag = null; } }