<?xml version="1.0"?>
<!--=====================================================================
 Stylesheet: sin_cos.xsl
    Version: 1.0 (2004-07-21)
     Author: Boris Karapetyan
    Purpose: calculate the Sin and Cos for positive angles (from 0 to 360) with 5 grad 
             step, so for 31 to 35 the result is same. This presision is enough for pie chart.
      Usage: <xsl:call-template name="sin" or "cos">
                <xsl:with-param name="pX" select="floor(($angle+4) div 5) *5"/>
             </xsl:call-template>
     Notice: Copyright (c)2004 Zenteq.am  ALL RIGHTS RESERVED.
             No limitation on use - except this code may not be published,
             in whole or in part, without prior written consent of the
             copyright owner.
======================================================================= -->
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- exclude-result-prefixes="xsl"
  ==========================================================================
    Template: sin
     Purpose: Return the sine of X
  Parameters: $pX    - the angle in grad
  ========================================================================== -->
  <xsl:template name="sin">
    <xsl:param name="pX"/>
    <xsl:variable name="sign"> <!-- Sin(-X) == -Sin(X), see <xsl:value-of select="$sign * $val"> -->
       <xsl:choose>
           <xsl:when test="(0 &lt;= $pX and $pX &lt; 180) or $pX &gt;= 360"> <!-- last case for cos template -->
              <xsl:value-of select="1"/> 
           </xsl:when>
           <xsl:otherwise>
              <xsl:value-of select="-1"/> 
           </xsl:otherwise>
       </xsl:choose>
    </xsl:variable>
    <xsl:variable name="val">
       <xsl:choose> <!-- simple math -->
         <xsl:when test="0 &lt;= $pX and $pX &lt; 90">
           <xsl:call-template name="sinValue">
             <xsl:with-param name="angle" select="$pX"/>
           </xsl:call-template>
         </xsl:when>
         <xsl:when test="90 &lt;= $pX and $pX &lt; 180">
           <xsl:call-template name="sinValue">
             <xsl:with-param name="angle" select="180 - $pX"/>
           </xsl:call-template>
         </xsl:when>
         <xsl:when test="180 &lt;= $pX and $pX &lt; 270">
           <xsl:call-template name="sinValue">
               <xsl:with-param name="angle" select="$pX - 180"/>
           </xsl:call-template>
         </xsl:when>
         <xsl:when test="270 &lt;= $pX and $pX &lt; 360">
           <xsl:call-template name="sinValue">
               <xsl:with-param name="angle" select="360 - $pX"/>
           </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
             <xsl:call-template name="sinValue">
               <xsl:with-param name="angle" select="$pX - 360"/>
             </xsl:call-template>
          </xsl:otherwise>
       </xsl:choose>
    </xsl:variable>
    <xsl:value-of select="$sign * $val"/> 
  </xsl:template> 

  <xsl:template name="sinValue">
    <xsl:param name="angle"/>
    <xsl:choose>
       <xsl:when test="$angle = 0">
            <xsl:value-of select="0.087155742747658173558064270837474"/>
       </xsl:when>
       <xsl:when test="$angle &lt;= 5">
            <xsl:value-of select="0.087155742747658173558064270837474"/>
       </xsl:when>
       <xsl:when test="5 &lt; $angle and $angle &lt;= 10">
            <xsl:value-of select="0.17364817766693034885171662676931"/>
       </xsl:when>
       <xsl:when test="10 &lt; $angle and $angle &lt;= 15">
            <xsl:value-of select="0.25881904510252076234889883762405"/>
       </xsl:when>
       <xsl:when test="15 &lt; $angle and $angle &lt;= 20">
            <xsl:value-of select="0.34202014332566873304409961468226"/>
       </xsl:when>
       <xsl:when test="20 &lt; $angle and $angle &lt;= 25">
            <xsl:value-of select="0.42261826174069943618697848964773"/>
       </xsl:when>
       <xsl:when test="25 &lt; $angle and $angle &lt;= 30">
            <xsl:value-of select="0.5"/>
       </xsl:when>
       <xsl:when test="30 &lt; $angle and $angle &lt;= 35">
            <xsl:value-of select="0.57357643635104609610803191282616"/>
       </xsl:when>
       <xsl:when test="35 &lt; $angle and $angle &lt;= 40">
            <xsl:value-of select="0.64278760968653932632264340990726"/>
       </xsl:when>
       <xsl:when test="40 &lt; $angle and $angle &lt;= 45">
            <xsl:value-of select="0.70710678118654752440084436210485"/>
       </xsl:when>
       <xsl:when test="45 &lt; $angle and $angle &lt;= 50">
            <xsl:value-of select="0.76604444311897803520239265055542"/>
       </xsl:when>
       <xsl:when test="50 &lt; $angle and $angle &lt;= 55">
            <xsl:value-of select="0.81915204428899178968448838591684"/>
       </xsl:when>
       <xsl:when test="55 &lt; $angle and $angle &lt;= 60">
            <xsl:value-of select="0.86602540378443864676372317075294"/>
       </xsl:when>
       <xsl:when test="60 &lt; $angle and $angle &lt;= 65">
            <xsl:value-of select="0.90630778703664996324255265675432"/>
       </xsl:when>
       <xsl:when test="65 &lt; $angle and $angle &lt;= 70">
            <xsl:value-of select="0.93969262078590838405410927732473"/>
       </xsl:when>
       <xsl:when test="70 &lt; $angle and $angle &lt;= 75">
            <xsl:value-of select="0.9659258262890682867497431997289"/>
       </xsl:when>
       <xsl:when test="75 &lt; $angle and $angle &lt;= 80">
            <xsl:value-of select="0.98480775301220805936674302458952"/>
       </xsl:when>
       <xsl:when test="80 &lt; $angle and $angle &lt;= 85">
            <xsl:value-of select="0.99619469809174553229501040247389"/>
       </xsl:when>
      <xsl:otherwise>
       <!-- xsl:when test="85 &lt; $angle and $angle &lt;= 90" -->
            <xsl:value-of select="1"/>
       </xsl:otherwise>
     </xsl:choose>
  </xsl:template>

  <!--==========================================================================
    Template: cos
     Purpose: Return the cosine of X
  Parameters: $pX - the angle in degrees  
  ========================================================================== -->
  <xsl:template name="cos">
    <xsl:param name="pX"/>
    <xsl:call-template name="sin">
      <xsl:with-param name="pX" select="$pX+90"/>
    </xsl:call-template>
  </xsl:template>
</xsl:stylesheet>
