StudyPane.java
import javafx.scene.Scene; import javafx.scene.Node; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.*; import java.util.*; import javafx.scene.control.*; public class StudyPane extends Pane { double s = 10; double radius1 = 1.5; double qx = 22; double qy = 8; Circle circ1 = new Circle(qx*s,qy*s,radius1*s); double radius2 = 1.0; double px = 10; double py = 13; Circle circ2 = new Circle(px*s,py*s,radius2*s); double vx = 8; double vy = -4; Line vec; double t1, t2; double wd,ht; TextArea area; boolean flag = true; int ns; StudyPane() { wd = 300; ht = 200; setStyle("-fx-background-color: honeydew;"); Rectangle rect = new Rectangle(0,0,wd,ht); rect.setFill(Color.WHITE); vec = new Line(px*s,py*s,(px+vx)*s,(py+vy)*s); circ1.setFill(Color.GREEN); circ2.setFill(Color.BLUE); getChildren().addAll(rect,vec,circ1,circ2); area = new TextArea(); area.setPrefRowCount(6); area.setWrapText(true); area.setEditable(false); area.appendText("Ball study"); setOnMousePressed( e -> { double x = e.getX(); double y = e.getY(); ns = 0; if (flag) { px = x/s; py = y/s; circ2.setCenterX(x); circ2.setCenterY(y); vx = 0; vy = 0; flag= false; } else { vx = x/s - px; vy = y/s - py; flag = true; } vec.setStartX(px*s); vec.setStartY(py*s); vec.setEndX((px+vx)*s); vec.setEndY((py+vy)*s); solve(); } ); } public void solve() { t1 = t2 = -1; // relative velocity double rx = vx; double ry = vy; // relative position double dx = px - qx; double dy = py - qy; // test radius double r = radius1 + radius2; double A = rx*rx+ry*ry; double B = rx*dx+ry*dy; double C = dx*dx + dy*dy - r*r; area.clear(); area.appendText(String.format("A %g B %g C %g%n",A,B,C)); double radical = B*B-A*C; area.appendText(String.format("radical B*B - A*C = %g%n",radical)); if (A<=0.0) { area.appendText(String.format("A==0, No motion%n")); return; } if (radical<0) { area.appendText(String.format("radical < 0, No solution%n")); return; } if (B>0) { area.appendText(String.format("B>0, balls moving away from one another%n")); return; } double R = Math.sqrt(radical); t1 = (-B-R)/A; t2 = (-B+R)/A; area.appendText(String.format("t1 = %g t2 = %g%n",t1,t2)); } public void move(int pos) { ns = pos; if (pos==0) { circ2.setCenterX(px*s); circ2.setCenterY(py*s); } if (pos==1 && t1>0) { circ2.setCenterX((px+vx*t1)*s); circ2.setCenterY((py+vy*t1)*s); } if (pos==2 && t2>0) { circ2.setCenterX((px+vx*t2)*s); circ2.setCenterY((py+vy*t2)*s); } } }
Maintained by John Loomis, updated Sun Mar 04 16:16:51 2018