3D ball with gradated shading,

Denis Girou Denis.Girou at idris.fr
Wed Jul 23 21:52:49 CEST 1997


-----------------------------------------------------------------------------
This is the PSTricks mailing list, devoted to discussions about computational
graphics in (La)TeX using the PSTricks package from Timothy van Zandt.
For help using this mailing list, see instructions at the end of message.
-----------------------------------------------------------------------------

>>>>> "Jan.Krupa" == Jan Krupa <krupa at alpha.sggw.waw.pl> writes:

    Jan.Krupa> I would like to draw ** a 3D ball filled gradually to achieve the 3D effect**.
    Jan.Krupa> How it is possible using PSTricks?

    Jan.Krupa> ............................................................

    Jan.Krupa> The problem is that 'gradient' macro fills the region gradually but the
    Jan.Krupa> lines of "the same intensity of color" are **straight line**.
    Jan.Krupa> How is it possible to force the gradient macro to have the lines as e.g. 
    Jan.Krupa> ellipse or other curve ?

  Here is one attempt (not perfect, but is seems usable).

  Variations are obtained playing with the lines ". . scale" in the
GradientFill macro and "x . div y . div CurrentY 0 360 arc" in the
GradientLoop macro.

D.G.

\documentclass{article}

\usepackage{pst-grad}

\pagestyle{empty}

\begin{document}

\psset{linestyle=none,fillstyle=gradient,gradmidpoint=0}

\pstVerb{%
tx at GradientDict begin
/GradientFill {
  rotate
  /MidPoint ED
  /NumLines ED
  /LastBlue ED
  /LastGreen ED
  /LastRed ED
  /FirstBlue ED
  /FirstGreen ED
  /FirstRed ED
  % This avoids gaps due to rounding errors:
  clip
  pathbbox           %leave llx,lly,urx,ury on stack
  /y ED /x ED
  2 copy translate
  y sub neg /y ED
  x sub neg /x ED
  % This avoids gaps due to rounding errors:
  LastRed FirstRed add 2 div
  LastGreen FirstGreen add 2 div
  LastBlue FirstBlue add 2 div
  setrgbcolor
% DG modification begin - Jul. 23, 1997
%  fill
%  /YSizePerLine y NumLines div def
%  /CurrentY 0 def
%  /MidLine NumLines 1 MidPoint sub mul abs cvi def
  /YSizePerLine y NumLines div def
  /CurrentY y 2 div def
  /MidLine NumLines 2 div 1 MidPoint sub mul abs cvi def
% DG modification end
  MidLine NumLines 2 sub gt
  { /MidLine NumLines def }
  { MidLine 2 lt { /MidLine 0 def } if }
  ifelse
  MidLine 0 gt
  {
    /Red FirstRed def
    /Green FirstGreen def
    /Blue FirstBlue def
    /RedIncrement LastRed FirstRed sub MidLine 1 sub div def
    /GreenIncrement LastGreen FirstGreen sub MidLine 1 sub div def
    /BlueIncrement LastBlue FirstBlue sub MidLine 1 sub div def
    MidLine { GradientLoop } repeat
  } if
  MidLine NumLines lt
  {
    /Red LastRed def
    /Green LastGreen def
    /Blue LastBlue def
    /RedIncrement FirstRed LastRed sub NumLines MidLine sub 1 sub div def
    /GreenIncrement FirstGreen LastGreen sub NumLines MidLine sub 1 sub div def
    /BlueIncrement FirstBlue LastBlue sub NumLines MidLine sub 1 sub div def
    NumLines MidLine sub { GradientLoop } repeat
  } if
} def
/GradientLoop {
% DG modification begin - Jul. 23, 1997
  CurrentY 0 gt {
    x 2 div y 2 div CurrentY 0 360 arc
    Red Green Blue setrgbcolor fill
    /CurrentY CurrentY YSizePerLine sub def
    /Blue Blue BlueIncrement add def
    /Green Green GreenIncrement add def
    /Red Red RedIncrement add def
  } if
% DG modification end
} def
end}

\pscircle[gradbegin=red,gradend=white]{2}

\pstVerb{%
tx at GradientDict begin
/GradientLoop {
% DG modification begin - Jul. 23, 1997
  CurrentY 0 gt {
    x 1.5 div y 1.5 div CurrentY 0 360 arc
    Red Green Blue setrgbcolor fill
    /CurrentY CurrentY YSizePerLine sub def
    /Blue Blue BlueIncrement add def
    /Green Green GreenIncrement add def
    /Red Red RedIncrement add def
  } if
% DG modification end
} def
end}

\vspace{5cm}
\pscircle[gradbegin=green,gradend=white]{2}

\vspace{5cm}
\psellipse[gradbegin=blue,gradend=white](3,1.5)

\pstVerb{%
tx at GradientDict begin
/GradientFill {
  rotate
  /MidPoint ED
  /NumLines ED
  /LastBlue ED
  /LastGreen ED
  /LastRed ED
  /FirstBlue ED
  /FirstGreen ED
  /FirstRed ED
  % This avoids gaps due to rounding errors:
  clip
  pathbbox           %leave llx,lly,urx,ury on stack
  /y ED /x ED
  2 copy translate
  y sub neg /y ED
  x sub neg /x ED
  % This avoids gaps due to rounding errors:
  LastRed FirstRed add 2 div
  LastGreen FirstGreen add 2 div
  LastBlue FirstBlue add 2 div
  setrgbcolor
% DG modification begin - Jul. 23, 1997
%  fill
%  /YSizePerLine y NumLines div def
%  /CurrentY 0 def
%  /MidLine NumLines 1 MidPoint sub mul abs cvi def
  /YSizePerLine y NumLines div def
  /CurrentY y 2 div def
  /MidLine NumLines 2 div 1 MidPoint sub mul abs cvi def
% DG modification end
  MidLine NumLines 2 sub gt
  { /MidLine NumLines def }
  { MidLine 2 lt { /MidLine 0 def } if }
  ifelse
  MidLine 0 gt
  {
    /Red FirstRed def
    /Green FirstGreen def
    /Blue FirstBlue def
    /RedIncrement LastRed FirstRed sub MidLine 1 sub div def
    /GreenIncrement LastGreen FirstGreen sub MidLine 1 sub div def
    /BlueIncrement LastBlue FirstBlue sub MidLine 1 sub div def
% DG modification begin - Jul. 23, 1997
    2 1 scale
% DG modification end
    MidLine { GradientLoop } repeat
  } if
  MidLine NumLines lt
  {
    /Red LastRed def
    /Green LastGreen def
    /Blue LastBlue def
    /RedIncrement FirstRed LastRed sub NumLines MidLine sub 1 sub div def
    /GreenIncrement FirstGreen LastGreen sub NumLines MidLine sub 1 sub div def
    /BlueIncrement FirstBlue LastBlue sub NumLines MidLine sub 1 sub div def
    NumLines MidLine sub { GradientLoop } repeat
  } if
} def
/GradientLoop {
% DG modification begin - Jul. 23, 1997
  CurrentY 0 gt {
    x 4 div y 2 div CurrentY 0 360 arc
    Red Green Blue setrgbcolor fill
    /CurrentY CurrentY YSizePerLine sub def
    /Blue Blue BlueIncrement add def
    /Green Green GreenIncrement add def
    /Red Red RedIncrement add def
  } if
% DG modification end
} def
end}

\vspace{5cm}
\pscircle[gradbegin=black,gradend=white]{2}

\end{document}

-----------------------------------------------------------------------------
To [un]subscribe to this list, send mail to pstricks-request at mail.tug.org
Leave the subject line blank and in the body put the line
[un]subscribe <email-address>
For help, put the word "help" in the message body.
To obtain current archive, put the words "get current" in the message body.
-----------------------------------------------------------------------------



More information about the PSTricks mailing list