Hello World

The Interface can be accessed not just from the command line, but also programmatically. In this section, you’ll create a script that creates a billiards system, simulates it, and then visualizes it with the interface.

Consider this hello world for pooltool.

Script

For those that want to jump straight in, here is the script:

#! /usr/bin/env python

import pooltool as pt

# We need a table, some balls, and a cue stick
table = pt.Table.default()
balls = pt.get_rack(pt.GameType.NINEBALL, table)
cue = pt.Cue(cue_ball_id="cue")

# Wrap it up as a System
shot = pt.System(table=table, balls=balls, cue=cue)

# Aim at the head ball with a strong impact
shot.cue.set_state(V0=8, phi=pt.aim.at_ball(shot, "1"))

# Evolve the shot.
pt.simulate(shot, inplace=True)

# Open up the shot in the GUI
pt.show(shot)

The rest of this document details a line-by-line explanation.

Explanation

Importing

First thing’s first, the pooltool package is imported which gives you access to the top-level API:

import pooltool as pt

The most important classes and functions are available directly from this top-level API.

Creating a System

Then a table, a cue stick, and a collection of balls are created and wrapped up into a System:

# We need a table, some balls, and a cue stick
table = pt.Table.default()
balls = pt.get_rack(pt.GameType.NINEBALL, table)
cue = pt.Cue(cue_ball_id="cue")

# Wrap it up as a System
shot = pt.System(table=table, balls=balls, cue=cue)

Aiming the cue stick

The cue stick parameters are then set with Cue.set_state. A large impact speed of V0=8 (m/s) is chosen, and an aiming utility function is used to aim the cue ball directly at the one-ball.

# Aim at the head ball with a strong impact
shot.cue.set_state(V0=8, phi=pt.aim.at_ball(shot, "1"))

Simulating a system

The cue parameters have been set, but the system still hasn’t been simulated. This is done with a call to pooltool.evolution.simulate().

# Evolve the shot.
pt.simulate(shot, inplace=True)

The system has now been evolved from its initial to its final state.

Opening the GUI

To visualize the shot, open the GUI with pooltool.show():

# Open up the shot in the GUI
pt.show(shot)

Next

Obviously, this script is just the beginning. Pooltool offers much more than this, which means you have much more to learn. From here, you should check out the Examples page and dive into whatever topic interests you.