Skip to content

Commit

Permalink
Add cube
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Feb 3, 2025
1 parent 1896752 commit 4dc7012
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
1 change: 0 additions & 1 deletion mathics/builtin/drawing/graphics3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ def eval_check(self, positions, radius, evaluation: Evaluation):

class Cuboid(Builtin):
"""
<url>:Cuboid:
https://en.wikipedia.org/wiki/Cuboid</url> (<url>
:WMA:https://reference.wolfram.com/language/ref/Cuboid.html</url>)
Expand Down
34 changes: 32 additions & 2 deletions mathics/builtin/drawing/uniform_polyhedra.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,40 @@
# Here we are also hiding "drawing" since this can erroneously appear at the top level.
sort_order = "mathics.builtin.uniform-polyhedra"

uniform_polyhedra_names = "tetrahedron, octahedron, dodecahedron, icosahedron"
uniform_polyhedra_names = "cube, tetrahedron, octahedron, dodecahedron, icosahedron"
uniform_polyhedra_set = frozenset(uniform_polyhedra_names.split(", "))


class Cube(Builtin):
"""
<url>:Cube:
https://en.wikipedia.org/wiki/Cube</url> (<url>:WMA:
https://reference.wolfram.com/language/ref/Cube.html</url>)
<dl>
<dt>'Cube'[]
<dd>represents a regular cube centered at the origin with unit edge length.
<dt>'Cube'[$l$]
<dd>represents a cube centered at the origin with edge length $l$.
<dt>'Cube'[{$x$, $y$, $z$}, ...]
<dd>represents a cube centered at {$x$ $y$, $z$}.
</dl>
>> Graphics3D[Cube[]]
= -Graphics3D-
"""

summary_text = "produce a cube"
rules = {
"Cube[]": """UniformPolyhedron["cube"]""",
"Cube[l_?NumberQ]": """UniformPolyhedron["cube", {{0, 0, 0}}, l]""",
"Cube[positions_List, l_?NumberQ]": """UniformPolyhedron["cube", positions, l]""",
}


class Dodecahedron(Builtin):
"""
Expand All @@ -32,7 +62,7 @@ class Dodecahedron(Builtin):
<dt>'Dodecahedron'[$l$]
<dd>a regular dodecahedron centered at the origin with edge length $l$.
<dt>'Dodecahedron'[{$x$, $y$, $z$}, ...]'
<dt>'Dodecahedron'[{$x$, $y$, $z$}, ...]
<dd>a regular dodecahedron centered at {$x$ $y$, $z$}.
</dl>
Expand Down
7 changes: 1 addition & 6 deletions mathics/format/asy.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@
asy_create_pens,
asy_number,
)
from mathics.format.asy_polyhedra import (
HEDRON_NAME_MAP,
dodecahedron,
tetrahedron,
unimplimented_polygon,
)
from mathics.format.asy_polyhedra import HEDRON_NAME_MAP, unimplimented_polygon

INVERSE_POINT_FACTOR = 1 / DEFAULT_POINT_FACTOR

Expand Down
40 changes: 40 additions & 0 deletions mathics/format/asy_polyhedra.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,45 @@
from typing import Callable, Dict


def cube(center: tuple, length: float, color_str: str) -> str:
"""
Return an asymptote program string to draw a cube at `center`
with length `length`.
"""
return f"""
real length={length};
triple center={center};
real unit_corner = sqrt(3) / 4;
triple[] d;
d[0]=center + length*(unit_corner,unit_corner,unit_corner);
d[1]=center + length*(unit_corner,-unit_corner,unit_corner);
d[2]=center + length*(-unit_corner,-unit_corner,unit_corner);
d[3]=center + length*(-unit_corner,unit_corner,unit_corner);
d[4]=center + length*(-unit_corner,unit_corner,-unit_corner);
d[5]=center + length*(-unit_corner,-unit_corner,-unit_corner);
d[6]=center + length*(unit_corner,-unit_corner,-unit_corner);
d[7]=center + length*(unit_corner,unit_corner,-unit_corner);
path3[] p;
p[0]=d[0]--d[1]--d[2]--d[3]--cycle;
p[1]=d[0]--d[3]--d[4]--d[7]--cycle;
p[2]=d[0]--d[1]--d[6]--d[7]--cycle;
p[3]=d[3]--d[4]--d[5]--d[2]--cycle;
p[4]=d[7]--d[6]--d[5]--d[4]--cycle;
p[5]=d[1]--d[2]--d[5]--d[6]--cycle;
pen sides={color_str};
draw(surface(p[0]),sides);
draw(surface(p[1]),sides);
draw(surface(p[2]),sides);
draw(surface(p[3]),sides);
draw(surface(p[4]),sides);
draw(surface(p[5]),sides);
"""


def dodecahedron(center: tuple, length: float, color_str: str) -> str:
"""
Return an asymptote program string to draw a dodecahedron at `center`
Expand Down Expand Up @@ -150,6 +189,7 @@ def unimplimented_polygon(center: tuple, length: float, color_str: str) -> str:


HEDRON_NAME_MAP: Dict[str, Callable] = {
"cube": cube,
"dodecahedron": dodecahedron,
"octahedron": octahedron,
"tetrahedron": tetrahedron,
Expand Down

0 comments on commit 4dc7012

Please sign in to comment.