Skip to content

Instantly share code, notes, and snippets.

@aduffey
Created December 23, 2023 03:47
Show Gist options
  • Save aduffey/d098132676fb566a0d766aefb4199efb to your computer and use it in GitHub Desktop.
Save aduffey/d098132676fb566a0d766aefb4199efb to your computer and use it in GitHub Desktop.
CRT simulation work
from imageio.v3 import imwrite
import numpy as np
def cos_isotropic_spot(x, y, s):
"""Takes scalar x, scalar y, and vector s"""
return sqrt(s) * (0.5 + 0.5 * np.cos(np.pi * np.clip(np.divide(np.sqrt(x**2 + y**2), sqrt(s), out=np.zeros_like(s), where=horiz!=0), -1, 1)))
def line(x, y, s):
"""Takes scalar x, scalar y, and vector s"""
np.trapz(cos_isotropic_spot(x, y, s),
def linear_to_srgb(img):
"""Linear float to sRGB uint8"""
out = np.where(img < 0.0031308, img * 12.92, 1.055 * (np.power(img, (1.0 / 2.4))) - 0.055)
out = (img * 255).astype(np.uint8)
return out
def main():
xaxis = np.linspace(0, 320, 3200)
yaxis = np.linspace(-2, 2, 40)
taxis = np.linspace(0, 320, 3200)
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
from imageio.v3 import imwrite
import numpy as np
def cos_sep_spot(x, y):
return (0.5 + 0.5 * np.cos(np.pi * np.clip(x, -1, 1))) * (0.5 + 0.5 * np.cos(np.pi * np.clip(y, -1, 1)))
def cos_sep_2_spot(x, y):
horiz = 0.5 + 0.5 * np.cos(np.pi * np.clip(x, -1, 1))
return horiz * (0.5 + 0.5 * np.cos(np.pi * np.clip(np.divide(y, np.power(horiz, 1/5), out=np.zeros((800, 800)), where=horiz!=0), -1, 1)))
def cos_isotropic_spot(x, y):
return (0.5 + 0.5 * np.cos(np.pi * np.clip(np.sqrt(x**2 + y**2), -1, 1)))
def gauss_spot(x, y):
c = 1 / (2 * np.sqrt(2 * np.log(2)))
#return 1 / (np.sqrt(2 * np.pi) * c) * np.exp(-x**2 / (2 * c**2)) * np.exp(-y**2 / (2 * c**2))
return np.exp(-x**2 / (2 * c**2)) * np.exp(-y**2 / (2 * c**2))
def linear_to_srgb(img):
"""Linear float to sRGB uint8"""
out = np.where(img < 0.0031308, img * 12.92, 1.055 * (np.power(img, (1.0 / 2.4))) - 0.055)
out = (img * 255).astype(np.uint8)
return out
def main():
xaxis = np.linspace(-2, 2, 800)
yaxis = np.linspace(-2, 2, 800)
gauss_result = gauss_spot(xaxis[:,None], yaxis[None,:])
print(gauss_result[400, 400], gauss_result[500, 400])
imwrite("gauss_spot.png", linear_to_srgb(gauss_result))
cos_sep_result = cos_sep_spot(xaxis[:,None], yaxis[None,:])
print(cos_sep_result[400, 400], cos_sep_result[500, 400])
imwrite("cos_sep_spot.png", linear_to_srgb(cos_sep_result))
cos_sep_2_result = cos_sep_2_spot(xaxis[:,None], yaxis[None,:])
print(cos_sep_2_result[400, 400], cos_sep_2_result[500, 400])
imwrite("cos_sep_2_spot.png", linear_to_srgb(cos_sep_2_result))
cos_isotropic_result = cos_isotropic_spot(xaxis[:,None], yaxis[None,:])
print(cos_isotropic_result[400, 400], cos_isotropic_result[500, 400])
imwrite("cos_isotropic_spot.png", linear_to_srgb(cos_isotropic_result))
diff = np.abs(cos_isotropic_result - cos_sep_2_result) * 10
imwrite("cos_diff.png", linear_to_srgb(diff))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment