xxxxxxxxxx
float[] dragPoints;
float[] coords;
int dragPoint;
float dragPointBackup0, dragPointBackup1;
boolean dragging, activated;
int dragX, dragY;
PFont fnt;
PImage bg;
float pi = 2*acos(0.0);
int appW, appH;
float originX, originY, scale;
int numDragPoints = 2;
void setup() {
appW = 600;
appH = 400;
originX = appW/2;
originY = appH/2;
scale = appH*7/16;
size(600, 400);
bg = createImage(appW, appH, RGB);
dragging = false;
dragPoint = -666;
dragPoints = new float[numDragPoints*2];
dragPoints[0] = originX-appW*0.125;
dragPoints[1] = originY+appH*0.125;
dragPoints[2] = originX+appW*0.125;
dragPoints[3] = originY-appH*0.125;
fnt = createFont("Arial",16,true);
ellipseMode(RADIUS);
activated = false;
}
void findDragPoint() {
int cutoff = 49;
int oldDragPoint = dragPoint;
float dragPointD = 666666666;
dragPoint = -666;
for (int t = 0; t < numDragPoints; t++) {
float d2 = (mouseX-dragPoints[t*2])*(mouseX-dragPoints[t*2]) + (mouseY-dragPoints[t*2+1])*(mouseY-dragPoints[t*2+1]);
if (d2 <= dragPointD) {
dragPointD = d2;
if (dragPointD < cutoff) {
dragPoint = t;
}
}
}
if (dragPoint != oldDragPoint) {
loop();
}
}
void mouseMoved() {
if (activated) {
if (!dragging) {
findDragPoint();
loop();
}
}
}
void mouseClicked() {
if (dragPoint < 0) {
activated = !activated;
if (activated) {
findDragPoint();
}
}
loop();
}
void mousePressed() {
if (dragPoint >= 0) {
dragging = true;
dragPointBackup0 = dragPoints[dragPoint*2];
dragPointBackup1 = dragPoints[dragPoint*2+1];
} else {
dragging = false; // Not needed?
}
loop();
}
void mouseDragged() {
if (!activated) {
dragPoint = -666;
activated = true;
findDragPoint();
}
if (dragging) {
int x = mouseX;
int y = mouseY;
if (x < 5) {
x = 5;
} else if (x >= appW - 5) {
x = appW - 6;
}
if (y < 5) {
y = 5;
} else if (y >= appH - 5) {
y = appH - 6;
}
dragPoints[dragPoint*2] = x;
dragPoints[dragPoint*2+1] = y;
loop();
}
}
void mouseReleased() {
if (activated && dragging) {
dragging = false;
loop();
}
}
float sign(float value) {
if (value > 0) {
return 1.0;
} else if (value < 0) {
return -1.0;
} else {
return 0;
}
}
void draw() {
for(int y = 0; y < appH; y++) {
for(int x = 0; x < appW; x++) {
float x0 = (dragPoints[0]-x)/scale;
float y0 = (dragPoints[1]-y)/scale;
float x1 = (dragPoints[2]-x)/scale;
float y1 = (dragPoints[3]-y)/scale;
float gain;
if (x0 == x1 && y0 == y1) {
gain = sqrt(x0*x0 + y0*y0);
} else if (x0 == 0 && y0 == 0) {
gain = exp(-1)*sqrt(x1*x1 + y1*y1);
} else if (x1 == 0 && y1 == 0) {
gain = exp(-1)*sqrt(x0*x0 + y0*y0);
} else {
gain = exp((x0*y1 - x1*y0)*atan2(x0*y1 - x1*y0, x0*x1 + y0*y1)/(sq(x0 - x1) + sq(y0 - y1)) - 1)*pow(x0*x0 + y0*y0, (x1*(x0 - x1) + y0*(y0 - y1) + sq(x0 - x1))/(2*(sq(x0 - x1) + sq(y0 - y1))))*pow(x1*x1 + y1*y1, (x0*(x1 - x0) + y1*(y1 - y0) + sq(x1 - x0))/(2*(sq(x1 - x0) + sq(y1 - y0))));
}
int intensity10 = round(log(gain)/log(10)*0x200)&0xff;
int intensity1 = round(log(gain)/log(10)*(0x200*10))&0xff;
bg.pixels[y*appW + x] = color(intensity10, 0xff, intensity1);
}
}
image(bg, 0, 0);
noFill();
stroke(0, 0, 255);
strokeWeight(1);
line(dragPoints[0], dragPoints[1], dragPoints[2], dragPoints[3]);
//ellipse(originX, originY, scale, scale);
if (!activated) {
textFont(fnt,16);
fill(0, 0, 0);
text("Click to activate",10,20);
for (int x = 0; x < appW; x++) {
color c = color(110*x/appW+128, 110*x/appW+128, 110*x/appW+128);
set(x, 0, c);
}
for (int y = 0; y < appH; y++) {
color c = color(110*y/appH+128, 110*y/appH+128, 110*y/appH+128);
set(0, y, c);
}
}
for (int u = 0; u < numDragPoints; u++) {
stroke(0, 0, 255);
if (dragPoint == u) {
if (dragging) {
fill(0, 0, 255);
strokeWeight(3);
ellipse(dragPoints[u*2], dragPoints[u*2+1], 5, 5);
} else {
noFill();
strokeWeight(3);
ellipse(dragPoints[u*2], dragPoints[u*2+1], 6, 6);
}
} else {
//noFill();
//strokeWeight(1);
//ellipse(dragPoints[u*2], dragPoints[u*2+1], 6, 6);
}
}
noLoop();
}