Xah Talk Show 2025-12-04 Ep718 Wolfram Language, Custom Function, 2d Graphics to SVG. Part 2

xah talk show ep718 2ef30
xah talk show ep718 2ef30

Video Summary (Generated by AI, Edited by Human.)

This video is the second part of a series focusing on writing a custom Wolfram Language function to convert Wolfram Language graphics to SVG.

Here's a summary of what's covered:


The video discusses different "centers" of a triangle, which are specific points within or related to a triangle. The speaker mentions the following:


The video discusses the concept and history of notebooks, particularly in the context of the Wolfram Language.




xah talk show ep718 2394c
xah talk show ep718 2394c
xah talk show ep718 252ee
xah talk show ep718 252ee
xah talk show ep718 25301
xah talk show ep718 25301
xah talk show ep718 273d8
xah talk show ep718 273d8
xah talk show ep718 25310
xah talk show ep718 25310
xah talk show ep718 25e6c
xah talk show ep718 25e6c

Graphics[Circle[{0, 0}, 1]]

Graphics[Circle[{0, 0}, 1], Axes -> True]

Graphics[
{
Circle[{0, 0}, 1],
Line[{{0,0},{2,1}}]
} ,
 Axes -> True]
StringJoin["some" , "thing"]
(* something *)

"some" <> "thing"
(* something *)
(*
2 days ago, we wrote this function that convert circle to svg.
today, we gonna modify the function, so it does lines too.
and, we gonna make it actually export to a file.
 *)

Clear[xGraphicsToSVGString]

xGraphicsToSVGString::usage = "
xGraphicsToSVGString[xGraphics]
return a string that is svg representation of xGraphics.
xGraphics is any expression with head Graphics.
";

xGraphicsToSVGString[ Graphics[gra_] ] :=
"<svg width=\"100\" height=\"100\">" <>
(StringJoin @
ReplaceAll[
gra,
{
Circle[ {x_, y_}, r_] :>
StringJoin["<circle cx=\"",
ToString[x],
"\" cy=\"",
ToString[y],
"\" r=\"",
ToString[r],
"\" style=\"fill:none; stroke:blue; stroke-width:5\" />"
],
(* <line x1="0" y1="0" x2="50" y2="10" style="stroke:black" /> *)
Line[ {{a_, b_}, {c_, d_}}] :>
StringJoin["<line x1=\"",
ToString[a],
"\" y1=\"",
ToString[b],
"\" x2=\"",
ToString[c],
"\" y2=\"",
ToString[d],
"\" style=\"stroke:black; stroke-width:5\" />"
]
}

]) <>
"</svg>"

Clear[xGraphicsToSVGFile]

xGraphicsToSVGFile::usage = "
xGraphicsToSVGFile[xGraphics, xFilepath]
Export xGraphics to SVG string and save it at xFilepath.
xGraphics is any expression with head Graphics.
";

xGraphicsToSVGFile[ xexpr_, xfilepath_ ] :=
(
WriteString[ xfilepath, xGraphicsToSVGString[xexpr]];
Close[ xfilepath ]
)

(* xGraphicsToSVGFile[ xexpr_, xfilepath_ ] := *)
(* CompoundExpression[WriteString[ xfilepath, xGraphicsToSVGString[xexpr]] *)
(* , *)
(* Close[ xfilepath ] *)
(* ] *)

(* s------------------------------ *)

xGraphicsToSVGFile[
Graphics[
{
Circle[{0, 0}, 1] , Circle[{0, 2}, 1],
Line[{{0, 0}, {2, 1}}]
}
],
"my-fantastic-function.svg"
 ]

(* success.
next to do.
need to center the origin.
need to reverse the y axes.
need to rescale the coordinate, i.e. svg viewBox.
 *)

Wolfram Language Graphics to SVG