Re: Trouble Getting Graphic Primitives in a Module to Display with Show Command
- To: mathgroup at smc.vnet.net
- Subject: [mg125429] Re: Trouble Getting Graphic Primitives in a Module to Display with Show Command
- From: "djmpark" <djmpark at comcast.net>
- Date: Tue, 13 Mar 2012 03:08:37 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <17979570.11446.1331544406716.JavaMail.root@m06>
The problem is that the behavior of Mathematica with respect to the display of Graphics has changed since Nancy wrote her book. The display of graphics used to be a side effect and the notebook output was uninteresting and so was suppressed with a ";". Now the display is the directly generated output and you no longer want to suppress it. So just use (without the terminating semicolon): sketchEarthOrbit[] With the Show statement you have to use a lot of what I call "Graphics level jumping". And what if you want to include a curve produced by some plot statement? The Presentations Application makes this much easier, natural and more intuitive. Here is the way to produce the elements of the graphic separately and then combine them. I added color for the earth and sun. Notice that we have to draw the orbit first so the earth disk will fall on top of it. <<Presentations` orbit = Circle[{0, 0}, {1., .5}]; earth = {Green, Disk[{-.7, .35}, 0.05], Black, Text["Earth", {-.7, .45}]}; sun = {Yellow, Disk[{.3, 0}, 0.1], Black, Text["Sun", {.3, .17}]}; Draw2D[{orbit, earth, sun}] Draw2D replaces Show and by default uses Automatic AspectRatio. Of course, this plot is wildly incorrect. The sun is nowhere near the focus of the ellipse. Nancy probably took some pains to place the earth on top of the ellipse. And earth's orbit is nowhere near that elliptical. So let's change earth to a comet and use a simple parameterization of the ellipse (s is not the time along the orbit - that's a more complicated calculation), and introduce a function that calculates the position of one focus. ellipse[a_, b_][s_] := {a Cos[s], b Sin[s]} ellipseFocus[a_, b_] := If[a >= b, {Sqrt[a^2 - b^2], 0}, {0, Sqrt[a^2 - b^2]}] Then we draw the elements using the ellipse definitions. ParametricDraw is equivalent to ParametricPlot but just produces the graphics primitives and not an entire plot. Now we can calculate the positions of the comet and sun. orbit = ParametricDraw[ellipse[1, 0.5][s], {s, 0, 2 \[Pi]}]; earth = With[{position = ellipse[1, 0.5][3 \[Pi]/4]}, {Green, Disk[position, 0.04], Black, Text["Comet", position, {0, -3}]}]; sun = With[{position = ellipseFocus[1, 0.5]}, {Yellow, Disk[position, 0.08], Black, Text["Sun", position, {0, -4}]}]; The diagram is produced with the very same statement we used before. Notice that we have combined standard primitives and a plotted curve without having to worry about graphics levels. Draw2D[{orbit, earth, sun}] David Park djmpark at comcast.net http://home.comcast.net/~djmpark/index.html From: Kenneth Bures [mailto:kenbures at gmail.com] I have been trying to display graphics primitives (lines, circles) from inside a module, but I can't get it to work. If you have Nancy Blachman's book, MATHEMATICA: A Practical Approach, 2nd Edition, she gives an example on page 399 of a 4-line module that generates 3 graphics objects and then has a Show command (inside the module) to display them. I've listed the code below. The book illustrates that when you call this module from the main program with the instruction sketchEarthOrbit[] it displays the 3 graphics objects. When I copy this program exactly, it does not work. I get no output. If I cut & paste the exact four lines of code to outside of the module, they do display the desired result, so there seems to be is something wrong with her basic approach. Why doesn't this example work? Actually, what I would really like to do is pass the graphics objects out of the module, and display them from the main program, rather than having the Show inside the module. But I decided that I should understand what is wrong with Blachman's approach before I spend too much time on the other. Any help would be appreciated, either with explaining Blachman's approach and/or how to transfer graphics objects out of a module. Here is Blachman's code (I put the Clear command there): Clear[sketchEarthOrbit]; sketchEarthOrbit[] := Module[{earth, sun, orbit}, earth = Graphics[{ Disk[{-.7, .35}, 0.05], Text["Earth", {-.7, .45}] }]; sun = Graphics[{ Disk[{.3, 0}, 0.1], Text["Sun", {.3, .17}] }]; orbit = Graphics[{ Circle[{0, 0}, {1., .5}] }]; Show[orbit, sun, earth, AspectRatio -> Automatic] ]; sketchEarthOrbit[];
- Follow-Ups:
- RV: Re: Trouble Getting Graphic Primitives in a Module to Display with Show Command
- From: "E. Martín-Serrano" <eMartinSerrano@telefonica.net>
- Re: Trouble Getting Graphic Primitives in a Module to Display with Show Command
- From: "E. Martín-Serrano" <eMartinSerrano@telefonica.net>
- RV: Re: Trouble Getting Graphic Primitives in a Module to Display with Show Command