Node p;
Node q;
Node a;
Circle c;
Node cnode;
void setup() {
size(800, 800);
background(100);
c = new Circle(new PVector(width * 0.5, height * 0.5), 100);
cnode = new Node("C");
cnode.weight = 5;
cnode.position = c.center;
p = new Node("P");
q = new Node("Q");
a = new Node("A");
a.weight = 7;
update(700, 400);
}
void draw() {
background(255);
noStroke();
fill(0, 0, 255, 30);
triangle(a.position.x, a.position.y, c.center.x, c.center.y, p.position.x, p.position.y);
triangle(a.position.x, a.position.y, c.center.x, c.center.y, q.position.x, q.position.y);
stroke(0, 125);
noFill();
line(c.center.x, c.center.y, p.position.x, p.position.y);
line(c.center.x, c.center.y, q.position.x, q.position.y);
stroke(0, 255);
noFill();
c.draw();
fill(0);
cnode.draw();
stroke(0);
fill(0);
p.draw();
stroke(0);
fill(255);
q.draw();
stroke(0);
fill(0, 0, 255);
a.draw();
}
void mousePressed() {
update(mouseX, mouseY);
}
void mouseDragged() {
update(mouseX, mouseY);
}
void update(float x, float y) {
p.position = new PVector(x, y);
q.position = c.invert(p.position);
float phi;
float d;
if (p.position.dist(c.center) > c.r) {
phi = PVector.sub(p.position, c.center).heading();
d = c.center.dist(q.position);
} else {
phi = PVector.sub(q.position, c.center).heading();
d = c.center.dist(p.position);
}
float alpha = acos(d / c.r);
PVector v = PVector.fromAngle(phi + alpha);
v.mult(c.r);
a.position = PVector.add(c.center, v);
}
class Node {
PVector position;
String name;
int weight;
public Node(String name) {
this.position = new PVector(0, 0);
this.name = name;
this.weight = 10;
}
void draw() {
ellipse(this.position.x, this.position.y, this.weight, this.weight);
fill(50, 50, 255);
textSize(16);
text(this.name, this.position.x - 10, this.position.y - 10);
}
}
class Circle {
float r;
PVector center;
Circle(PVector center, float r) {
this.r = r;
this.center = center;
}
boolean containsPoint(PVector p) {
return this.center.dist(p) < this.r;
};
PVector invert(PVector p) {
float op = this.center.dist(p);
float oq = this.r * this.r / op;
PVector dq = PVector.sub(p, this.center).normalize().mult(oq);
PVector newP = PVector.add(this.center, dq);
return newP;
};
void draw() {
ellipse(this.center.x, this.center.y, 2 * this.r, 2 * this.r);
}
}