Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Hatched shading?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg111208] Re: Hatched shading?
  • From: "David Park" <djmpark at comcast.net>
  • Date: Fri, 23 Jul 2010 07:14:25 -0400 (EDT)

Here is a better method for hatching. The problem with using RegionFunction
is that Mathematica uses even spacing of points and then determines if they
are in the predicate region. This produces poor results unless we use brute
force with many PlotPoints and do extra trimming on the lines.

An alternative method is to define hatching lines so that the y value is
real within the region and imaginary outside the region. The Mathematica
algorithm then uses recursion near the boundary line to obtain better
defined lines. We no longer have to use brute force or trim the lines.

Needs["Presentations`Master`"] 

f[x_] := Sin[x];
hatch[k_][x_] := 
  If[0 < x < 3 \[And] 0 <= (x - k)/3 <= f[3 x], (x - k)/3, I];
Draw2D[
 {(* Draw hatch lines first *)
  Red, AbsoluteThickness[1],
  Table[
   ParametricDraw[{x, hatch[k][x]}, {x, 0, 3},
    PlotPoints -> Automatic,
    MaxRecursion -> Automatic],
   {k, -3, 3.0, .1}],
  
  
  (* Draw the curve without a fill *)
  Blue, AbsoluteThickness[2],
  Draw[f[3 x], {x, 0, 3}, PlotRange -> {0, 1}]
  },
 
 AspectRatio -> .6,
 PlotRange -> {{0, 3}, {0, 1}},
 PlotRangePadding -> {.1, .05},
 Axes -> True,
 ImageSize -> 400] 


David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/  



From: David Park [mailto:djmpark at comcast.net] 

The problem with RegionFunction is how many points does Mathematica use,
especially with a straight line, and how does the algorithm determine where
the boundary should be. Suppose one point is well within the region and the
next point is well without. I'm sure that Mathematica doesn't calculate the
exact intersections with boundaries. So I think the only reliable method is
to use some brute force and trim the lines if they extend outside the
region. Again, here is a Presentations solution where it is convenient to
treat the hatching and the curve separately.

Needs["Presentations`Master`"] 

f[x_] := Sin[x];
Draw2D[
 {(* Draw hatch lines first *)
  Table[
   Draw[(x - k)/3, {x, -1.5, 3},
     RegionFunction -> 
      Function[{x, y}, 0 < x < 3 \[And] 0 <= y <= f[3 x]],
     PlotRange -> {{1, 3}, {0, 1}},
     PlotPoints -> 200,
     PlotStyle -> 
      Directive[AbsoluteThickness[2], Red]] /. {x_?NumberQ, 
      y_?NumberQ} :> 
     If[0 < x < 3 \[And] 0 <= y <= f[3 x], {x, y}, 
      Unevaluated[Sequence[]]], {k, -3, 3, .1}],
  
  (* Draw the curve without a fill *)
  Blue, AbsoluteThickness[2],
  Draw[f[3 x], {x, 0, 3}, PlotRange -> {0, 1}]
  },
 
 AspectRatio -> .6,
 PlotRange -> {{0, 3}, {0, 1}},
 PlotRangePadding -> {.1, .05},
 Axes -> True,
 ImageSize -> 400] 

If you do the same graphic with say 25 points you will see that Mathematica
did not always use points very close to the boundary.


David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/  





From: ADL [mailto:alberto.dilullo at tiscali.it] 

Following what Bob brilliantly suggested, I found a possible bug in
Mathematica 7.0.1 for Windows.
If you type the following, you will get a couple of red lines getting
out of their boundary:

f[x_] := Sin[x];

Plot[
 {Table[(x - k)/3, {k, -3, 3, .10}],    f[3x]},
 {x, 0, 3},
 PlotRange -> {0, 1},
 RegionFunction ->  Function[{x, y}, 0 < y <= f[3x]],
 PlotStyle -> {
   Directive[ AbsoluteThickness[4], Red],
   Directive[ Thick,  Blue]
 }
]

Does anybody else confirms this?

ADL



On 18 Lug, 07:06, Bob Hanlon <hanl... at cox.net> wrote:
> I forget to copy the definition of f[x] that I was using as an example
>
> f[x_] = Exp[-x]
>
> Bob Hanlon






  • Prev by Date: Re: Kolmogorov-Smirnov 2-sample test
  • Next by Date: Re: Very very basic question about Mathematica expressions
  • Previous by thread: Re: Hatched shading?
  • Next by thread: Combine matrices of equal height