Skip to content

Commit

Permalink
add light.typ code + assets and list in readme
Browse files Browse the repository at this point in the history
a Typst file for visualizing linearly polarized electromagnetic wave
also deduplicate Typst and Latex code files before to avoid repetition in update-readme-table.py
  • Loading branch information
janosh committed Dec 31, 2024
1 parent 0d33951 commit da6833a
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 56 deletions.
Binary file added assets/light/light-hd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/light/light.pdf
Binary file not shown.
Binary file added assets/light/light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 55 additions & 0 deletions assets/light/light.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

// adapted from https://github.com/cetz-package/cetz/blob/a082e02a/gallery/waves.typ
#import "@preview/cetz:0.3.1": canvas, draw, vector, matrix

#set page(width: auto, height: auto, margin: .5cm)

#canvas({
import draw: *

// Set up the transformation matrix
set-transform(matrix.transform-rotate-dir((1, 1, -1.3), (0, 1, .4)))
scale(x: 1.5, z: -1)
let arrow-style = (mark: (end: "stealth", fill: black, scale: 0.5))
// Coordinate axes labels and arrows
draw.line((0, -2, 0), (0, 2.5, 0), ..arrow-style)
draw.line((-0.5, 0, 0), (8.5, 0, 0), ..arrow-style)
draw.line((0, 0, -1.5), (0, 0, 2), ..arrow-style)
content((0, 0, 2.3), [$arrow(E)$])
content((0, 3, 0), [$arrow(B)$])
content((8.7, 0, 0), [$arrow(v)$])

grid((0, -2), (8, 2), stroke: gray + .5pt)

// Draw a sine wave on the xy plane
let wave(amplitude: 1, fill: none, phases: 2, scale: 8, samples: 100) = {
line(
..(
for x in range(0, samples + 1) {
let x = x / samples
let p = (2 * phases * calc.pi) * x
((x * scale, calc.sin(p) * amplitude),)
}
),
fill: fill,
)

let subdivs = 8
for phase in range(0, phases) {
let x = phase / phases
for div in range(1, subdivs + 1) {
let p = 2 * calc.pi * (div / subdivs)
let y = calc.sin(p) * amplitude
let x = x * scale + div / subdivs * scale / phases
line((x, 0), (x, y), stroke: rgb(0, 0, 0, 150) + .5pt)
}
}
}

// Draw waves
group({
rotate(x: 90deg)
wave(amplitude: 1.6, fill: rgb(0, 0, 255, 50))
})
wave(amplitude: 1, fill: rgb(255, 0, 0, 50))
})
10 changes: 10 additions & 0 deletions assets/light/light.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
title: Electromagnetic Wave
tags:
- physics
- electromagnetism
- waves
- optics
- electromagnetic radiation
- Maxwell's equations
description: |
Visualization of a linearly polarized electromagnetic wave propagating in the z-direction, showing the orthogonal electric (E) and magnetic (B) field components oscillating in phase. The electric field (blue) and magnetic field (red) are perpendicular to each other and to the direction of propagation, forming a transverse wave as described by Maxwell's equations. This is a fundamental representation of electromagnetic radiation, which includes visible light, radio waves, X-rays, and other forms of electromagnetic waves that travel at the speed of light in vacuum.
104 changes: 52 additions & 52 deletions readme.md

Large diffs are not rendered by default.

20 changes: 16 additions & 4 deletions scripts/update-readme-table.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,24 @@
with open(f"{ROOT}/site/package.json", "r") as file:
site_url = json.load(file)["homepage"]

tex_paths = sorted(glob(f"{TEX_DIR}/**/*.tex") + glob(f"{TEX_DIR}/**/*.typ"))
# Get all .tex and .typ paths
tex_paths = glob(f"{TEX_DIR}/**/*.tex")
typ_paths = glob(f"{TEX_DIR}/**/*.typ")

# Create a dict mapping directory names to file paths
# Prefer .typ files over .tex files when both exist
path_dict = {}
for path in sorted(tex_paths + typ_paths):
dir_name = os.path.basename(os.path.dirname(path))
if dir_name not in path_dict or path.endswith(".typ"):
path_dict[dir_name] = path

# Convert back to sorted list
unique_paths = sorted(path_dict.values())

md_table = f"| {' ' * 22} | {' ' * 22} |\n| :---: | :---: |\n"

for path1, path2 in zip_longest(tex_paths[::2], tex_paths[1::2]):
for path1, path2 in zip_longest(unique_paths[::2], unique_paths[1::2]):
dir1, dir2 = map(os.path.dirname, (path1, path2 or ""))
fig1, fig2 = map(os.path.basename, (dir1, dir2))

Expand All @@ -31,7 +44,6 @@
img_link2 = f"![`{fig2}.png`](assets/{fig2}/{fig2}.png)" if path2 else ""
md_table += f"| {img_link1} | {img_link2} |\n"


with open(f"{ROOT}/README.md", "r") as file:
readme = file.read()

Expand All @@ -44,7 +56,7 @@
)

# update count in "Collection of **110** "
readme = re.sub(r"(?<=Collection of \*\*)\d+(?=\*\* )", str(len(tex_paths)), readme)
readme = re.sub(r"(?<=Collection of \*\*)\d+(?=\*\* )", str(len(unique_paths)), readme)

with open(f"{ROOT}/README.md", "w") as file:
file.write(readme)
Expand Down

0 comments on commit da6833a

Please sign in to comment.