Skip to content

Instantly share code, notes, and snippets.

@tsurumau
Created August 13, 2014 20:59
Show Gist options
  • Save tsurumau/8bec562aa82b90eacb13 to your computer and use it in GitHub Desktop.
Save tsurumau/8bec562aa82b90eacb13 to your computer and use it in GitHub Desktop.
int srcX, srcY, dstX, dstY;
void setup()
{
srcX = srcY = dstX = dstY = 0;
size(640, 480);
}
void draw()
{
background(125);
//line(srcX, srcY, dstX, dstY);
if (srcX != dstX && srcY != dstY) {
drawSquare();
}
}
void drawSquare()
{
float size = dist(srcX, srcY, dstX, dstY);
PVector src = new PVector(srcX, srcY);
PVector dst = new PVector(dstX, dstY);
PVector dir = PVector.sub(dst, src);
dir.rotate(HALF_PI);
dir.limit(size/2);
PVector v1 = dir.get();
PVector v2 = v1.get();
v2.rotate(PI);
PVector v3 = v2.get();
PVector v4 = v1.get();
v1.add(src);
v2.add(src);
v3.add(dst);
v4.add(dst);
quad(v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, v4.x, v4.y);
}
void mousePressed()
{
srcX = mouseX;
srcY = mouseY;
dstX = mouseX;
dstY = mouseY;
}
void mouseReleased()
{
dstX = mouseX;
dstY = mouseY;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment