home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / Conic Sections 0.9.2 / Sources / DrawContent.cpp < prev    next >
Encoding:
Text File  |  1997-07-13  |  6.0 KB  |  207 lines  |  [TEXT/CWIE]

  1. //Copyright (c) 1997 Brian and Aidan Cully
  2. //All rights reserved
  3.  
  4. #include <math.h>
  5. #include <Quickdraw.h>
  6. #include <Menus.h>
  7. #include "DrawContent.h"
  8. #include "Misc.h"
  9. #include "MenuHandler.h"
  10. #include "CLStrStuff.h"
  11. #include "Messages.h"
  12. #include "CLLine.h"
  13. #include "CLStringShape.h"
  14. #include "CLColorStyle.h"
  15. #include "CLRectShape.h"
  16. #include "LMath.h"
  17.  
  18. TEquContent::TEquContent( TLayoutBranch *super, MActionHandler *superHandler, Boolean hasDD,
  19.  TPlane *nplane ):
  20.     TDocContent( super, hasDD, superHandler, nplane )
  21. {
  22. }
  23.  
  24. SInt8 TEquContent::DXDivY( int x, int y, unsigned char *num, unsigned char *den )
  25. {
  26.     MoveTo( x, y );
  27.     DrawString( num );
  28.     MoveTo( x, y+3 );
  29.     Line( 40, 0 );
  30.     MoveTo( x, y+12+6 );
  31.     DrawString( den );
  32. }
  33.  
  34. void TEquContent::DrawSelf( TDrawSlate *gr )
  35. {
  36.     TEraseRectShape( GetLocalRect() ).RenderOn( gr );
  37.     float a,b,npang;
  38.     int dec,sign,deccount;
  39.     Str32 astr,bstr;
  40.  
  41.     MoveTo(20,20);
  42.     npang = fmod(mPlane->GetAngle(),180);
  43.     if (fabs(npang)<0.001) {
  44.         a = mPlane->GetDistance()*mPlane->GetDistance();
  45.         StrCpy( astr, gcvt(a,5) );
  46.         DrawString("\px^2 + y^2 =");
  47.         DrawString(astr);
  48.     } else if ((npang == 45) || (npang == 135)) {
  49.         a = 4*mPlane->GetDistance()/2;
  50.         if( npang == 135 )
  51.             a = -a;
  52.         DrawString("\px^2 = ");
  53.         StrCpy( astr, gcvt( a, 5 ) );
  54.         DrawString(astr);
  55.         DrawString("\py");
  56.     } else if ((npang < 45) || (npang > 135)) {
  57.         a = mPlane->GetDistance()/mPlane->GetCosAngle();
  58.         a = a*a;
  59.         b = mPlane->GetDistance()*tan((45-mPlane->GetAngle())*M_PI/180);
  60.         b += mPlane->GetDistance()*tan((45+mPlane->GetAngle())*M_PI/180);
  61.         b /= 2;
  62.         b = b*b;
  63.         StrCpy( astr, gcvt( a, 5 ) );
  64.         DXDivY( 20, 20, "\px^2", astr );
  65.         StrCpy( bstr, gcvt( b, 5 ) );
  66.         DXDivY( 80, 20, "\py^2", bstr );
  67.         MoveTo( 70, 28 );
  68.         DrawString("\p+");
  69.         Move(47,0);
  70.         DrawString("\p= 1");
  71.     } else {
  72.         a = mPlane->GetDistance()/mPlane->GetSinAngle();
  73.         a = a*a;
  74.         b = mPlane->GetDistance()*tan((45-mPlane->GetAngle())*M_PI/180);
  75.         b += (mPlane->GetDistance()*tan((45+mPlane->GetAngle())*M_PI/180));
  76.         b /= 2;
  77.         b = b*b;
  78.         StrCpy( astr, gcvt( a, 5 ) );
  79.         DXDivY( 20, 20, "\py^2", astr );
  80.         StrCpy( bstr, gcvt( b, 5 ) );
  81.         DXDivY( 80, 20, "\px^2", bstr );
  82.         MoveTo( 70, 28 );
  83.         DrawString( "\p-" );
  84.         Move(54,0);
  85.         DrawString("\p= 1");
  86.     }
  87. }
  88.  
  89. TPlotContent::TPlotContent( TLayoutBranch *super, MActionHandler *superHandler, Boolean hasDD,
  90.  TConic *nconic, TPlane *nplane ):
  91.     TDocContent( super, hasDD, superHandler, nplane ),
  92.     mConic( nconic )
  93. {
  94. }
  95.  
  96. void TPlotContent::DrawSelf( TDrawSlate *gr )
  97. {
  98.     TEraseRectShape( GetLocalRect() ).RenderOn( gr );
  99.     DrawAxis( gr );
  100.     DrawIntersect( gr );
  101. }
  102.  
  103. SInt8 TPlotContent::DrawAxis( TDrawSlate *gr )
  104. {
  105.     Rect drawRect = GetLocalRect();
  106.     TColorStyle xcol( 0, 0x8000, 0xffff );
  107.     TColorStyle ycol( 0, 0, 0xffff );
  108.     TColorStyle black( 0, 0, 0 );
  109.     int iter;
  110.  
  111.     ::OffsetRect( &drawRect, -drawRect.left, -drawRect.top );
  112.     //color 0xff0000
  113.     TLine( &xcol, 0, drawRect.bottom/2, drawRect.right, drawRect.bottom/2 ).RenderOn( gr );
  114.     //gr->SetColor( 0, 0, 0 );
  115.     TStringShape( &black, drawRect.right-12, drawRect.bottom/2, "x" ).RenderOn( gr );
  116.     TLine( &ycol, drawRect.right/2, 0, drawRect.right/2, drawRect.bottom ).RenderOn( gr );
  117.     TStringShape( &black, drawRect.right/2, drawRect.bottom-8, "y" ).RenderOn( gr );
  118.     for( iter= -6; iter<=6; iter++ ) {
  119.         int x, y;
  120.         x= (drawRect.left+drawRect.right)/2+iter*((drawRect.right-drawRect.left)/12);
  121.         y= (drawRect.top+drawRect.bottom)/2+iter*((drawRect.bottom-drawRect.top)/12);
  122.         TLine( &black, x, drawRect.bottom/2-3, x, drawRect.bottom/2+3 ).RenderOn( gr );
  123.         TLine( &black, drawRect.right/2-3, y, drawRect.right/2+3, y ).RenderOn( gr );
  124.     }
  125.     //color 0xff00
  126. }
  127.  
  128. SInt8 TPlotContent::DrawIntersect( TDrawSlate *gr )
  129. {
  130.     int numint,border[8],numtint,numbint;
  131.     p2d inter2,ptint[180],pbint[180],point,opoint;
  132.     p3d inter3;
  133.     Rect drawRect= GetLocalRect();
  134.     float npang= fmod( mPlane->GetAngle(), 180.0 )-45.0;
  135.  
  136.     numtint = 0;
  137.     numbint = 0;
  138.  
  139.     //gr->SetColor( 0xffff, 0, 0 );
  140.     TColorStyle inter( 0xffff, 0, 0 );
  141.     for(numint=0;numint<mConic->mNumInt;numint++) {
  142.         inter3 = mConic->mConic[mConic->mIntersect[numint]];
  143.         inter2.x = inter3.x;
  144.         inter2.y = mPlane->GetCosAngle()*inter3.y + mPlane->GetSinAngle()*inter3.z;
  145.         if (!(mConic->mIntersect[numint]%2)) {
  146.             ptint[numtint].x = inter2.x;
  147.             ptint[numtint].y = inter2.y;
  148.             numtint++;
  149.         } else {
  150.             pbint[numbint].x = inter2.x;
  151.             pbint[numbint].y = inter2.y;
  152.             numbint++;
  153.         }
  154.     }
  155.     if( numbint < mConic->mNumInt ) {
  156.         point.x = ptint[0].x;
  157.         point.y = ptint[0].y;
  158.         point.x *= (drawRect.right-drawRect.left)/12;
  159.         point.x += (drawRect.right-drawRect.left)/2;
  160.         point.y *= -(drawRect.bottom-drawRect.top)/12;
  161.         point.y += (drawRect.bottom-drawRect.top)/2;
  162.         /*if( (npang < 90.0) && (npang>0.0) )
  163.             numtint--;*/
  164.         for(numint=0;numint<numtint;numint++) {
  165.             opoint = point;
  166.             point.x = ptint[numint].x;
  167.             point.y = ptint[numint].y;
  168.             point.x *= (drawRect.right-drawRect.left)/12;
  169.             point.x += (drawRect.right-drawRect.left)/2;
  170.             point.y *= -(drawRect.bottom-drawRect.top)/12;
  171.             point.y += (drawRect.bottom-drawRect.top)/2;
  172.             //color 0xff
  173.             TLine( &inter, opoint.x,opoint.y,point.x,point.y ).RenderOn( gr );
  174.             //gr->DrawLine( opoint.x,opoint.y,point.x,point.y );
  175.         }
  176.         /*if( (npang < 90.0) && (npang>0.0) )
  177.             numtint++;*/
  178.     }
  179.     if (numtint < mConic->mNumInt) {
  180.         point.x = pbint[0].x;
  181.         point.y = pbint[0].y;
  182.         point.x *= (drawRect.right-drawRect.left)/12;
  183.         point.x += (drawRect.right-drawRect.left)/2;
  184.         point.y *= -(drawRect.bottom-drawRect.top)/12;
  185.         point.y += (drawRect.bottom-drawRect.top)/2;
  186.         /*if( (npang < 90.0) && (npang>0.0) )
  187.             numbint--;*/
  188.         for(numint=0;numint<numbint;numint++) {
  189.             opoint = point;
  190.             point.x = pbint[numint].x;
  191.             point.y = pbint[numint].y;
  192.             point.x *= (drawRect.right-drawRect.left)/12;
  193.             point.x += (drawRect.right-drawRect.left)/2;
  194.             point.y *= -(drawRect.bottom-drawRect.top)/12;
  195.             point.y += (drawRect.bottom-drawRect.top)/2;
  196.             /*if ((opoint.y == point.y) || (opoint.x == point.x)) {
  197.                 opoint.x = point.x;
  198.                 opoint.y = point.y;
  199.             }*/
  200.             //color 0xff
  201.             TLine( &inter, opoint.x,opoint.y,point.x,point.y ).RenderOn( gr );
  202.             //gr->DrawLine( opoint.x,opoint.y,point.x,point.y );
  203.         }
  204.         /*if( (npang < 90.0) && (npang>0.0) )
  205.             numbint++;*/
  206.     }
  207. }