Click here to Skip to main content
15,885,757 members
Articles / Desktop Programming / MFC

The Diffraction Grating Calculator

Rate me:
Please Sign up or sign in to vote.
4.40/5 (5 votes)
30 Oct 2010GPL38 min read 41.3K   678   6  
An MFC Windows program related to Concave Diffraction Gratings using VTK libraries.
#include "stdafx.h"
#include "Sphere3D.h"
#include "GSLutilities.h"

CSphere3D::CSphere3D(void)
{
   Center.X = 0.0;
   Center.Y = 0.0;
   Center.Z = 0.0;
   Radius = 1.0;
}

CSphere3D::CSphere3D(double dX, double dY, double dZ, double dRadius)
{
   Center.X = dX;
   Center.Y = dY;
   Center.Z = dZ;
   Radius = dRadius;
}

CSphere3D::CSphere3D(CPoint3D& dOrigin, double dRadius)
{
   Center = dOrigin;
   Radius = dRadius;
}

CSphere3D::CSphere3D(const CSphere3D& ObjSrc)
{
   Center = ObjSrc.Center;
   Radius = ObjSrc.Radius;
}

const CSphere3D& CSphere3D::operator=(CSphere3D const& ObjSrc)
{
   if ( this != &ObjSrc ) // Prevent self assignment
   {
      // Build the specific copy
      Center = ObjSrc.Center;
      Radius = ObjSrc.Radius;
   }
   return *this;
}

CSphere3D::~CSphere3D(void)
{
}

bool CSphere3D::IsIntersection(CLine3D &Line)
{
   CPoint3D Diff;
   Diff = Line.Origin - Center;
   double a, b, c;
   a = 1.0;
   b = 2.0 * (CPoint3D::dotprod(Line.Origin,Line.UnitVersor) - CPoint3D::dotprod(Line.UnitVersor,Center));
   c = CPoint3D::dotprod(Diff,Diff) - Radius*Radius;
   double t1, t2;
   t1 = t2 = 0.0;
   return quadratic(a,b,c,t1,t2,0.0,100.0);
}

void CSphere3D::Intersection(CLine3D &Line, CPoint3D& P1, CPoint3D& P2)
{
   if ( !IsIntersection(Line) )
      return;
   CPoint3D Diff;
   Diff = Line.Origin - Center;
   double a, b, c;
   a = 1.0;
   b = 2.0 * (CPoint3D::dotprod(Line.Origin,Line.UnitVersor) - CPoint3D::dotprod(Line.UnitVersor,Center));
   c = CPoint3D::dotprod(Diff,Diff) - Radius*Radius;
   double t1, t2;
   t1 = t2 = 0.0;
   if ( quadratic(a,b,c,t1,t2,0.0,100.0) )
   {
      P1 = Line.MoveTo(t1);
      P2 = Line.MoveTo(t2);
   }
}

bool CSphere3D::IsInside(CPoint3D& P)
{
   bool bres = false;
   if ( (P-Center).Length() < Radius )
      bres = true;
   return bres;
}

bool CSphere3D::IsOnSphere(CPoint3D& P)
{
   bool bres = false;
   if ( fabs((P-Center).Length() - Radius) <= 1.0e-15 )
      bres = true;
   return bres;
}

bool CSphere3D::IsOutside(CPoint3D& P)
{
   bool bres = false;
   if ( (P-Center).Length() > Radius )
      bres = true;
   return bres;
}

double CSphere3D::Volume()
{
   double res = 0.0;
   res = (4.0/3.0) * acos(-1.0) * Radius * Radius * Radius;
   return res;
}

double CSphere3D::Surface()
{
   double res = 0.0;
   res = acos(-1.0) * Radius * Radius;
   return res;
}

double CSphere3D::Diameter()
{
   double res = 0.0;
   res = 2.0 * Radius;
   return res;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
Italy Italy
Senior Software Developer in C/C++ and Oracle.
Ex-physicist holding a Ph.D. on x-ray lasers.

Comments and Discussions