Xah Talk Show 2025-09-10 Ep701 Wolfram Language Coding Session, Export Graphics to SVG

xah talk show ep701
xah talk show ep701

Video Summary (AI Generated. Reviewed by Human)

  • This video is a coding session in Wolfram Language, focusing on writing a custom function to export 2D graphics to SVG (Scalable Vector Graphics).
  • The main problem the video aims to solve is that Wolfram Language's built-in SVG export generates unnecessarily large file sizes (5:30), often using complex Bezier curves to approximate simple shapes like circles (9:00), instead of using efficient SVG primitive tags.
  • The core challenge discussed is how to programmatically find the minimum and maximum X and Y coordinates (the "bounds") of a given 2D graphics object in Wolfram Language (16:34).
  • The creator explores several Wolfram Language functions like RegionBounds, BoundingRegion, and BoundaryDiscretizeGraphics to achieve this.
  • Towards the end of the video, the creator encounters an unexpected issue where BoundaryDiscretizeGraphics seems to fail when dealing with graphics containing two circles whose radii or positions cause them to overlap or be sufficiently large/far apart (1:06:09).
  • The creator suspects this might be a bug in Wolfram Language (1:10:06).
(*
Write a command to export 2d graphics to svg.
We want to do this because the bultin Export function creates huge file size,
that is almost 10 times larger than necessary.
 *)

(* sample simple 2d graphics of just a circle *)
Graphics[ Circle[{0,0}, 1] ]

(* this is builtin export to svg *)
Export["xxcircle.svg" , Graphics[ Circle[{0,0}, 1] ]]

(* HHHH------------------------------ *)

Clear[graphicsToString]

graphicsToString::usage = "
graphicsToString[g]
return a string, that is a SVG format, representing Graphics object g.";

graphicsToString[ gra_Graphics ] := "
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<svg xmlns=\"http://www.w3.org/2000/svg\"
xmlns:xlink=\"http://www.w3.org/1999/xlink\"
width=\"1000\" height=\"1000\"
viewBox=\"0 0 1000 1000 \" version=\"1.1\">
</svg>"

(* First job, is to find the max and min of x coord, and y coord, in the graphics. *)

xprims = { Circle[{0,0}, 1] , Circle[{650, 945}, 1126] }
xgraphics = Graphics[ xprims ]
xboundary = BoundaryDiscretizeGraphics[ xgraphics ]
xregionBounds = RegionBounds[ xboundary ]
(* seems like we found a bug. need to look into more. *)

(* the bug is this. when a circle's radius differ by just 5,
BoundaryDiscretizeGraphics doesn't work *)

(* this works *)
xprims = { Circle[{0,0}, 1] , Circle[{6, 9}, 6] }
xgraphics = Graphics[ xprims ]
xboundary = BoundaryDiscretizeGraphics[ xgraphics ]
xregionBounds = RegionBounds[ xboundary ]

(* this does not work *)
xprims = { Circle[{0,0}, 1] , Circle[{6, 9}, 11] }
xgraphics = Graphics[ xprims ]
xboundary = BoundaryDiscretizeGraphics[ xgraphics ]
xregionBounds = RegionBounds[ xboundary ]
xah talk show ep701 3179f
xah talk show ep701 3179f
xah talk show ep701 317e0
xah talk show ep701 317e0
xah talk show ep701 318c5
xah talk show ep701 318c5
xah talk show ep701 318ce
xah talk show ep701 318ce

Wolfram language coding, export to SVG