RE: Color Fill areas in 2D graphic

*To*: mathgroup at smc.vnet.net*Subject*: [mg23387] RE: [mg23354] Color Fill areas in 2D graphic*From*: "David Park" <djmp at earthlink.net>*Date*: Fri, 5 May 2000 02:07:22 -0400 (EDT)*Sender*: owner-wri-mathgroup at wolfram.com

> > Hi, > I want combine several ParametricPlot Images. After this is done, how > can I fill areas within the image with a specific color ? > thanks > Roland Pabel > Hi Roland, If you wish to try out my DrawingPaper routines, which are available at my web site below, then the attached Mathematica notebook, ColoredArea.nb, shows how to fill in areas with color. (MathGroup readers may contact me for a copy of the notebook.) If you have a closed curve defined by a parametric expression, then all you have to do is convert the Line primitive to a Polygon primitive and supply a color. If the area is defined by the combination of several parametric arcs, then DrawingPaper has a routine, StitchLineSegments which will stitch the arcs together into one line which can then be converted to a Polygon. Of course, the standard way to color the area between two curves is to use FilledPlot, but this requires the first curve to always be above the second curve. Sometimes this does not happen in the x-y plane but does happen in a different coordinate system. In those cases the DrawingPaper routine DrawingTransform can convert a FilledPlot back to x-y coordinates. For example, the area outside a unit circle but inside a cardioid can be filled with FilledPlot in a theta-r representation and then transformed to a x-y representation. The notebook has examples of coloring in a tilted ellipse, coloring in the area bounded by two intersecting parabolas with their axes in the y and -y direction, and the circle-cardioid example mentioned above. The packages you will need for the notebook from my web site are DrawingPaper.m and FilledDrawing.m. The packages should be put in the AddOns/ExtraPackages/Graphics folder. There are also other Drawing packages and a tutorial. David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ (*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 4.0, MathReader 4.0, or any compatible application. The data for the notebook starts with the line containing stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info at wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. ***********************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 8299, 242]*) (*NotebookOutlinePosition[ 8952, 265]*) (* CellTagsIndexPosition[ 8908, 261]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Filling Areas with Color", "Title"], Cell["\<\ David Park djmp at earthlink.net http://home.earthlink.net/~djmp/\ \>", "Subtitle"], Cell[TextData[{ "The Graphics`DrawingPaper` packages can be downloaded from my web site. It \ should be placed in the AddOns/ExtraPackages/Graphics directory. It can then \ be used just like the standard packages. Here is a click-on link to the web \ site:\n", ButtonBox["http://home.earthlink.net/~djmp/ ", ButtonData:>{ URL[ "http://home.earthlink.net/~djmp/ "], None}, ButtonStyle->"Hyperlink"], " " }], "Text"], Cell[BoxData[{ \(Needs["\<Graphics`DrawingPaper`\>"]\), "\[IndentingNewLine]", \(Needs["\<Graphics`FilledDrawing`\>"]\), "\[IndentingNewLine]", \(Needs["\<Geometry`Rotations`\>"]\)}], "Input"], Cell[CellGroupData[{ Cell["For a closed curve", "Section"], Cell["\<\ This is the parametric representation of an ellipse with 7:3 axes ratio and \ rotated an an angle \[Theta].\ \>", "Text"], Cell[BoxData[ \(ellipse[\[Theta]_, t_] := RotationMatrix2D[\(-\[Theta]\)] . {7 Cos[t], 3 Sin[t]}\)], "Input"], Cell["\<\ We set the color to be LightBlue (DrawingPaper automatically loads in \ Graphics`Colors`), parametrically draw the curve, save it, convert the Line \ to a Polygon, change the color to Black and display the curve.\ \>", "Text"], Cell[BoxData[ \(\(Show[ Graphics[\n\t\t{LightBlue, \((curve ParametricDraw[ Evaluate[ellipse[\[Pi]/4, t]], {t, 0, 2 \[Pi]}])\) /. Line \[Rule] Polygon, Black, curve}], \n\n AspectRatio \[Rule] Automatic, PlotRange \[Rule] {{\(-10\), 10}, {\(-10\), 10}}, Background \[Rule] Linen, \n{}];\)\)], "Input", GeneratedCell->False] }, Closed]], Cell[CellGroupData[{ Cell["For a region defined by several arcs", "Section"], Cell["\<\ Here are two parabolas with their axes in the y and -y directions. (If they \ were in the x direction we could use FilledPlot or FilledDraw.)\ \>", "Text"], Cell[BoxData[{ \(\(par1[t_] := {\(-3\) + t\^2, t};\)\), "\[IndentingNewLine]", \(par2[t_] := {3 - t\^2, t}\)}], "Input"], Cell["This finds their points of intersection.", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(Solve[par1[t] \[Equal] par2[t]]\)], "Input"], Cell[BoxData[ \({{t \[Rule] \(-\ at 3\)}, {t \[Rule] \ at 3}}\)], "Output"] }, Open ]], Cell["This plots the two curves to show the area we wish to color.", "Text"], Cell[BoxData[ \(\(Show[ Graphics[\n\t\t{ParametricDraw[ Evaluate[{par1[t], par2[t]}], {t, \(-\ at 3\), \ at 3}]}], \n\n AspectRatio \[Rule] Automatic, PlotRange \[Rule] {{\(-10\), 10}, {\(-10\), 10}}/2, Background \[Rule] Linen, \n{}];\)\)], "Input", GeneratedCell->False], Cell["\<\ We want to define a curve by stitching the two arc segments together. Then \ all we have to do is convert the Line to a Polygon. DrawingPaper contains a \ routine for stitching together arcs. The only thing we have to be careful \ about is stitching them end to end in the correct order. Since the plotting \ iterators always run from the minimum value to the maximum value (no matter \ what order we put them), we will often have to reverse the order of the \ points in some of the line segments.\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(\(?StitchLineSegments\)\)], "Input"], Cell[BoxData[ \("StitchLineSegments[segs_List] will stitch together a list of line \ segments into one line segment. One must be careful with the order of the \ segments and the order of the points within each segment. The segs list is \ Flattened as the first step."\)], "Print"] }, Open ]], Cell["\<\ So here we assemble our curve by drawing the two line segments separately and \ stitching them together.\ \>", "Text"], Cell[BoxData[ \(\(curve StitchLineSegments[{ParametricDraw[ par1[t] // Evaluate, {t, \(-\ at 3\), \ at 3}], ParametricDraw[par2[t] // Evaluate, {t, \(-\ at 3\), \ at 3}] /. Line[a_] \[RuleDelayed] Line[Reverse[a]]}];\)\)], "Input"], Cell["\<\ Now it is a simple matter to draw the colored area and outline it in Black.\ \>", "Text"], Cell[BoxData[ \(\(Show[ Graphics[\n\t\t{LightBlue, curve /. Line \[Rule] Polygon, Black, curve}], \n\nAspectRatio \[Rule] Automatic, PlotRange \[Rule] {{\(-10\), 10}, {\(-10\), 10}}/2, Background \[Rule] Linen, \n{}];\)\)], "Input", GeneratedCell->False] }, Closed]], Cell[CellGroupData[{ Cell["\<\ For a region where FilledPlot can be used in a different coordinate system\ \>", "Section"], Cell["\<\ Sometimes we can use a different coordinate system where FilledPlot will work \ for the filling of the areas between two curve. For example, to color the \ area inside of a cardioid, but outside a unit circle, we can do a FilledDraw \ in the \[Theta]-r plane. Then we can use the DrawingTransform routine from \ DrawingPaper to transform to an x-y representation.\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(\(?DrawingTransform\)\)], "Input"], Cell[BoxData[ \("DrawingTransform[f1, f2], where f1 and f2 are function names, is a \ rule which applies the following transformation to all points in the graphics \ object: {x_?NumberQ, y_?NumberQ} \[Rule] {f1[x, y], f2[x, y]}."\)], "Print"] }, Open ]], Cell[TextData[{ "This shows how ", Cell[BoxData[ \(DrawingTransform\)]], " can often be used to simplify the filling of areas. We make an \[Theta]-r \ plot and then transform to rectangular coordinates. Don't mix up the order of \ \[Theta] and r! DrawPolarR is the DrawingPaper version of PolarPlot and is \ used to outline the regions." }], "Text"], Cell[BoxData[ \(\(Show[ Graphics[\n\t\t{FilledDraw[{1, 1 + Cos[\[Theta]]}, {\[Theta], \(-\[Pi]\)/2, \[Pi]/2}, Fills \[Rule] LightSteelBlue, \ Curves \[Rule] None, PlotPoints \[Rule] 31] /. DrawingTransform[Function[{\[Theta], r}, r\ Cos[\[Theta]]], Function[{\[Theta], r}, r\ Sin[\[Theta]]]], \n\t\tDrawPolarR[ 1 + Cos[\[Theta]], {\[Theta], 0, 2 \[Pi]}, PlotPoints \[Rule] 31], \n\t\tDrawPolarR[ 1, {\[Theta], 0, 2 \[Pi]}]}], \n\n AspectRatio \[Rule] Automatic, PlotRange \[Rule] {{\(-1.501\), 2.5}, {\(-2\), 2}}, Background \[Rule] Linen, \n{Frame \[Rule] True, FrameLabel \[Rule] {x, y}}];\)\)], "Input"] }, Closed]] }, Open ]] }, FrontEndVersion->"4.0 for Microsoft Windows", ScreenRectangle->{{0, 1280}, {0, 943}}, WindowSize->{660, 763}, WindowMargins->{{0, Automatic}, {Automatic, 0}} ] (*********************************************************************** Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. ***********************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1739, 51, 41, 0, 115, "Title"], Cell[1783, 53, 90, 4, 122, "Subtitle"], Cell[1876, 59, 434, 10, 71, "Text"], Cell[2313, 71, 206, 3, 70, "Input"], Cell[CellGroupData[{ Cell[2544, 78, 37, 0, 59, "Section"], Cell[2584, 80, 131, 3, 33, "Text"], Cell[2718, 85, 122, 2, 30, "Input"], Cell[2843, 89, 236, 4, 52, "Text"], Cell[3082, 95, 420, 9, 150, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[3539, 109, 55, 0, 39, "Section"], Cell[3597, 111, 165, 3, 52, "Text"], Cell[3765, 116, 129, 2, 52, "Input"], Cell[3897, 120, 56, 0, 33, "Text"], Cell[CellGroupData[{ Cell[3978, 124, 64, 1, 30, "Input"], Cell[4045, 127, 73, 1, 32, "Output"] }, Open ]], Cell[4133, 131, 76, 0, 33, "Text"], Cell[4212, 133, 320, 7, 135, "Input"], Cell[4535, 142, 521, 8, 109, "Text"], Cell[CellGroupData[{ Cell[5081, 154, 56, 1, 30, "Input"], Cell[5140, 157, 284, 4, 82, "Print"] }, Open ]], Cell[5439, 164, 128, 3, 33, "Text"], Cell[5570, 169, 276, 5, 77, "Input"], Cell[5849, 176, 99, 2, 33, "Text"], Cell[5951, 180, 299, 6, 130, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[6287, 191, 101, 2, 39, "Section"], Cell[6391, 195, 388, 6, 71, "Text"], Cell[CellGroupData[{ Cell[6804, 205, 54, 1, 30, "Input"], Cell[6861, 208, 246, 3, 63, "Print"] }, Open ]], Cell[7122, 214, 361, 8, 71, "Text"], Cell[7486, 224, 785, 14, 190, "Input"] }, Closed]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)