CoordinateSystem
FluxRender.core.CoordinateSystem
CoordinateSystem(x_range: tuple, y_range: tuple, width: int, height: int, keep_aspect_ratio: bool = False)
Handles coordinate transformations between the 2D screen space (pixels) and the mathematical simulation space.
This class manages the mapping from a mathematical domain (e.g., -10 to 10) to the render resolution (e.g., 0 to 1200). It supports aspect ratio correction to ensure that geometrical shapes retain their proportions (e.g., circles remain circular).
Attributes:
| Name | Type | Description |
|---|---|---|
width |
int
|
Screen width in pixels. |
height |
int
|
Screen height in pixels. |
x_min |
float
|
The actual lower bound of the mathematical X axis. |
x_max |
float
|
The actual upper bound of the mathematical X axis. |
y_min |
float
|
The actual lower bound of the mathematical Y axis. |
y_max |
float
|
The actual upper bound of the mathematical Y axis. |
math_width |
float
|
The total span of the X axis (max - min). |
math_height |
float
|
The total span of the Y axis (max - min). |
Initializes the coordinate system with specific bounds and screen dimensions.
If keep_aspect_ratio is set to True, the provided ranges will be adjusted
(expanded) to match the screen's aspect ratio. The strategy used is "Fit/Expand":
it ensures the requested range is fully visible, adding extra margins to the
shorter axis if necessary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_range
|
tuple[float, float]
|
The desired (min, max) values for the X axis. |
required |
y_range
|
tuple[float, float]
|
The desired (min, max) values for the Y axis. |
required |
width
|
int
|
Window/buffer width in pixels. |
required |
height
|
int
|
Window/buffer height in pixels. |
required |
keep_aspect_ratio
|
bool
|
If True, adjusts x_range or y_range to preserve 1:1 scaling (square pixels). Defaults to False. |
False
|
Example
To create a coordinate system that maps the mathematical range of -2 to 2 on both axes to a screen resolution of 1800x950 pixels without keeping the aspect ratio:
To create the same coordinate system but with aspect ratio correction (ensuring circles look like circles):
Then, in the above case, the ranges on the X and Y axis will be adjusted accordingly to maintain the aspect ratio.Source code in FluxRender/core.py
move
Moves the virtual mathematical environment by given values in units of the width and height of the mathematical space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_offset
|
float
|
Camera offset relative to the X axis by x_offset*math_width |
required |
y_offset
|
float
|
Camera offset relative to the Y axis by y_offset*math_height |
required |
Example
To move the camera to the right by 10% of the current mathematical width and up by 10% of the current mathematical height:
Source code in FluxRender/core.py
to_math
Converts screen coordinates (pixels) to mathematical coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pixel_x
|
float / tuple / list
|
X coordinate on the screen. |
required |
pixel_y
|
float / tuple / list
|
Y coordinate on the screen. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
point |
tuple[Float, Float]
|
Corresponding coordinates in the mathematical domain. |
Source code in FluxRender/core.py
to_screen
Converts mathematical coordinates to screen coordinates (pixels).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_math
|
float / tuple / list
|
X coordinate(s) in the mathematical domain. |
required |
y_math
|
float / tuple / list
|
Y coordinate(s) in the mathematical domain. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
point |
tuple[Float, Float]
|
Corresponding coordinates on the screen in pixels. |
Source code in FluxRender/core.py
zoom
Zooms the camera in/out in a virtual mathematical environment relative to the pivot point (in standard units from 0 to 1) by a given factor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor
|
float
|
Scale multiplier (e.g. 1.1 = zoom out, 0.9 = zoom in) |
required |
pivot
|
tuple
|
The pivot point for zooming, given as (x, y) in normalized screen coordinates (0 to 1). |
required |