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 >
Wrap
Text File
|
1996-11-13
|
3KB
|
160 lines
//Copyright 1996 Brian+Aidan Cully
//All rights reserved
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <types.h>
#include <QuickDraw.h>
#include <Events.h>
#include "conic.h"
extern p3d rotpoint(p3d,float);
TConic::TConic() {
aconic = 0;
conic = 0;
intersect = 0l;
}
TConic::~TConic() {
if( conic )
delete conic;
if( aconic )
delete aconic;
if( intersect )
delete intersect;
}
void TConic::Init() {
int surf;
p3d cpoint,bcpoint;
conic = new p3d[CONICPARTS+2];
aconic = new p3d[CONICPARTS+2];
intersect = new int[CONICPARTS+2];
cpoint.x = 0;
cpoint.y = 6;
cpoint.z = 6;
bcpoint.x = 0;
bcpoint.y = -6;
bcpoint.z = -6;
for (surf=0;surf<=CONICPARTS;surf+=2) {
aconic[surf] = rotpoint(cpoint,(((float)360)/CONICPARTS*surf));
aconic[surf+1] = rotpoint(bcpoint,(((float)360)/CONICPARTS*surf));
}
}
void TConic::CalcIntersect( const TPlane &plane ) {
float mu,temp;
int lineno,s;
p3d p1,p2,n,d;
n = plane.pnorm;
s = n.x*plane.plane[0].x + n.y*plane.plane[0].y + n.z*plane.plane[0].z;
tnumint = 0;
for(lineno=0;lineno<=CONICPARTS;lineno+=2) {
p1 = aconic[lineno];
p2 = aconic[lineno+1];
conic[lineno] = p1;
conic[lineno+1] = p2;
d.x = p1.x - p2.x;
d.y = p1.y - p2.y;
d.z = p1.z - p2.z;
temp = n.x*d.x + n.y*d.y + n.z*d.z;
if (temp != 0) {
mu = (s - (n.x*p1.x + n.y*p1.y + n.z*p1.z))/temp;
mu = -1*mu;
if ((mu <= 0.5) && (mu >= 0)) {
conic[lineno].x = p1.x + -1*mu*d.x;
conic[lineno].y = p1.y + -1*mu*d.y;
conic[lineno].z = p1.z + -1*mu*d.z;
intersect[tnumint] = lineno;
tnumint++;
}
if ((mu >= 0.5) && (mu <= 1)) {
conic[lineno+1].x = p1.x + -1*mu*d.x;
conic[lineno+1].y = p1.y + -1*mu*d.y;
conic[lineno+1].z = p1.z + -1*mu*d.z;
intersect[tnumint] = lineno + 1;
tnumint++;
}
}
}
}
TPlane::TPlane() {
aplane = 0l;
plane = 0l;
}
TPlane::~TPlane() {
if( aplane )
delete aplane;
if( plane )
delete plane;
}
void TPlane::Init() {
aplane = new p3d[4];
plane = new p3d[4];
pang.SetAngle(0);
pdis = 2;
aplane[0].x = -6; aplane[0].y = -6; aplane[0].z = pdis;
aplane[1].x = 6; aplane[1].y = -6; aplane[1].z = pdis;
aplane[2].x = 6; aplane[2].y = 6; aplane[2].z = pdis;
aplane[3].x = -6; aplane[3].y = 6; aplane[3].z = pdis;
}
void TPlane::SetDistance( float dis ) {
int surf;
pdis = dis;
if( pdis < 0.25 )
pdis = 0.25;
else if( pdis > 7 )
pdis = 7;
for( surf = 0; surf < 4; surf++ )
aplane[surf].z = pdis;
RotPlane();
}
void TPlane::RotPlane() {
int vert;
for( vert=0; vert<4; vert++ ) {
plane[vert].x = aplane[vert].x;
plane[vert].y = GetCosAngle()*aplane[vert].y-GetSinAngle()*aplane[vert].z;
plane[vert].z = GetSinAngle()*aplane[vert].y+GetCosAngle()*aplane[vert].z;
}
CalcNormal();
}
void TPlane::SetAngle( float ang ) {
pang.SetAngle( ang );
RotPlane();
}
void TPlane::CalcNormal() {
p3d v1, v2, n;
v1.x = plane[0].x - plane[1].x;
v1.y = plane[0].y - plane[1].y;
v1.z = plane[0].z - plane[1].z;
v2.x = plane[0].x - plane[2].x;
v2.y = plane[0].y - plane[2].y;
v2.z = plane[0].z - plane[2].z;
n.x = v1.y*v2.z - v1.z*v2.y;
n.y = v1.z*v2.x - v1.x*v2.z;
n.z = v1.x*v2.y - v1.y*v2.x;
pnorm = n;
}