home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / online / source / cpp / graphics / Conics.sit.hqx / Conics / Sources / DrawWindows.cpp < prev    next >
Text File  |  1996-11-13  |  6KB  |  200 lines

  1. //Copyright 1996 Brian+Aidan Cully
  2. //All rights reserved
  3.  
  4. #include <math.h>
  5. #include <Quickdraw.h>
  6. #include <Menus.h>
  7. #include "DrawWindows.h"
  8. #include "Graphics.h"
  9. #include "MenuHandler.h"
  10. #include "StrStuff.h"
  11.  
  12. SInt8 TEquWindow::DXDivY( int x, int y, unsigned char *num, unsigned char *den ) {
  13.     MoveTo( x, y );
  14.     DrawString( num );
  15.     MoveTo( x, y+3 );
  16.     Line( 40, 0 );
  17.     MoveTo( x, y+12+6 );
  18.     DrawString( den );
  19. }
  20.  
  21. void TEquWindow::DrawSelf() {
  22.     EraseRect( &qd.screenBits.bounds );
  23.     float a,b,npang;
  24.     int dec,sign,deccount;
  25.     Str32 astr,bstr;
  26.  
  27.     MoveTo(20,20);
  28.     npang = fmod(plane->GetAngle(),180);
  29.     if (npang == 0) {
  30.         a = pow(plane->GetDistance(),2);
  31.         StrCpy( astr, gcvt(a,5) );
  32.         DrawString("\px^2 + y^2 =");
  33.         DrawString(astr);
  34.     } else if ((npang == 45) || (npang == 135)) {
  35.         a = 4*plane->GetDistance()/2;
  36.         if( npang == 135 )
  37.             a = -a;
  38.         DrawString("\px^2 = ");
  39.         StrCpy( astr, gcvt( a, 5 ) );
  40.         DrawString(astr);
  41.         DrawString("\py");
  42.     } else if ((npang < 45) || (npang > 135)) {
  43.         a = plane->GetDistance()/cos(plane->GetAngle()*_PI/180);
  44.         a = fabs(a);
  45.         b = plane->GetDistance()*tan((45-plane->GetAngle())*_PI/180);
  46.         b += plane->GetDistance()*tan((45+(plane->GetAngle())*_PI/180));
  47.         b /= 2;
  48.         b = fabs(b);
  49.         StrCpy( astr, gcvt( a, 5 ) );
  50.         DXDivY( 20, 20, "\px^2", astr );
  51.         StrCpy( bstr, gcvt( b, 5 ) );
  52.         DXDivY( 80, 20, "\py^2", bstr );
  53.         MoveTo( 70, 28 );
  54.         DrawString("\p+");
  55.         Move(47,0);
  56.         DrawString("\p= 1");
  57.     } else {
  58.         a = plane->GetDistance()/plane->GetSinAngle();
  59.         a = fabs(a);
  60.         b = plane->GetDistance()*tan((45-plane->GetAngle())*_PI/180);
  61.         b += (plane->GetDistance()*tan((45+plane->GetAngle())*_PI/180));
  62.         b /= 2;
  63.         b = fabs(b);
  64.         StrCpy( astr, gcvt( a, 5 ) );
  65.         DXDivY( 20, 20, "\py^2", astr );
  66.         StrCpy( bstr, gcvt( b, 5 ) );
  67.         DXDivY( 80, 20, "\px^2", bstr );
  68.         MoveTo( 70, 28 );
  69.         DrawString( "\p-" );
  70.         Move(54,0);
  71.         DrawString("\p= 1");
  72.     }
  73. }
  74.  
  75. SInt8 TEquWindow::MakeActive( Boolean active ) {
  76.     inherited::MakeActive( active );
  77.     CheckItem( GetMenuHandle( m_WINDOW ), mw_EQU, active );
  78.     return( true );
  79. }
  80.  
  81. void TInstructWindow::DrawSelf() {
  82.     puttextxy(10,12,"\pUp - Rotate Axis Up" );
  83.     puttextxy(10,24,"\pLeft - Rotate Axis Left");
  84.     puttextxy(10,36,"\pRight - Rotate Axis Right");
  85.     puttextxy(10,48,"\pDown - Rotate Axis Down");
  86.  
  87.     puttextxy(10,60,"\pW - Rotate Plane Clockwise");
  88.     puttextxy(10,72,"\pZ - Rotate Plane Counter-Clockwise");
  89.     puttextxy(10,84,"\p- - Move Plane Toward Origin");
  90.     puttextxy(10,96,"\p+ - Move Plane From Origin");
  91. }
  92.  
  93. SInt8 TInstructWindow::MakeActive( Boolean active ) {
  94.     inherited::MakeActive( active );
  95.     CheckItem( GetMenuHandle( m_WINDOW ), mw_INSTRUCT, active );
  96.     if( active )
  97.         DisableItem( GetMenuHandle( m_EDIT ), me_COPY );
  98.     else
  99.         EnableItem( GetMenuHandle( m_EDIT ), me_COPY );
  100.     return( true );
  101. }
  102.  
  103. void TPlotWindow::DrawSelf() {
  104.     DrawIntersect();
  105. }
  106.  
  107. SInt8 TPlotWindow::DrawAxis() {
  108.     Rect drawRect = ::GetWindowPort( window )->portRect;
  109.  
  110.     ::OffsetRect( &drawRect, -drawRect.left, -drawRect.top );
  111.     putline(0,drawRect.bottom/2,drawRect.right,drawRect.bottom/2,0xff0000);
  112.     puttextxy(drawRect.right-12,drawRect.bottom/2,"\px");
  113.     putline(drawRect.right/2,0,drawRect.right/2,drawRect.bottom,0xff00);
  114.     puttextxy(drawRect.right/2,drawRect.bottom-8,"\py");
  115. }
  116.  
  117. SInt8 TPlotWindow::DrawIntersect() {
  118.     int numint,border[8],numtint,numbint;
  119.     p2d inter2,ptint[180],pbint[180],point,opoint;
  120.     p3d inter3;
  121.     Rect drawRect;
  122.  
  123.     drawRect = ::GetWindowPort( window )->portRect;
  124.     ::OffsetRect( &drawRect, -drawRect.left, -drawRect.top );
  125.     ::EraseRect( &drawRect );
  126.     DrawAxis();
  127.  
  128.     numtint = 0;
  129.     numbint = 0;
  130.  
  131.     for(numint=0;numint<conic->tnumint;numint++) {
  132.         inter3 = conic->conic[conic->intersect[numint]];
  133.         inter2.x = inter3.x;
  134.         inter2.y = plane->GetCosAngle()*inter3.y + plane->GetSinAngle()*inter3.z;
  135.         if ((int)(fmod((float)(conic->intersect[numint]),2.0)) == 0) {
  136.             ptint[numtint].x = inter2.x;
  137.             ptint[numtint].y = inter2.y;
  138.             numtint++;
  139.         } else {
  140.             pbint[numbint].x = inter2.x;
  141.             pbint[numbint].y = inter2.y;
  142.             numbint++;
  143.         }
  144.     }
  145.     if (numbint < conic->tnumint) {
  146.         point.x = ptint[0].x;
  147.         point.y = ptint[0].y;
  148.         point.x *= drawRect.right/12;
  149.         point.x += drawRect.right/2;
  150.         point.y *= -drawRect.bottom/12;
  151.         point.y += drawRect.bottom/2;
  152.         for(numint=0;numint<numtint;numint++) {
  153.             opoint = point;
  154.             point.x = ptint[numint].x;
  155.             point.y = ptint[numint].y;
  156.             point.x *= drawRect.right/12;
  157.             point.x += drawRect.right/2;
  158.             point.y *= -drawRect.bottom/12;
  159.             point.y += drawRect.bottom/2;
  160.             if ((opoint.y == point.y) || (opoint.x == point.x)) {
  161.                 opoint.x = point.x;
  162.                 opoint.y = point.y;
  163.             }
  164.             putline(opoint.x,opoint.y,point.x,point.y,0xff);
  165.         }
  166.     }
  167.     if (numtint < conic->tnumint) {
  168.         point.x = pbint[0].x;
  169.         point.y = pbint[0].y;
  170.         point.x *= drawRect.right/12;
  171.         point.x += drawRect.right/2;
  172.         point.y *= -drawRect.bottom/12;
  173.         point.y += drawRect.bottom/2;
  174.         for(numint=0;numint<numbint;numint++) {
  175.             opoint = point;
  176.             point.x = pbint[numint].x;
  177.             point.y = pbint[numint].y;
  178.             point.x *= drawRect.right/12;
  179.             point.x += drawRect.right/2;
  180.             point.y *= -drawRect.bottom/12;
  181.             point.y += drawRect.bottom/2;
  182.             if ((opoint.y == point.y) || (opoint.x == point.x)) {
  183.                 opoint.x = point.x;
  184.                 opoint.y = point.y;
  185.             }
  186.             putline(opoint.x,opoint.y,point.x,point.y,0xff);
  187.         }
  188.     }
  189. }
  190.  
  191. SInt8 TPlotWindow::MakeActive( Boolean active ) {
  192.     inherited::MakeActive( active );
  193.     CheckItem( GetMenuHandle( m_WINDOW ), mw_PLOT, active );
  194.     return( true );
  195. }
  196.  
  197. SInt8 TPlotWindow::RespondResizeSelf( const Rect &oldRect, const Rect &newRect ) {
  198.     TOffScreen::RespondResizeSelf( oldRect, newRect );
  199.     ::InvalRect( &oldRect );
  200. }