Skip to content
Projects

Triangle

Download notebookView on GitHubRun when the site was built.

A triangle from three vertices, in either orientation.

Triangle(vertices) has an exact level set, the signed distance to its edges, and an exact outline: its three corners, counter-clockwise.

import matplotlib.pyplot as plt
import numpy as np
from simgeometry import Grid, Triangle
wedge = Triangle([(0.0, 0.0), (1.0, 0.0), (0.3, 0.8)])
print("centroid inside:", wedge.contains((0.43, 0.27)))
print("distance from (0.5, -0.2):", wedge.signed_distance((0.5, -0.2)))
assert np.isclose(wedge.signed_distance((0.5, -0.2)), 0.2)
centroid inside: True
distance from (0.5, -0.2): 0.2

The grid nodes inside, the outline loop and the outward normals at points on the boundary from surface_normal.

grid = Grid(2, (-0.1, -0.1), (1.1, 0.9), (25, 21))
mask = wedge.node_mask(grid).ravel()
nodes = grid.node_points()
(loop,) = wedge.outlines()
panels = wedge.boundary_panels(max_spacing=0.15)
normals = wedge.surface_normal(panels.midpoint)
fig, ax = plt.subplots(figsize=(5.5, 4.5), layout="constrained")
ax.scatter(*nodes[:, ~mask], s=6, color="0.75")
ax.scatter(*nodes[:, mask], s=8, color="tab:blue", label="nodes inside")
ax.plot(*np.c_[loop, loop[:, :1]], color="k")
ax.quiver(
*panels.midpoint,
*normals,
color="tab:red",
scale=12,
width=0.005,
label="outward normals",
)
ax.set(xlabel="x", ylabel="y", title="A Triangle on a grid")
ax.set_aspect("equal")
ax.legend(loc="upper right")
plt.show()
Notebook output