Skip to content

Instantly share code, notes, and snippets.

@micycle1
Last active August 5, 2024 09:57
Show Gist options
  • Save micycle1/05e710ed88bec87de3afd94c2c60d601 to your computer and use it in GitHub Desktop.
Save micycle1/05e710ed88bec87de3afd94c2c60d601 to your computer and use it in GitHub Desktop.
Exterior bisecting angle and tangent
void testBisector() {
PVector C = new PVector(100, 300);
PVector B = new PVector(mouseX, mouseY);
PVector A = new PVector(500, 500);
float r = 100;
float[] angle = new float[1]; // To store the calculated angle
PVector point = findPointOnBisector(A, B, C, r, angle);
stroke(0);
strokeWeight(2);
text(degrees(angle[0]), 10, 10);
// Draw vectors
line(A.x, A.y, B.x, B.y);
line(B.x, B.y, C.x, C.y);
// Draw points
fill(255, 0, 0);
ellipse(A.x, A.y, 10, 10);
ellipse(B.x, B.y, 10, 10);
ellipse(C.x, C.y, 10, 10);
// Draw result point
fill(0, 255, 0);
ellipse(point.x, point.y, 10, 10);
// Draw line from B to result point
stroke(0, 255, 0);
line(B.x, B.y, point.x, point.y);
// Draw perpendicular vector
PVector tangent = new PVector(-sin(angle[0]), cos(angle[0]));
tangent.mult(50); // Scale vector for visibility
PVector endTangent = PVector.add(point, tangent);
stroke(255, 0, 0);
line(point.x, point.y, endTangent.x, endTangent.y);
fill(0);
text("Perpendicular Vector", endTangent.x, endTangent.y);
}
PVector findPointOnBisector(PVector A, PVector B, PVector C, float r, float[] angle) {
// Calculate vectors
PVector v1 = PVector.sub(B, A);
PVector v2 = PVector.sub(C, B);
// Normalize vectors
PVector u1 = v1.copy().normalize();
PVector u2 = v2.copy().normalize();
// Add normalized vectors (negating u1 for exterior angle)
PVector w = PVector.sub(u2, u1);
// Normalize the result
w.normalize();
w.mult(r);
PVector result = PVector.sub(B, w);
if (angle != null) {
// Calculate the angle of the line tangent to the line from B to result point
PVector tangentVector = PVector.sub(result, B);
angle[0] = atan2(tangentVector.y, tangentVector.x) - PI; // Subtract HALF_PI to rotate 90 degrees CCW
angle[0] = (angle[0] + TWO_PI) % TWO_PI; // Normalize angle to be between 0 and TWO_PI
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment