<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="/pattern.xsl"?>
<pattern id="">
  <name>
    Reactor
  </name>
  <relatedPatterns>
    <relatedPattern ref="">
	  <patternLink>Controller</patternLink>
    </relatedPattern>
  </relatedPatterns>
  <diagram>
  <web>
  <img src="DiagramReac.gif" width="400"  />	
  </web>
  <latex>
    <img src="DiagramReac.pdf" width="400"  />			
	</latex>
  </diagram>
  <what>
    <paragraph>
      Make an object respond to the proximity of another object.
    </paragraph>		
  </what>
  <when>
    <paragraph>
      The essence of parametric modeling is the definition of object properties
      in terms of the properties of upstream objects. A problem arises, if the
      relation between the object and its upstream precedents is based on
      proximity. The new location for the object becomes based on the old
      location for the object, making the object definition become circular!
      And circularity is not allowed in a propagation graph.
    </paragraph>
    <paragraph>
      Use this pattern when you want to make an object respond to the presence of another object.
    </paragraph>
  </when>
  <why>
    <paragraph>
      Designers often use the metaphor of <em>response</em> in which
      one part of a design depends upon the state of
      another. Reversing the perspective, is as if part of a design
      becomes a tool for shaping the other. This situation is very
      much like that encountered in the
      <patternLink>Controller</patternLink> pattern, but with a key
      difference---the controlling property is proximity. 
    </paragraph>
  </why>
  <how>
    <paragraph>
      The essential idea is simple: connect an <em>interactor</em> to a <em>result</em>
      through a <em>reference</em>.
    </paragraph>
    <paragraph>
       The trick is to join the <em>interactor</em> and the
       <em>result</em> with an intermediary and usually fixed object,
       which we call a <em>reference.</em> The <em>interactor</em> and
       the <em>reference</em> interact to produce the <em>result.</em>
    </paragraph>
    <paragraph>
      For example (see sample "Circle Radii below"), you might have a
      point and a circle, and you want the circle to get bigger (or
      smaller or elliptical) as you move the point closer. This can be
      done by using the <patternName>Reactor</patternName> pattern. As
      you might have guessed, the point is an interactor and the circle
      is a result (which can be replicated to give us an array of
      circles). The circles have to be somewhere. This somewhere is
      the reference. The reference is usually hidden in a
      <patternName>Reactor</patternName>.
    </paragraph>
    <paragraph>
      Position is a complex property that can manifest in many ways
      through a <patternName>Reactor</patternName>. Position is
      any combination of location and direction, for instance, the
      length or direction of lines, parameter or number or position of
      points, direction of planes, radius of circles and even
      additional properties such as color if they are made to depend
      on position.
    </paragraph>
  </how>
  <samples newpage="false">
    <sample id="Circle Radii and Point Interactor">
      <name>Circle Radii and Point Interactor</name>
      <when>
	<paragraph>
	  Control the size of a set of circles by proximity to a point.
	</paragraph>
      </when>
      <how>
	<paragraph>
	  Perhaps the simplest definition of a circle<latex>\index{circle}</latex> requires just
	  its <math latex="\afVar{centre}">centre</math> and <math
	  latex="\afVar{radius}">radius</math>. The <math
	  latex="\afVar{centre}">centre</math> (a point) is the reference and
	  the <math latex="\afVar{radius}">radius</math> is the result. The
	  free point is the interactor. Making the <math
	  latex="\afVar{radius}">radius</math> a function<latex>\index{function}</latex> of the distance
	  between the <math latex="\afVar{centre}">centre</math> and the
	  controlling point<latex>\index{point}</latex> completes this instance of the
	  <patternName>Reactor</patternName> pattern. In this case,
	  the function is a direct relationship--its value shrinks as
	  distance shrinks. As the interactor moves closer to the
	  circle, the circle gets smaller.
	</paragraph>
	<paragraph>
	  Replicating the reference causes all of the circles to react to 
	  the movement of the interactor. Hiding the reference,
	  creates an illusion of direct control from interactor to
	  result.
	</paragraph>
      </how>
      <a href="Samples/ReacCirclesRadius/ReacCirclesRadius.gct" >Click to download ReacCirclesRadius.gct</a> 
      <animation src="Samples/ReacCirclesRadius/AnimationCirclesRadius00.swf"/>
      <animation src="Samples/ReacCirclesRadius/AnimationCirclesRadius10.swf"/>
    </sample>

    <sample id="Circle Radii and Curve Interactor">
      <name>Circle Radii and Curve Interactor</name>
      <when>
	<paragraph>
	  Control the size of a set of circles by proximity to a curve.
	</paragraph>
      </when>
      <how>
	<paragraph>
	  In modeling terms, this sample hardly differs from the previous one. In both,
	  the radii of circles<latex>\index{circle}</latex> arranged in an array change with proximity to an
	  interactor. The only differences are that the interactor here is a curve<latex>\index{curve}</latex> and the
	  radii grow with proximity rather than shrink. The distance from a reference point<latex>\index{point}</latex> to
	  the curve is the distance between the point and its projection onto the curve --
	  this is the shortest distance between the point and curve. 
	</paragraph>
	<paragraph>
	  Hiding the reference removes the explicit visual fixed point of each circle. The
	  eye focus on the displayed part, which is changing, rather than the fixed,
	  hidden part. 
	</paragraph>
	<paragraph>
	  <latex>
\begin{bodyNote}
  \pbOneCol[r]
    {\pbIncludeGraphics[\extImagesPath/NASA/nilebend_etm_2000293_lrg.jpg]{width=\currentWidth}\\
    \source{NASA Earth Observatory image created by Jesse Allen, using Landsat data provided by the University of Maryland's Global Land Cover Facility.}}
\end{bodyNote}	  
	  </latex>
	</paragraph>
      </how>
      <a href="Samples/ReacCirclesRadius/ReacCirclesRadiusToCurve.gct" >Click to download ReacCirclesRadiusToCurve.gct</a> 
    </sample>

    <sample id="Lift">
      <name>Lift</name>
      <when>
	<paragraph>
	  Make a line's length increase as you move a point<latex>\index{point}</latex> closer to
	  its start point.
	</paragraph>
      </when>
      <how>
	<paragraph>
	  Define two points; call one the reference and one the interactor. On the reference define a vertical line<latex>\index{line}</latex>: the result. 
	</paragraph>
	<paragraph>
	  The length of the result is given by any two argument
	  function that increases as its distance between the
	  reference and its interactor shrinks, for example <math
	  latex="1/(\afFunc{distance}{\afVar{interactor},
	  \afVar{reference}}+0.1)">1/(Distance(interactor,
	  reference)+0.1)</math>. The small amount of <math
	  latex="0.1">0.1</math> that is added to the Distance
	  function result prevents the line from becoming infinitely
	  long as the distance between the points approaches zero. The
	  <patternLink>Mapping</patternLink> pattern gives several
	  better, but more complex functions that can be used here.
	</paragraph>
	<paragraph>
	  Replicate the start point and hide it. Now you have a set of
	  lines that react to the movement of the interactor.  In
	  turn, use the line endpoints to define the shape of another
	  object using the lines, such as a roof surface.
	</paragraph>
      </how>
      <a href="Samples/ReacLift/ReacLift.gct" >Click to download ReacLift.gct</a> 
      <animation src="Samples/ReacLift/AnimationLift00.swf"/>
      <animation src="Samples/ReacLift/AnimationLift10.swf"/>
      <animation src="Samples/ReacLift/AnimationLift20.swf"/>
    </sample>
	<latex>\renewcommand{\sampleNewPage}[0]{false}</latex>
    <sample id="Repeller">
      <name>Repeller</name>
      <when>
	<paragraph>
	  Make a point move away from a controlling point. 
	</paragraph>
      </when>
      <how>
	<paragraph>
	  Define two points<latex>\index{point}</latex>; call one the reference and one the
	  interactor. The result will lie on the infinite line defined
	  by the reference and the interactor.
	</paragraph>
	<paragraph>
	  The result is the sum of the reference and a vector<latex>\index{vector}</latex>. The
	  vector's direction is the same as the vector between the
	  interactor and the reference. Its length is an inverse
	  function of the distance between the interactor and the
	  reference: as the interactor moves towards the reference,
	  the length increases.
	</paragraph>
	<paragraph>
	  The particular function<latex>\index{function}</latex> used in this sample is
	  <latex>
             \[SD/(\afFunc{distance}{\afTMath{interactor},\afTMath{reference}}+\afTMath{SD}*0.01)\]
	  </latex>
	  <web>
	    (SD/(Distance(interactor,reference)+SD/100)
	    </web>
	  where <math>SD</math> stands for
	  <em>Standard Distance</em> and measures the size of
	  the modeled object. As
	  <math>SD</math> grows, so does
	  the effective distance over which the pattern operates.  The
	  small amount
	  <math latex="\afTMath{SD}*0.01">SD*0.01</math> 
	  added to the distance
	  prevents the result from moving infinitely as the interactor approaches the reference.
	</paragraph>
	<paragraph>
	  Replicate and hide the reference. The result points now appear to respond to the
	  interactor. In turn, use the result to define other objects, such as a surface.
	</paragraph>
      </how>
      <a href="Samples/ReacRepeller/ReacRepeller.gct" >Click to download ReacRepeller.gct</a>
      <animation src="Samples/ReacRepeller/AnimationRepeller00.swf"/>
      <animation src="Samples/ReacRepeller/AnimationRepeller10.swf"/>
    </sample>		
    <sample id="Vector Field">
      <name>Vector Field</name>
      <when>
	<paragraph>
	  Rotate a bound vector<latex>\index{vector!bound}</latex> as a controlling point<latex>\index{point}</latex> moves, so that it
	  always has the same angle to the point. Replicate to define a vector field<latex>\index{vector!field}</latex>.
	</paragraph>
      </when>
      <how>
	<paragraph>
  	  Define two points<latex>\index{point}</latex>: an interactor and a reference. The goal is to define
	  a bound vector from the reference such that it is always
	  right-handedly perpendicular to the imaginary line that connects the two
	  points.  

	  Create a coordinate system<latex>\index{frame}</latex> on the reference point by using
	  the interactor point to locate its <math latex="X">X</math>
	  axis.  Making the result vector on the <math
	  latex="y">y</math> axis of this coordinate system, ensures
	  that it will always be perpendicular to the <math
	  latex="x">x</math> axis and consequently to the connecting
	  line.
	</paragraph>
	<paragraph>
	  Note that the reference can be more than one object. In this
	  case both the start point of the vector and the coordinate
	  system comprise the reference. The coordinate
	  system could replace the reference point, simplifying back
	  to a single reference, but such reduction is not always possible.
	</paragraph>
	<paragraph>
	  Replicate<latex>\index{replication}</latex> the reference point in 1, 2 or 3 dimensions and
	  hide it. All of the result vectors will react to the
	  position of the interactor and portray a continuous vector field.
	</paragraph>
	<paragraph>
      <diagram>
	<img src="Samples/ReacVectorField/ironFillingsandMagnet.jpg" width="300"/>
      </diagram>
	</paragraph>
      </how>
      <a href="Samples/ReacVectorField/ReacVectorField.gct" >Click to download ReacVectorField.gct</a>
      <animation src="Samples/ReacVectorField/AnimationVectorField00.swf"/>
      <animation src="Samples/ReacVectorField/AnimationVectorField10.swf"/>			
    </sample>
    <sample id="Dimple">
      <name>Dimple</name>
      <when>
	<paragraph>
	  Make the local shape of a closed curve react to a nearby point.
	</paragraph>
      </when>
      <how> 
	<paragraph> 
	  Define a circle<latex>\index{circle}</latex> and two
	  points<latex>\index{point}</latex> on the circle. Call one
	  of these points the reference and the other the interactor.
	  Draw a radius of the circle from the reference point to the
	  circle's centre. Place the result, a parametric point on
	  this line, that moves toward the center if the interactor
	  gets <em>too close</em> to it.  The trick is to use a
	  condition that sets the parameter of the result point to
	  some value (here <math latex="0.4">0.4</math>) if the
	  distance between the interactor and the reference (which is
	  measured by the modular distance between their parameters)
	  becomes less than a value <math latex="d">d</math>
	  (explained later), otherwise sets it to another value (here
	  <math latex="0.2">0.2</math>).  This distance condition can
	  be defined as follows: </paragraph>
      </how>
<pre>
function modular01Distance (double t0, double t1)
 {
  object result = t0-t1;
  return 
   result > 0.5 ? 
    1 - result : 
    result >= 0 ? 
     result : 
     result > -0.5 ? 
      Abs(result) : 
      result + 1.0; 
 }
</pre>
      <more>
	<paragraph>
	  The parameter <math latex="d">d</math> here can be a number less than or equal
	  to <math latex="0.5">0.5</math> and greater than or equal to half of the
	  distance between each two references. The latter condition is needed so that the
	  test is always true for at least one point.  With <math
	  latex="\afVar{count}">count</math> reference objects equally distributed by
	  parametric distance, a minimal value for <math latex="d">d</math> is <math
	  latex="d = 1 / (2 * \afVar{count})">d = 1 / (2 * \afVar{count})</math>.
	</paragraph>
	<paragraph>
	  After replicating the reference, create a closed curve<latex>\index{curve}</latex>
	  interpolating the result points.  This curve will look like a
	  circle deformed by the interactor.
	</paragraph>
	<paragraph>
	  <latex>
	    \begin{bodyNote}
 	      \pbOneCol
                {\input{\GCFigsPath/Patterns/ReactorDimpleExplain0.tex}}
	    \end{bodyNote}
	  </latex>
	</paragraph>
      </more>
      <a href="Samples/ReacCircle/ReacCircle.gct" >Click to download ReacCircle.gct</a>
      <animation src="Samples/ReacCircle/AnimationCircle00.swf"/>
      <animation src="Samples/ReacCircle/AnimationCircle20.swf"/>
      <animation src="Samples/ReacCircle/AnimationCircle10.swf"/>
    </sample>
    
  </samples>
</pattern>

