Связь и интернет Архив Программирование
   
Сделать стартовойСделать закладку            
   ПОИСК  
   
Главная / Алгоритмы / Математика / Вычислительная геометрия / Уравнения различных фигур и их составление по разным данным /
8  Perl
8  PHP
8  JavaScript
8  HTML
8  DHTML
8  XML
8  CSS
8  C / C++
8  Pascal и Delphi
8  Турбо Ассемблер
8  MySQL
8  CASE-технологии
8  Алгоритмы
8  Python
8  Обратная связь
8  Гостевая книга
Новости о мире


Окружность по трем точкам 2D - Программирование от RIN.RU
Окружность по трем точкам 2D



Исходник


// Circle.cpp: implementation of the Circle class.
//
//////////////////////////////////////////////////////////////////////


#include "stdafx.h"
#include "MediSurf.h"
#include "Circle.h"


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


Circle::Circle()
{
this->m_dRadius=-1; // error checking
}


Circle::~Circle()
{


}


Circle::Circle(Point *V1, Point *V2, Point *V3)
{
this->m_dRadius=-1; // error checking


Point *pt1=new Point; Point *pt2=new Point; Point *pt3=new Point;


pt1->Copy(V1); pt2->Copy(V2); pt3->Copy(V3);


if (!this->IsPerpendicular(pt1, pt2, pt3) ) this->CalcCircle(pt1, pt2, pt3);
else if (!this->IsPerpendicular(pt1, pt3, pt2) ) this->CalcCircle(pt1, pt3, pt2);
else if (!this->IsPerpendicular(pt2, pt1, pt3) ) this->CalcCircle(pt2, pt1, pt3);
else if (!this->IsPerpendicular(pt2, pt3, pt1) ) this->CalcCircle(pt2, pt3, pt1);
else if (!this->IsPerpendicular(pt3, pt2, pt1) ) this->CalcCircle(pt3, pt2, pt1);
else if (!this->IsPerpendicular(pt3, pt1, pt2) ) this->CalcCircle(pt3, pt1, pt2);
else {
TRACE("\nThe three pts are perpendicular to axis\n");
// pt1->trace(); pt2->trace(); pt3->trace();
delete pt1; delete pt2; delete pt3;
this->m_dRadius=-1;
return ;
}
delete pt1; delete pt2; delete pt3;


}


bool Circle::IsPerpendicular(Point *pt1, Point *pt2, Point *pt3)
// Check the given point are perpendicular to x or y axis
{
double yDelta_a= pt2->y() - pt1->y();
double xDelta_a= pt2->x() - pt1->x();
double yDelta_b= pt3->y() - pt2->y();
double xDelta_b= pt3->x() - pt2->x();




// TRACE(" yDelta_a: %f xDelta_a: %f \n",yDelta_a,xDelta_a);
// TRACE(" yDelta_b: %f xDelta_b: %f \n",yDelta_b,xDelta_b);


// checking whether the line of the two pts are vertical
if (fabs(xDelta_a) <= 0.000000001 && fabs(yDelta_b) <= 0.000000001){
TRACE("The points are pependicular and parallel to x-y axis\n");
return false;
}


if (fabs(yDelta_a) <= 0.0000001){
// TRACE(" A line of two point are perpendicular to x-axis 1\n");
return true;
}
else if (fabs(yDelta_b) <= 0.0000001){
// TRACE(" A line of two point are perpendicular to x-axis 2\n");
return true;
}
else if (fabs(xDelta_a)<= 0.000000001){
// TRACE(" A line of two point are perpendicular to y-axis 1\n");
return true;
}
else if (fabs(xDelta_b)<= 0.000000001){
// TRACE(" A line of two point are perpendicular to y-axis 2\n");
return true;
}
else return false ;
}


double Circle::CalcCircle(Point *pt1, Point *pt2, Point *pt3)
{
double yDelta_a= pt2->y() - pt1->y();
double xDelta_a= pt2->x() - pt1->x();
double yDelta_b= pt3->y() - pt2->y();
double xDelta_b= pt3->x() - pt2->x();


if (fabs(xDelta_a) <= 0.000000001 && fabs(yDelta_b) <= 0.000000001){
TRACE("Calc cirlce \n");
this->m_Center.m_x= 0.5*(pt2->x() + pt3->x());
this->m_Center.m_y= 0.5*(pt1->y() + pt2->y());
this->m_Center.m_z= pt1->z();
this->m_dRadius= length(&m_Center,pt1); // calc. radius
// TRACE(" Center: %f %f %f\n", m_Center.x(), m_Center.y(), m_Center.z());
// TRACE(" radius: %f %f %f\n", length(&m_Center,pt1), length(&m_Center,pt2),length(&m_Center,pt3));


return this->m_dRadius;
}


// IsPerpendicular() assure that xDelta(s) are not zero
double aSlope=yDelta_a/xDelta_a; //
double bSlope=yDelta_b/xDelta_b;
if (fabs(aSlope-bSlope) <= 0.000000001){ // checking whether the given points are colinear.
TRACE("The three pts are colinear\n");
return -1;
}


// calc center
this->m_Center.m_x= (aSlope*bSlope*(pt1->m_y - pt3->m_y) + bSlope*(pt1->m_x + pt2 ->m_x)
- aSlope*(pt2->m_x+pt3->m_x) )/(2* (bSlope-aSlope) );
this->m_Center.m_y = -1*(m_Center.x() - (pt1->x()+pt2->x())/2)/aSlope + (pt1->y()+pt2->y())/2;
this->m_Center.m_z= pt1->m_z;


this->m_dRadius= length(&m_Center,pt1); // calc. radius
// TRACE(" Center: %f %f %f\n", m_Center.x(), m_Center.y(), m_Center.z());
// TRACE(" radius: %f %f %f\n", length(&m_Center,pt1), length(&m_Center,pt2),length(&m_Center,pt3));
return this->m_dRadius;
}


Point* Circle::GetCenter()
{
return &this->m_Center;


}


double Circle::GetRadius()
{
return this->m_dRadius;
}






// Circle.h: interface for the Circle class.
// Circle class.
// Purpose : Represent the circle object
// Input : 3 different points
// Process : Calcuate the radius and center
// Output : Circle
//
// This class originally designed for representation of discretized curvature information
// of sequential pointlist
// KJIST CAD/CAM Ryu, Jae Hun ( ryu@geguri.kjist.ac.kr)
// Last update : 1999. 7. 4
#if !defined(AFX_CIRCLE_H__1EC15131_4038_11D3_8404_00C04FCC7989__INCLUDED_)
#define AFX_CIRCLE_H__1EC15131_4038_11D3_8404_00C04FCC7989__INCLUDED_


#include "Point.h" // Added by ClassView
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


class Circle
{
public:
double GetRadius();
Point* GetCenter();
Circle(Point *p1, Point *p2, Point *p3); // p1, p2, p3 are co-planar
Circle();
virtual ~Circle();


private:
double CalcCircle(Point *pt1, Point *pt2, Point *pt3);
bool IsPerpendicular(Point *pt1, Point *pt2, Point *pt3);
double m_dRadius;
Point m_Center;
};


#endif // !defined(AFX_CIRCLE_H__1EC15131_4038_11D3_8404_00C04FCC7989__INCLUDED_)






<<<  Назад
 1  2 


 8  Комментарии к статье  8 8  Обсудить в чате

 
  
  
    Copyright ©  RIN 2003 - 2004      * Обратная связь