xxxxxxxxxx
149
let t = 0;
let tr = 0;
let ω = 1 / 60;
let r = 50;
let rel = 0;
let g = 1 / 6;
let x = 0;
let y = 0;
let l = [];
class dline {
constructor(x1, y1, x2, y2, c, w) {
if (w === undefined) {
w = 1;
}
if (c === undefined) {
c = "black";
}
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.width = w;
this.colour = c;
this.n1 = y1 - y2;
this.n2 = x2 - x1;
this.c = -this.n1 * x1 - this.n2 * y1;
this.length = sqrt(sq(this.n1) + sq(this.n2));
}
draw() {
stroke(this.colour);
strokeWeight(this.width);
line(this.x1, this.y1, this.x2, this.y2);
stroke("black");
strokeWeight(1);
}
dist(x, y) {
if (
max(dist(x, y, this.x1, this.y1), dist(x, y, this.x2, this.y2)) >
this.length
) {
return min(dist(x, y, this.x1, this.y1), dist(x, y, this.x2, this.y2));
} else {
return abs(this.n1 * x + this.n2 * y + this.c) / this.length;
}
}
}
class dpoly {
constructor(x, c, w) {
if (w === undefined) {
w = 1;
}
if (c === undefined) {
c = "black";
}
this.x = x;
this.width = w;
this.colour = c;
}
draw() {
stroke(this.colour);
strokeWeight(this.width);
for (let i = 0; i < this.x.length - 2; i += 2) {
line(this.x[i], this.x[i + 1], this.x[i + 2], this.x[i + 3]);
}
line(
this.x[this.x.length - 2],
this.x[this.x.length - 1],
this.x[0],
this.x[1]
);
stroke("black");
strokeWeight(1);
}
ldist(l) {
let d = l.dist(this.x[0], this.x[1]);
print(d);
for (let i = 2; i < this.x.length; i += 2) {
d = min(d, l.dist(this.x[i], this.x[i + 1]));
}
return d;
}
setCoord(x) {
this.x = x;
}
}
function setup() {
createCanvas(400, 400);
l1 = new dline(100, 100, 100, 400);
l2 = new dline(300, 100, 300, 400);
l = [l1, l2];
//p1=new dpoly([0,0,0,10,10,0,10,10],'blue',1)
c = createVector(200, 200);
s = createVector(200, 200);
v = createVector(0, 0);
}
function touchingLine() {
for (let i = 0; i < l.length; i++) {
if (l[i].dist(s.x, s.y) < 4*v.mag() && rel == 2) {
return i;
}
}
return false;
}
function draw() {
background(220);
t++;
if (rel < 2) {
c.set(mouseX, mouseY);
s = p5.Vector.add(c, [r * cos(ω * t), r * sin(ω * t)]);
line(c.x, c.y, s.x, s.y);
if (mouseIsPressed == true) {
t = t / 1.01;
ω *= 1.01;
rel = 1;
} else if (rel == 1) {
rel = 2;
v.set([-r * ω * sin(ω * t), r * ω * cos(ω * t)]);
t = 0;
}
} else {
v.y += g;
s.add(v);
}
if (keyIsDown(32)) {
t = 0;
rel = 0;
ω = 1 / 60;
}
circle(s.x, s.y, 10);
l1.draw();
l2.draw();
if (touchingLine()!==false) {
v.add([-l1.n1*2*abs(v.dot(l1.n1,l1.n2))/v.dot(l1.n1,l1.n2)/l1.length,-l1.n2*2*abs(v.dot(l1.n1,l1.n2))/v.dot(l1.n1,l1.n2)/l1.length]);
while(touchingLine()!==false){
s.add(v);
print(touchingLine());
}
//v[1]=tr;
} else {
x += mouseX - x;
y += mouseY - y;
fill("transparent");
}
//p1.setCoord([mouseX,mouseY+50,mouseX-50,mouseY,mouseX,mouseY-50,mouseX+50,mouseY])
print(l1.class)
}