PVector randomInCirc(float cx, float cy, float d) {
d/=4;
float t = TWO_PI*random(d);
float u = random(d)+random(d);
d = (u>1) ? 2-u : u;
return new PVector(cx+d*cos(t), cy+d*sin(t));
}
PVector randomInCircSnap(float cx, float cy, float d, int snap) {
d/=4;
float t = TWO_PI*random(d);
float u = random(d)+random(d);
d = (u>1) ? 2-u : u;
PVector end = new PVector(cx+d*cos(t), cy+d*sin(t));
return new PVector(round((end.x/snap))*snap, round((end.y/snap))*snap);
}
PVector randomInTri(PVector A, PVector B, PVector C) {
float r1 = random(1);
float r2 = random(1);
float x = (1 - sqrt(r1)) * A.x + (sqrt(r1) * (1 - r2)) * B.x + (sqrt(r1) * r2) * C.x;
float y = (1 - sqrt(r1)) * A.y + (sqrt(r1) * (1 - r2)) * B.y + (sqrt(r1) * r2) * C.y;
return new PVector(x, y);
}
PVector quantize2dVector(PVector v, int units)
{
float d = dist(v.x, v.y,0,0);
float a = atan2(v.y,v.x);
a = round(a*units/TWO_PI) * TWO_PI/units;
return new PVector( cos(a)*d, sin(a)*d );
}
void setup()
{
size(500,500);
}
void draw()
{
background(255);
pushMatrix();
translate(width/2, height/2);
PVector m = new PVector(mouseX, mouseY);
PVector c = new PVector(width/2, height/2);
// Compuate actual vector towards mouse
float d = PVector.dist(m,c);
float a = atan2(mouseY-width/2, mouseX-height/2);
PVector v = new PVector(cos(a)*d, sin(a)*d);
float mag = v.mag();
println(mag);
// Draw original line
strokeWeight(2);
stroke(0);
line(0,0, v.x, v.y);
// Quantize it
v = quantize2dVector(v,8);
// Draw Quantized line in green
stroke(0,255,0);
line(0,0, v.x, v.y);
popMatrix();
}
PVector quantize2dVector(PVector v, int units)
{
float d = dist(v.x, v.y,0,0);
float a = atan2(v.y,v.x);
a = round(a*units/TWO_PI) * TWO_PI/units;
return new PVector( cos(a)*d, sin(a)*d );
}