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

  1. //Copyright (c) 1997 Brian and Aidan Cully
  2. //All rights reserved
  3.  
  4. #include "ConicContent.h"
  5. #include "Messages.h"
  6. #include <math.h>
  7. #include "CLLine.h"
  8. #include "CLStringShape.h"
  9. #include "CLColorStyle.h"
  10. #include "CLMouseHandler.h"
  11. #include "CLNullHandler.h"
  12. #include "CLKeyboardHandler.h"
  13. #include "CLRectShape.h"
  14. #include <Drag.h>
  15. #include "LMath.h"
  16. #include "CLBaseWindow.h"
  17.  
  18. TConicContent::TConicContent( TLayoutBranch *super, MActionHandler *superHandler, Boolean hasDD,
  19.  TConic *nconic, TPlane *nplane, float ntheta, float nphi ):
  20.     TDocContent( super, hasDD, superHandler, nplane ),
  21.     mTheta( ntheta ),
  22.     mPhi( nphi )
  23. {
  24.     mConic= nconic;
  25.     d = 45000;
  26.     D = 32767;
  27.     S = 10;
  28.     mNormCurs= ::GetCursor( 130 );
  29.     mShifCurs= ::GetCursor( 131 );
  30.     mOptCurs= ::GetCursor( 133 );
  31.     ::MoveHHi( (Handle)mNormCurs );
  32.     ::HLock( (Handle)mNormCurs );
  33.     ::MoveHHi( (Handle)mShifCurs );
  34.     ::HLock( (Handle)mShifCurs );
  35.     ::MoveHHi( (Handle)mOptCurs );
  36.     ::HLock( (Handle)mOptCurs );
  37. }
  38.  
  39. p2d TConicContent::P3dT2d( p3d coord3d, Rect scaleRect )
  40. {
  41.     p3d eyec;
  42.     p2d coord2d;
  43.     Point center;
  44.     Rect tRect = scaleRect;
  45.  
  46.     ::OffsetRect( &tRect, -scaleRect.left, -scaleRect.top );
  47.     center.h = tRect.right/2+scaleRect.left;
  48.     center.v = tRect.bottom/2+scaleRect.top;
  49.  
  50.     eyec.x = -1 * coord3d.x*mTheta.GetSinAngle() + coord3d.y*mTheta.GetCosAngle();
  51.     eyec.y = -1 * coord3d.x*mTheta.GetCosAngle()*mPhi.GetCosAngle() -
  52.      coord3d.y*mTheta.GetSinAngle()*mPhi.GetCosAngle() + coord3d.z*mPhi.GetSinAngle();
  53.     eyec.z = -1 * coord3d.x*mPhi.GetSinAngle()*mTheta.GetCosAngle() -
  54.      coord3d.y*mPhi.GetSinAngle()*mTheta.GetSinAngle() - coord3d.z*mPhi.GetCosAngle() + D;
  55.  
  56.     coord2d.x = (d/S) * (eyec.x/eyec.z);
  57.     coord2d.y = (d/S) * (eyec.y/eyec.z);
  58.  
  59.     coord2d.x = (coord2d.x * tRect.right/3)+center.h;
  60.     coord2d.y = coord2d.y * -tRect.bottom/3+center.v;
  61.  
  62.     return(coord2d);
  63. }
  64.  
  65. SInt8 TConicContent::DrawConic( TDrawSlate *gr )
  66. {
  67.     int surf;
  68.     p3d temp;
  69.     p2d opoint,point,obpoint,bpoint, center;
  70.     Rect drawRect = GetLocalRect();
  71.     TColorStyle rim( 0, 0, 0xffff ), mid( 0xffff, 0, 0 );
  72.  
  73.     DrawAxis( gr );
  74.     point = P3dT2d(mConic->mConic[0], drawRect);
  75.     bpoint = P3dT2d(mConic->mConic[1], drawRect);
  76.     temp.x = 0;
  77.     temp.y = 0;
  78.     temp.z = 0;
  79.     center = P3dT2d( temp, drawRect );
  80.  
  81.     RGBColor col;
  82.     for(surf = 0;surf<=CONICPARTS;surf+=2)
  83.     {
  84.         opoint = point;
  85.         obpoint = bpoint;
  86.         point = P3dT2d(mConic->mConic[surf], drawRect);
  87.         bpoint = P3dT2d(mConic->mConic[surf+1], drawRect);
  88.         TLine( &rim, opoint.x, opoint.y, point.x, point.y ).RenderOn( gr );
  89.         TLine( &mid, point.x, point.y, center.x, center.y ).RenderOn( gr );
  90.         TLine( &mid, center.x, center.y, bpoint.x, bpoint.y ).RenderOn( gr );
  91.         TLine( &rim, obpoint.x, obpoint.y, bpoint.x, bpoint.y ).RenderOn( gr );
  92.     }
  93. }
  94.  
  95. SInt8 TConicContent::DrawPlaneAxis( TDrawSlate *gr )
  96. {
  97.     TColorStyle xax( 0, 0x8000, 0xffff );
  98.     TColorStyle yax( 0, 0, 0xffff );
  99.     TColorStyle black( 0x8000, 0x8000, 0x8000 );
  100.     p3d e1, e2, iter;
  101.     p2d point, opoint;
  102.     int surf;
  103.     Rect drawRect= GetLocalRect();
  104.  
  105.     e1.x= (mPlane->mPlane[0].x+mPlane->mPlane[1].x)/2;
  106.     e1.y= (mPlane->mPlane[0].y+mPlane->mPlane[1].y)/2;
  107.     e1.z= (mPlane->mPlane[0].z+mPlane->mPlane[1].z)/2;
  108.     point= P3dT2d( e1, drawRect );
  109.     e2.x= (mPlane->mPlane[2].x+mPlane->mPlane[3].x)/2;
  110.     e2.y= (mPlane->mPlane[2].y+mPlane->mPlane[3].y)/2;
  111.     e2.z= (mPlane->mPlane[2].z+mPlane->mPlane[3].z)/2;
  112.     opoint= P3dT2d( e2, drawRect );
  113.     TLine( &yax, point.x, point.y, opoint.x, opoint.y ).RenderOn( gr );
  114.     iter= e1;
  115.     p3d temp= e2;
  116.     int ofx, ofy;
  117.     temp.x-= 0.5;
  118.     p2d tpoint= P3dT2d( temp, drawRect );
  119.     ofx= tpoint.x-opoint.x;
  120.     ofy= tpoint.y-opoint.y;
  121.     for( surf= 0; surf<12; surf++ ) {
  122.         TLine( &black, point.x-ofx, point.y-ofy, point.x+ofx, point.y+ofy ).RenderOn( gr );
  123.         iter.x+= (e2.x-e1.x)/12;
  124.         iter.y+= (e2.y-e1.y)/12;
  125.         iter.z+= (e2.z-e1.z)/12;
  126.         point= P3dT2d( iter, drawRect );
  127.     }
  128.     TLine( &black, point.x-ofx, point.y-ofy, point.x+ofx, point.y+ofy ).RenderOn( gr );
  129.     e1.x= (mPlane->mPlane[1].x+mPlane->mPlane[2].x)/2;
  130.     e1.y= (mPlane->mPlane[1].y+mPlane->mPlane[2].y)/2;
  131.     e1.z= (mPlane->mPlane[1].z+mPlane->mPlane[2].z)/2;
  132.     point= P3dT2d( e1, drawRect );
  133.     e2.x= (mPlane->mPlane[0].x+mPlane->mPlane[3].x)/2;
  134.     e2.y= (mPlane->mPlane[0].y+mPlane->mPlane[3].y)/2;
  135.     e2.z= (mPlane->mPlane[0].z+mPlane->mPlane[3].z)/2;
  136.     opoint= P3dT2d( e2, drawRect );
  137.     TLine( &xax, point.x, point.y, opoint.x, opoint.y ).RenderOn( gr );
  138.     iter= e1;
  139.     temp= e2;
  140.     temp.y-= (mPlane->mPlane[3].y-mPlane->mPlane[0].y)/24;
  141.     temp.z-= (mPlane->mPlane[3].z-mPlane->mPlane[0].z)/24;
  142.     tpoint= P3dT2d( temp, drawRect );
  143.     ofx= tpoint.x-opoint.x;
  144.     ofy= tpoint.y-opoint.y;
  145.     for( surf= 0; surf<12; surf++ ) {
  146.         TLine( &black, point.x-ofx, point.y-ofy, point.x+ofx, point.y+ofy ).RenderOn( gr );
  147.         iter.x+= (e2.x-e1.x)/12;
  148.         iter.y+= (e2.y-e1.y)/12;
  149.         iter.z+= (e2.z-e1.z)/12;
  150.         point= P3dT2d( iter, drawRect );
  151.     }
  152.     TLine( &black, point.x-ofx, point.y-ofy, point.x+ofx, point.y+ofy ).RenderOn( gr );
  153. }
  154.  
  155. SInt8 TConicContent::DrawPlane( TDrawSlate *gr )
  156. {
  157.     Rect drawRect = GetLocalRect();
  158.     p2d point, opoint;
  159.     int surf;
  160.     TColorStyle plane( 0, 0x8000, 0 );
  161.  
  162.     DrawPlaneAxis( gr );
  163.     point = P3dT2d(mPlane->mPlane[0], drawRect);
  164.     //gr->SetColor( 0, 0, 0x8000 );
  165.     for (surf = 0;surf<=3;surf++)
  166.     {
  167.         opoint = point;
  168.         point = P3dT2d(mPlane->mPlane[surf], drawRect);
  169.         TLine( &plane, opoint.x, opoint.y, point.x, point.y ).RenderOn( gr );
  170.     }
  171.     opoint = P3dT2d(mPlane->mPlane[0], drawRect);
  172.     TLine( &plane, opoint.x, opoint.y, point.x, point.y ).RenderOn( gr );
  173. }
  174.  
  175. SInt8 TConicContent::DrawAxis( TDrawSlate *gr )
  176. {
  177.     p3d x,y,z,bx,by,bz;
  178.     p2d xax,yax,zax,bxax,byax,bzax;
  179.     unsigned pattern;
  180.     Rect drawRect = GetLocalRect();
  181.     RGBColor col;
  182.     TColorStyle black( 0, 0, 0 );
  183.     TColorStyle xcol( 0, 0x8000, 0xffff );
  184.     TColorStyle ycol( 0, 0, 0xffff );
  185.     TColorStyle zcol( 0xffff, 0, 0xffff );
  186.  
  187.     bx.x = -10; bx.y = 0; bx.z = 0;
  188.     by.x = 0; by.y = -10; by.z = 0;
  189.     bz.x = 0; bz.y = 0; bz.z = -10;
  190.  
  191.     x.x = 10; x.y = 0; x.z = 0;
  192.     y.x = 0; y.y = 10; y.z = 0;
  193.     z.x = 0; z.y = 0; z.z = 10;
  194.  
  195.     bxax = P3dT2d(bx, drawRect);
  196.     xax = P3dT2d(x, drawRect);
  197.     //color 0xff00000
  198.     TLine( &xcol, bxax.x, bxax.y, xax.x, xax.y ).RenderOn( gr );
  199.     TStringShape( &black, xax.x, xax.y, "x" ).RenderOn( gr );
  200.  
  201.     byax = P3dT2d(by, drawRect);
  202.     yax = P3dT2d(y, drawRect);
  203.     //color 0xff00
  204.     //gr->SetColor( 0, 0xffff, 0 );
  205.     TLine( &ycol, byax.x, byax.y, yax.x, yax.y ).RenderOn( gr );
  206.     TStringShape( &black, yax.x, yax.y, "y" ).RenderOn( gr );
  207.  
  208.     bzax = P3dT2d(bz, drawRect);
  209.     zax = P3dT2d(z, drawRect);
  210.     //color 0xffff
  211.     TLine( &zcol, bzax.x, bzax.y, zax.x, zax.y ).RenderOn( gr );
  212.     TStringShape( &black, zax.x, zax.y, "z" ).RenderOn( gr );
  213. }
  214.  
  215. void TConicContent::DrawSelf( TDrawSlate *gr )
  216. {
  217.     TEraseRectShape( GetLocalRect() ).RenderOn( gr );
  218.     DrawConic( gr );
  219.     DrawPlane( gr );
  220.     //gr->SetColor( 0, 0, 0 );
  221. }
  222.  
  223. void TConicContent::HandleOptMouse()
  224. {
  225.     float theta( mTheta.GetAngle() ), phi( mPhi.GetAngle() );
  226.     float angle, y, x, dis;
  227.     CursHandle curs= ::GetCursor( 132 );
  228.     mTheta.SetAngle( 0 );
  229.     mPhi.SetAngle( 270 );
  230.     Rect localRect= GetLocalRect();
  231.     Draw( mWindow );
  232.     Point pt, opt;
  233.     TMouseHandler *mouse= TMouseHandler::SGetMouse();
  234.     mouse->GetPosition( pt );
  235.     mWindow->GlobalToLocal( &pt );
  236.     opt.h= pt.h+1;
  237.     ::SetCursor( *curs );
  238.     do {
  239.         if( (pt.h!=opt.h)||(pt.v!=opt.v) ) {
  240.             y= (pt.v-localRect.top)-(localRect.bottom-localRect.top)/2.0;
  241.             x= (localRect.right-localRect.left)/2.0-(pt.h-localRect.left);
  242.             //Yes, I know, this should all be accomplished with the atan2.  Tell that to that
  243.             //bloody MathLib I've got on my machine.
  244.             if( fabs(fabs(y)-fabs(x))>0.000001 )
  245.                 angle= (atan2( x, y )*180/M_PI);
  246.             else if( (y>0)&&(x>0) )
  247.                 angle= 45;
  248.             else if( (y<0)&&(x<0) )
  249.                 angle= 225;
  250.             else if( (y<0)&&(x>0) )
  251.                 angle= 135;
  252.             else if( (y>0)&&(x<0) )
  253.                 angle= 315;
  254.             dis= sqrt( (x*x)+(y*y) )/(localRect.right-localRect.left)*22;
  255.             mPlane->SetAngle( angle );
  256.             mPlane->SetDistance( dis );
  257.             mConic->CalcIntersect( *mPlane );
  258.             mPlane->SendMessage( (UINT32)kUpdateWindow );
  259.         }
  260.         opt= pt;
  261.         mouse->GetPosition( pt );
  262.         mWindow->GlobalToLocal( &pt );
  263.     } while( mouse->GetButton() );
  264.     mTheta.SetAngle( theta );
  265.     mPhi.SetAngle( phi );
  266.     Draw( mWindow );
  267.     ::SetCursor( mPresCurs );
  268. }
  269.  
  270. void TConicContent::HandleNormMouse( BOOLEAN shift )
  271. {
  272.     Point pt, opt;
  273.     CursHandle curs;
  274.     int dir;
  275.     TMouseHandler *mouse= TMouseHandler::SGetMouse();
  276.  
  277.     mouse->GetPosition( pt );
  278.     if( shift ) {
  279.         dir=0;
  280.         curs= GetCursor( 129 );
  281.     } else {
  282.         dir=3;
  283.         curs= GetCursor( 128 );
  284.     }
  285.     SetCursor( (*curs) );
  286.     while( mouse->GetButton() ) {
  287.         opt= pt;
  288.         mouse->GetPosition( pt );
  289.         if( (pt.h != opt.h) || (pt.v != opt.v) ) {
  290.             if( shift&&!dir ) {
  291.                 if( (pt.h-opt.h)*((pt.h-opt.h)<0?-1:1)>(pt.v-opt.v)*((pt.v-opt.v)<0?-1:1) )
  292.                     dir= 1;
  293.                 else
  294.                     dir= 2;
  295.             }
  296.             if( dir&1 )
  297.                 mTheta.SetAngle( mTheta.GetAngle()-(pt.h-opt.h)/2.0 );
  298.             if( dir&2 )
  299.                 mPhi.SetAngle( mPhi.GetAngle()+(pt.v-opt.v)/2.0 );
  300.             Draw( mWindow );
  301.         }
  302.     }
  303.     ::SetCursor( mPresCurs );
  304. }
  305.  
  306. Boolean TConicContent::HandleMouseSelf( TMouseButtonEvent *ev )
  307. {
  308.     if( !ev->button )
  309.         return( true );
  310.     UINT16 mods= TKeyboardHandler::GetKeyboard()->GetModifiers();
  311.     if( mods&optionKey )
  312.         HandleOptMouse();
  313.     else if( mHasDragDrop&&(mods&cmdKey) )
  314.         TDocContent::HandleMouseSelf( ev );
  315.     else
  316.         HandleNormMouse( (mods&shiftKey)!=0 );
  317.     return( true );
  318. }
  319.  
  320. void TConicContent::HandleMouseEnter()
  321. {
  322.     TNullHandler::GetNullHandler()->AddPulsar( this );
  323.     mState= 32767;
  324. }
  325.  
  326. void TConicContent::HandleMouseExit()
  327. {
  328.     TNullHandler::GetNullHandler()->RemovePulsar( this );
  329.     ::SetCursor( &qd.arrow );
  330. }
  331.  
  332. void TConicContent::Pulse( const TEvent *ev )
  333. {
  334.     UINT16 modifiers= TKeyboardHandler::GetKeyboard()->GetModifiers();
  335.     if( modifiers != mState ) {
  336.         mState= modifiers;
  337.         if( mState&optionKey )
  338.             mPresCurs= *mOptCurs;
  339.         else if( mState&shiftKey )
  340.             mPresCurs= *mShifCurs;
  341.         else if( mHasDragDrop&&(mState&cmdKey) )
  342.             mPresCurs= &qd.arrow;
  343.         else
  344.             mPresCurs= *mNormCurs;
  345.         ::SetCursor( mPresCurs );
  346.     }
  347. }