using toxiclibs geomutils to solve the "pulley problem"
in
Contributed Library Questions
•
2 years ago
I've been trying to use geomutils to create a simple 2 pulley and 1 rope graphic using the
pulley problem equation and having problems which are more related to me and trigonometry than geomutils... I know that acos returns a float between 0 and PI (right?) so I did some trial and error with dividing the first theta by 2 and multiplying the second theta by -1 and etc but couldn't solve it...
import java.util.Arrays; import java.util.Comparator; import toxi.geom.*; import toxi.processing.*; ToxiclibsSupport gfx; int count = 2; float sizeMin = 25; float sizeMax = 100; Circle[] circles; public void setup() { size(800, 600, P3D); noFill(); gfx = new ToxiclibsSupport(this); setupCircles(); } public void setupCircles() { circles = new Circle[count]; for (int i = 0; i < count; i++) circles[i] = new Circle((int) random(sizeMax, width - sizeMax), (int) random(sizeMax, height - sizeMax), random(sizeMin, sizeMax)); // circles[0] = new Circle(250, height / 2, 150); // circles[1] = new Circle(625, height / 2, 75); Arrays.sort(circles, new CircleXComparator()); } public void draw() { background(255); for (int i = 0; i < count; i++) { stroke(0); noFill(); gfx.ellipse(circles[i]); if (i > 0) { Circle c1 = circles[i - 1]; Circle c2 = circles[i]; // circle 1 thetas float c1a2 = 2 * acos((c1.getRadius() - c2.getRadius()) / c1.distanceTo(c2)); float c1a1 = c1a2 / 2; // circle 2 thetas float c2a2 = 2 * acos((c2.getRadius() - c1.getRadius()) / c2.distanceTo(c1)); float c2a1 = c1a2 / 2; // circle 1 points Vec2D c1v1 = new Vec2D(c1.x() + cos(c1a1) * c1.getRadius(), c1.y() + sin(c1a1) * c1.getRadius()); Vec2D c1v2 = new Vec2D(c1.x() + cos(c1a2) * c1.getRadius(), c1.y() + sin(c1a2) * c1.getRadius()); // circle 2 points Vec2D c2v1 = new Vec2D(c2.x() + cos(c2a1) * c2.getRadius(), c2.y() + sin(c2a1) * c2.getRadius()); Vec2D c2v2 = new Vec2D(c2.x() + cos(c2a2) * c2.getRadius(), c2.y() + sin(c2a2) * c2.getRadius()); // circle 1 to circle 2 line gfx.line(new Line2D(c1, c2)); // circle 1 lines stroke(255, 0, 0); gfx.line(new Line2D(c1, c1v1)); gfx.line(new Line2D(c1, c1v2)); // circle 2 lines gfx.line(new Line2D(c2, c2v1)); gfx.line(new Line2D(c2, c2v2)); // gfx.polygon2D((new Polygon2D(c1v1,c1v2,c2v1,c2v2)); Polygon2D p1 = new Polygon2D(); p1.add(c1v1); p1.add(c1v2); p1.add(c2v2); p1.add(c2v1); stroke(0, 255, 0); gfx.polygon2D(p1); } } } public void keyPressed() { setupCircles(); } public class CircleXComparator implements Comparator<Circle> { public int compare(Circle _c1, Circle _c2) { return (int) (_c1.x() - _c2.x()); } }
PS Thanks Anthony Mattox for the great
code formatter!
1