|
| 1 | +""" |
| 2 | +Sine and Cosine functions |
| 3 | +========================= |
| 4 | +
|
| 5 | +Identical to the Unit Circle example but you can change the angular frequencies using a UI |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +# test_example = false |
| 10 | +# sphinx_gallery_pygfx_docs = 'screenshot' |
| 11 | + |
| 12 | +import glfw |
| 13 | +import numpy as np |
| 14 | +import fastplotlib as fpl |
| 15 | +from fastplotlib.ui import EdgeWindow |
| 16 | +from imgui_bundle import imgui |
| 17 | + |
| 18 | + |
| 19 | +# initial frequency coefficients for sine and cosine functions |
| 20 | +P = 1 |
| 21 | +Q = 1 |
| 22 | + |
| 23 | + |
| 24 | +# helper function to make a circle |
| 25 | +def make_circle(center, radius: float, p, q, n_points: int) -> np.ndarray: |
| 26 | + theta = np.linspace(0, 2 * np.pi, n_points) |
| 27 | + xs = radius * np.cos(theta * p) |
| 28 | + ys = radius * np.sin(theta * q) |
| 29 | + |
| 30 | + return np.column_stack([xs, ys]) + center |
| 31 | + |
| 32 | + |
| 33 | +# we can define this layout using "extents", i.e. min and max ranges on the canvas |
| 34 | +# (x_min, x_max, y_min, y_max) |
| 35 | +# extents can be defined as fractions as shown here |
| 36 | +extents = [ |
| 37 | + (0, 0.5, 0, 1), # circle subplot |
| 38 | + (0.5, 1, 0, 0.5), # sine subplot |
| 39 | + (0.5, 1, 0.5, 1), # cosine subplot |
| 40 | +] |
| 41 | + |
| 42 | +# create a figure with 3 subplots |
| 43 | +figure = fpl.Figure( |
| 44 | + extents=extents, |
| 45 | + names=["circle", "sin", "cos"], |
| 46 | + size=(700, 560) |
| 47 | +) |
| 48 | + |
| 49 | +# set more descriptive figure titles |
| 50 | +figure["circle"].title = "sin(x*p) over cos(x*q)" |
| 51 | +figure["sin"].title = "sin(x * p)" |
| 52 | +figure["cos"].title = "cos(x * q)" |
| 53 | + |
| 54 | +# set the axes to intersect at (0, 0, 0) to better illustrate the unit circle |
| 55 | +for subplot in figure: |
| 56 | + subplot.axes.intersection = (0, 0, 0) |
| 57 | + subplot.toolbar = False # reduce clutter |
| 58 | + |
| 59 | +figure["sin"].camera.maintain_aspect = False |
| 60 | +figure["cos"].camera.maintain_aspect = False |
| 61 | + |
| 62 | +# create sine and cosine data |
| 63 | +xs = np.linspace(0, 2 * np.pi, 360) |
| 64 | +sine = np.sin(xs * P) |
| 65 | +cosine = np.cos(xs * Q) |
| 66 | + |
| 67 | +# circle data |
| 68 | +circle_data = make_circle(center=(0, 0), p=P, q=Q, radius=1, n_points=360) |
| 69 | + |
| 70 | +# make the circle line graphic, set the cmap transform using the sine function |
| 71 | +circle_graphic = figure["circle"].add_line( |
| 72 | + circle_data, thickness=4, cmap="bwr", cmap_transform=sine |
| 73 | +) |
| 74 | + |
| 75 | +# line to show the circle radius |
| 76 | +# use it to indicate the current position of the sine and cosine selctors (below) |
| 77 | +radius_data = np.array([[0, 0, 0], [*circle_data[0], 0]]) |
| 78 | +circle_radius_graphic = figure["circle"].add_line( |
| 79 | + radius_data, thickness=6, colors="magenta" |
| 80 | +) |
| 81 | + |
| 82 | +# sine line graphic, cmap transform set from the sine function |
| 83 | +sine_graphic = figure["sin"].add_line( |
| 84 | + sine, thickness=10, cmap="bwr", cmap_transform=sine |
| 85 | +) |
| 86 | + |
| 87 | +# cosine line graphic, cmap transform set from the sine function |
| 88 | +# illustrates the sine function values on the cosine graphic |
| 89 | +cosine_graphic = figure["cos"].add_line( |
| 90 | + cosine, thickness=10, cmap="bwr", cmap_transform=sine |
| 91 | +) |
| 92 | + |
| 93 | +# add linear selectors to the sine and cosine line graphics |
| 94 | +sine_selector = sine_graphic.add_linear_selector() |
| 95 | +cosine_selector = cosine_graphic.add_linear_selector() |
| 96 | + |
| 97 | + |
| 98 | +def set_circle_cmap(ev): |
| 99 | + # sets the cmap transforms |
| 100 | + |
| 101 | + cmap_transform = ev.graphic.data[:, 1] # y-val data of the sine or cosine graphic |
| 102 | + for g in [sine_graphic, cosine_graphic]: |
| 103 | + g.cmap.transform = cmap_transform |
| 104 | + |
| 105 | + # set circle cmap transform |
| 106 | + circle_graphic.cmap.transform = cmap_transform |
| 107 | + |
| 108 | +# when the sine or cosine graphic is clicked, the cmap_transform |
| 109 | +# of the sine, cosine and circle line graphics are all set from |
| 110 | +# the y-values of the clicked line |
| 111 | +sine_graphic.add_event_handler(set_circle_cmap, "click") |
| 112 | +cosine_graphic.add_event_handler(set_circle_cmap, "click") |
| 113 | + |
| 114 | + |
| 115 | +def set_x_val(ev): |
| 116 | + # used to sync the two selectors |
| 117 | + value = ev.info["value"] |
| 118 | + index = ev.get_selected_index() |
| 119 | + |
| 120 | + sine_selector.selection = value |
| 121 | + cosine_selector.selection = value |
| 122 | + |
| 123 | + circle_radius_graphic.data[1, :-1] = circle_data[index] |
| 124 | + |
| 125 | +# add same event handler to both graphics |
| 126 | +sine_selector.add_event_handler(set_x_val, "selection") |
| 127 | +cosine_selector.add_event_handler(set_x_val, "selection") |
| 128 | + |
| 129 | +# initial selection value |
| 130 | +sine_selector.selection = 50 |
| 131 | + |
| 132 | + |
| 133 | +class GUIWindow(EdgeWindow): |
| 134 | + def __init__(self, figure, size, location, title): |
| 135 | + super().__init__(figure=figure, size=size, location=location, title=title) |
| 136 | + |
| 137 | + self._p = 1 |
| 138 | + self._q = 1 |
| 139 | + |
| 140 | + def _set_data(self): |
| 141 | + global sine_graphic, cosine_graphic, circle_graphic, circle_radius_graphic, circle_data |
| 142 | + |
| 143 | + # make new data |
| 144 | + sine = np.sin(xs * self._p) |
| 145 | + cosine = np.cos(xs * self._q) |
| 146 | + circle_data = make_circle(center=(0, 0), p=self._p, q=self._q, radius=1, n_points=360) |
| 147 | + |
| 148 | + |
| 149 | + # set the graphics |
| 150 | + sine_graphic.data[:, 1] = sine |
| 151 | + cosine_graphic.data[:, 1] = cosine |
| 152 | + circle_graphic.data[:, :2] = circle_data |
| 153 | + circle_radius_graphic.data[1, :-1] = circle_data[sine_selector.get_selected_index()] |
| 154 | + |
| 155 | + def update(self): |
| 156 | + flag_set_data = False |
| 157 | + |
| 158 | + changed, self._p = imgui.input_int("P", v=self._p, step_fast=2) |
| 159 | + if changed: |
| 160 | + flag_set_data = True |
| 161 | + |
| 162 | + changed, self._q = imgui.input_int("Q", v=self._q, step_fast=2) |
| 163 | + if changed: |
| 164 | + flag_set_data = True |
| 165 | + |
| 166 | + if flag_set_data: |
| 167 | + self._set_data() |
| 168 | + |
| 169 | + |
| 170 | +gui = GUIWindow( |
| 171 | + figure=figure, |
| 172 | + size=100, |
| 173 | + location="right", |
| 174 | + title="Freq. coeffs" |
| 175 | +) |
| 176 | + |
| 177 | +figure.add_gui(gui) |
| 178 | + |
| 179 | +figure.show() |
| 180 | + |
| 181 | + |
| 182 | +# NOTE: `if __name__ == "__main__"` is NOT how to use fastplotlib interactively |
| 183 | +# please see our docs for using fastplotlib interactively in ipython and jupyter |
| 184 | +if __name__ == "__main__": |
| 185 | + print(__doc__) |
| 186 | + fpl.loop.run() |
0 commit comments