xxxxxxxxxx
80
function setup() {
createCanvas(windowWidth, windowHeight);
}
var X = 200;
var Y = 200;
var R = 100;
var N = 6;
var t = 0;
function draw() {
background(240, 240, 255);
module(width/2, height/2);
fill(255,100,255);
polygon(X,Y,R,N, t);
fill(0);
text("theta = " + nf(-t * 180 / PI,0,2), 200, 200);
if (mouseIsPressed) {
t = t - PI/90;
t %= 2 * PI; // caps @ 360 if you want to display theta
}
}
function draw() {
}
function module(x , y) {
let d = dist(x, y, mouseX, mouseY);
d = floor(d); // floor, round, ceil
let s = map(d, 100, 0, 0, 100);
s = constrain(s, 0, 100);
// if (s < 0) { s = 0; };
push();
translate(x,y);
//ellipse(0,0,10,10);
line(0,0,0, -s);
push();
translate(0,-2);
pop();
pop();
// ellipse(x, y, s*2, s*2);
line(x, y, mouseX, mouseY);
text(d, x, y);
}
function polygon(x, y, radius, npoints, theta) {
//degrees = radians × 180° / π
//radians = degrees × π /180°
var angle = TWO_PI / npoints; // TWO_PI is the equivalent of 360 degrees
console.log("radians: " + angle);
console.log("degrees: " + (angle *180/PI));
beginShape();
for (var a = theta; a <= TWO_PI; a = a + angle) {
var sx = x + cos(a) * radius;
var sy = y + sin(a) * radius;
vertex(sx, sy);
console.log("sx, sy: " + sx + ", " + sy);
}
endShape();
}
function windowResized() {
// !! this function runs when the window is resized, animations can be done using push and pop etc...
resizeCanvas(windowWidth, windowHeight);
module();
}