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 / conic.cpp < prev    next >
Text File  |  1996-11-13  |  3KB  |  160 lines

  1. //Copyright 1996 Brian+Aidan Cully
  2. //All rights reserved
  3.  
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include <types.h>
  9. #include <QuickDraw.h>
  10. #include <Events.h>
  11. #include "conic.h"
  12.  
  13. extern p3d rotpoint(p3d,float);
  14.  
  15. TConic::TConic() {
  16.     aconic = 0;
  17.     conic = 0;
  18.     intersect = 0l;
  19. }
  20.  
  21. TConic::~TConic() {
  22.     if( conic )
  23.         delete conic;
  24.     if( aconic )
  25.         delete aconic;
  26.     if( intersect )
  27.         delete intersect;
  28. }
  29.  
  30. void TConic::Init() {
  31.     int surf;
  32.     p3d cpoint,bcpoint;
  33.  
  34.     conic = new p3d[CONICPARTS+2];
  35.     aconic = new p3d[CONICPARTS+2];
  36.     intersect = new int[CONICPARTS+2];
  37.     cpoint.x = 0;
  38.     cpoint.y = 6;
  39.     cpoint.z = 6;
  40.  
  41.     bcpoint.x = 0;
  42.     bcpoint.y = -6;
  43.     bcpoint.z = -6;
  44.  
  45.     for (surf=0;surf<=CONICPARTS;surf+=2) {
  46.         aconic[surf] = rotpoint(cpoint,(((float)360)/CONICPARTS*surf));
  47.         aconic[surf+1] = rotpoint(bcpoint,(((float)360)/CONICPARTS*surf));
  48.     }
  49. }
  50.  
  51. void TConic::CalcIntersect( const TPlane &plane ) {
  52.     float mu,temp;
  53.     int lineno,s;
  54.     p3d p1,p2,n,d;
  55.  
  56.     n = plane.pnorm;
  57.     s = n.x*plane.plane[0].x + n.y*plane.plane[0].y + n.z*plane.plane[0].z;
  58.     tnumint = 0;
  59.  
  60.     for(lineno=0;lineno<=CONICPARTS;lineno+=2) {
  61.         p1 = aconic[lineno];
  62.         p2 = aconic[lineno+1];
  63.         conic[lineno] = p1;
  64.         conic[lineno+1] = p2;
  65.         d.x = p1.x - p2.x;
  66.         d.y = p1.y - p2.y;
  67.         d.z = p1.z - p2.z;
  68.  
  69.         temp = n.x*d.x + n.y*d.y + n.z*d.z;
  70.         if (temp != 0) {
  71.             mu = (s - (n.x*p1.x + n.y*p1.y + n.z*p1.z))/temp;
  72.             mu = -1*mu;
  73.             if ((mu <= 0.5) && (mu >= 0)) {
  74.                 conic[lineno].x = p1.x + -1*mu*d.x;
  75.                 conic[lineno].y = p1.y + -1*mu*d.y;
  76.                 conic[lineno].z = p1.z + -1*mu*d.z;
  77.                 intersect[tnumint] = lineno;
  78.                 tnumint++;
  79.             }
  80.             if ((mu >= 0.5) && (mu <= 1)) {
  81.                 conic[lineno+1].x = p1.x + -1*mu*d.x;
  82.                 conic[lineno+1].y = p1.y + -1*mu*d.y;
  83.                 conic[lineno+1].z = p1.z + -1*mu*d.z;
  84.                 intersect[tnumint] = lineno + 1;
  85.                 tnumint++;
  86.             }
  87.         }
  88.     }
  89. }
  90.  
  91. TPlane::TPlane() {
  92.     aplane = 0l;
  93.     plane = 0l;
  94. }
  95.  
  96. TPlane::~TPlane() {
  97.     if( aplane )
  98.         delete aplane;
  99.     if( plane )
  100.         delete plane;
  101. }
  102.  
  103. void TPlane::Init() {
  104.     aplane = new p3d[4];
  105.     plane = new p3d[4];
  106.  
  107.     pang.SetAngle(0);
  108.     pdis = 2;
  109.  
  110.     aplane[0].x = -6; aplane[0].y = -6; aplane[0].z = pdis;
  111.     aplane[1].x = 6; aplane[1].y = -6; aplane[1].z = pdis;
  112.     aplane[2].x = 6; aplane[2].y = 6; aplane[2].z = pdis;
  113.     aplane[3].x = -6; aplane[3].y = 6; aplane[3].z = pdis;
  114. }
  115.  
  116. void TPlane::SetDistance( float dis ) {
  117.     int surf;
  118.  
  119.     pdis = dis;
  120.     if( pdis < 0.25 )
  121.         pdis = 0.25;
  122.     else if( pdis > 7 )
  123.         pdis = 7;
  124.     for( surf = 0; surf < 4; surf++ )
  125.         aplane[surf].z = pdis;
  126.     RotPlane();
  127. }
  128.  
  129. void TPlane::RotPlane() {
  130.     int vert;
  131.  
  132.     for( vert=0; vert<4; vert++ ) {
  133.         plane[vert].x = aplane[vert].x;
  134.         plane[vert].y = GetCosAngle()*aplane[vert].y-GetSinAngle()*aplane[vert].z;
  135.         plane[vert].z = GetSinAngle()*aplane[vert].y+GetCosAngle()*aplane[vert].z;
  136.     }
  137.     CalcNormal();
  138. }
  139.  
  140. void TPlane::SetAngle( float ang ) {
  141.     pang.SetAngle( ang );
  142.     RotPlane();
  143. }
  144.  
  145. void TPlane::CalcNormal() {
  146.     p3d v1, v2, n;
  147.  
  148.     v1.x = plane[0].x - plane[1].x;
  149.     v1.y = plane[0].y - plane[1].y;
  150.     v1.z = plane[0].z - plane[1].z;
  151.  
  152.     v2.x = plane[0].x - plane[2].x;
  153.     v2.y = plane[0].y - plane[2].y;
  154.     v2.z = plane[0].z - plane[2].z;
  155.  
  156.     n.x = v1.y*v2.z - v1.z*v2.y;
  157.     n.y = v1.z*v2.x - v1.x*v2.z;
  158.     n.z = v1.x*v2.y - v1.y*v2.x;
  159.     pnorm = n;
  160. }