o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
private void drawTree(Position pos) { if (pos==selectedPosition){ pos.drawHighlighted();…
- User
private void drawTree(Position pos) { if (pos==selectedPosition){ pos.drawHighlighted(); } else { pos.draw(); } //draw the nodes under pos /*# YOUR CODE HERE */ pos.getTeam().forEach(this::drawTree);
}implement a force-directed algorithm to draw the tree.
- Assistant
// Implement force-directed algorithm to adjust positions of child nodes List<Position> children = pos.getTeam(); // Initialize positions of children if necessary for (Position child : children) { if (child.getPosition() == null) { // Place child near parent with some offset double angle = Math.random() * 2 * Math.PI; double radius = 100; double x = pos.getPosition().getX() + radius * Math.cos(angle); double y = pos.getPosition().getY() + radius * Math.sin(angle); child.setPosition(new Point2D(x, y)); } } // Apply forces to adjust positions int iterations = 50; double springLength = 100; double stiffness = 0.1; double repulsion = 5000; double damping = 0.85; for (int iter = 0; iter < iterations; iter++) { Map<Position, Vector2D> displacements = new HashMap<>(); for (Position v : children) { Vector2D displacement = new Vector2D(0, 0); // Attractive force toward parent Vector2D delta = pos.getPosition().subtract(v.getPosition()); double distance = delta.magnitude() + 0.01; Vector2D attractiveForce = delta.normalize().multiply(stiffness * (distance - springLength)); displacement = displacement.add(attractiveForce); // Repulsive forces among siblings for (Position u : children) { if (u != v) { delta = v.getPosition().subtract(u.getPosition()); distance = delta.magnitude() + 0.01; Vector2D repulsiveForce = delta.normalize().multiply(repulsion / (distance * distance)); displacement = displacement.add(repulsiveForce); } } displacements.put(v, displacement); } // Update positions for (Position v : children) { Vector2D displacement = displacements.get(v).multiply(damping); v.setPosition(v.getPosition().add(displacement)); } }