// BemInpDoc.cpp : implementation of the CBemInpDoc class
//

#include "stdafx.h"
#include "math.h"
#include "BemInp.h"

#include "BemInpDoc.h"
#include "geom.h"
#include "addnode.h"
#include "coefdial.h"
#include "addpoint.h"

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

/////////////////////////////////////////////////////////////////////////////
// CBemInpDoc

IMPLEMENT_DYNCREATE(CBemInpDoc, CDocument)

BEGIN_MESSAGE_MAP(CBemInpDoc, CDocument)
	//{{AFX_MSG_MAP(CBemInpDoc)
	ON_COMMAND(ID_GEOM_COEF, OnGeomCoef)
	ON_COMMAND(ID_GEOM_NODE_SAVE, OnGeomNodeSave)
	ON_UPDATE_COMMAND_UI(ID_GEOM_NODE_SAVE, OnUpdateGeomNodeSave)
	ON_COMMAND(ID_INTER_SAVE, OnInterSave)
	ON_UPDATE_COMMAND_UI(ID_INTER_SAVE, OnUpdateInterSave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(CBemInpDoc, CDocument)
	//{{AFX_DISPATCH_MAP(CBemInpDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//      DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

// Note: we add support for IID_IBemInp to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the 
//  dispinterface in the .ODL file.

// {F824B452-6C3E-11D2-A9BB-8AFA7D18B623}
static const IID IID_IBemInp =
{ 0xf824b452, 0x6c3e, 0x11d2, { 0xa9, 0xbb, 0x8a, 0xfa, 0x7d, 0x18, 0xb6, 0x23 } };

BEGIN_INTERFACE_MAP(CBemInpDoc, CDocument)
	INTERFACE_PART(CBemInpDoc, IID_IBemInp, Dispatch)
END_INTERFACE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBemInpDoc construction/destruction

CBemInpDoc::CBemInpDoc()
{
	// TODO: add one-time construction code here

	EnableAutomation();

	AfxOleLockApp();
}

CBemInpDoc::~CBemInpDoc()
{
	AfxOleUnlockApp();
}

BOOL CBemInpDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	m_size.cx=1000;m_size.cy=800;
	memset (&lfnt,0,sizeof(LOGFONT));
	lfnt.lfHeight=-11;
	strcpy (lfnt.lfFaceName,"Arial");
	kx=ky=thick=1.0f;precip=0.0f;

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CBemInpDoc serialization

void CBemInpDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		ar << name << kx << ky << thick;
		ar.Write (&lfnt,sizeof(LOGFONT));
	}
	else
	{
		ar >> name >> kx >> ky >> thick;
		ar.Read (&lfnt,sizeof(LOGFONT));
	}
	Nodes.Serialize(ar);
	Elems.Serialize(ar);
	Wells.Serialize(ar);
	IntNods.Serialize(ar);
	TriElems.Serialize(ar);
}

/////////////////////////////////////////////////////////////////////////////
// CBemInpDoc diagnostics

#ifdef _DEBUG
void CBemInpDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CBemInpDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CBemInpDoc commands

void CBemInpDoc::DeleteContents() 
{
	int i=0;
	while (i<Nodes.GetSize())
	{
		delete Nodes.GetAt(i++);
	}	                     
	Nodes.RemoveAll();
	i=0;
	while (i<Elems.GetSize())
	{
		delete Elems.GetAt(i++);
	}	                     
	Elems.RemoveAll();
	i=0;
	while (i<Wells.GetSize())
	{
		delete Wells.GetAt(i++);
	}	                     
	Wells.RemoveAll();
	i=0;
	while (i<IntNods.GetSize())
	{
		delete IntNods.GetAt(i++);
	}	                     
	IntNods.RemoveAll();
	i=0;
	while (i<TriElems.GetSize())
	{
		delete TriElems.GetAt(i++);
	}	                     
	TriElems.RemoveAll();
	CDocument::DeleteContents();
}

void CBemInpDoc::PlotNod (CDC* pDC,int sz,BOOL IsNum)
{
	int i,n;
	CFont hf,*hfp;
	hf.CreateFontIndirect(&lfnt);
	hfp=pDC->SelectObject(&hf);
	for (i=0;i<Nodes.GetSize();i++)
	{
		CNode* nd=GetNode(i);
		if (IsNum) n=i+1;
		else n=-1;
		nd->PlotNode (pDC,n,sz,gcb);
	}
	pDC->SelectObject(hfp);
	hf.DeleteObject();
}

void CBemInpDoc::PlotIntPt (CDC* pDC,int sz,BOOL IsNum)
{
	int i,n;
	CFont hf,*hfp;
	hf.CreateFontIndirect(&lfnt);
	hfp=pDC->SelectObject(&hf);
	for (i=0;i<IntNods.GetSize();i++)
	{
		CIntPoint* nd=GetIntPt(i);
		if (IsNum) n=i+1;
		else n=-1;
		nd->PlotPt (pDC,n,sz,gcb);
	}
	pDC->SelectObject(hfp);
	hf.DeleteObject();
}

BOOL CBemInpDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	
	// TODO: Add your specialized creation code here
	
	return TRUE;
}

CSize CBemInpDoc::GetDocSize()
{
	return m_size;
}

CNode* CBemInpDoc::GetNode(int i)
{
	return (CNode*)Nodes.GetAt(i);
}

CWell* CBemInpDoc::GetWell(int i)
{
	return (CWell*)Wells.GetAt(i);
}

CElement* CBemInpDoc::GetElement(int i)
{
	return (CElement*)Elems.GetAt(i);
}

CIntPoint* CBemInpDoc::GetIntPt(int i)
{
	return (CIntPoint*)IntNods.GetAt(i);
}

CIntElem* CBemInpDoc::GetIntElm(int i)
{
	return (CIntElem*)TriElems.GetAt(i);
}

CNode* CBemInpDoc::AddNode (float x,float y)
{
	CNode* nd=new CNode(x,y);
	Nodes.Add(nd);
	SetModifiedFlag();
	return nd;
}

CNode* CBemInpDoc::AddNode (CPoint p)
{
	CNode* nd=new CNode(p,gcb);
	Nodes.Add(nd);
	SetModifiedFlag();
	return nd;
}

CIntPoint* CBemInpDoc::AddIntPt (float x,float y)
{
	CIntPoint* nd=new CIntPoint(x,y,gcb);
	IntNods.Add(nd);
	SetModifiedFlag();
	return nd;
}

CIntPoint* CBemInpDoc::AddIntPt (CPoint p)
{
	CIntPoint* nd=new CIntPoint(p,gcb);
	IntNods.Add(nd);
	SetModifiedFlag();
	return nd;
}

CElement* CBemInpDoc::AddElement(int n1, int n2)
{
	CElement* el=new CElement(n1,n2);
	Elems.Add(el);
	SetModifiedFlag();
	return el;
}

CWell* CBemInpDoc::AddWell(CString nam, float x, float y, float dis, float diam)
{
	CWell* wl=new CWell(nam,x,y,gcb,dis,diam);
	Wells.Add(wl);
	SetModifiedFlag();
	return wl;
}

CWell* CBemInpDoc::AddWell(CString nam, CPoint pt, float dis, float diam)
{
	CWell* wl=new CWell(nam,pt,gcb,dis,diam);
	Wells.Add(wl);
	SetModifiedFlag();
	return wl;
}

void CBemInpDoc::NewNods()
{
	CAddNode nd;
	nd.m_stok="Add";nd.m_stcan="Stop";
	for (;;)
	{
		if (nd.DoModal()==IDCANCEL) break;
		AddNode (nd.m_x,nd.m_y);
	}
}

void CBemInpDoc::ScaleNods(CRect rc)
{
	int i;
	float xmin,xmax,ymin,ymax,dx,dxi,dy,dyi,dp1,dp2;
	if (Nodes.GetSize()==0) return;
	CNode* nd=GetNode(0);
	xmin=xmax=nd->xb;ymin=ymax=nd->yb;
	for (i=0;i<Nodes.GetSize();i++)
	{
		nd=GetNode(i);
		if (nd->xb>xmax) xmax=nd->xb;
		if (nd->xb<xmin) xmin=nd->xb;
		if (nd->yb>ymax) ymax=nd->yb;
		if (nd->yb<ymin) ymin=nd->yb;
	}
	dxi=rc.Width()-100;dyi=rc.Height()-100;
	dx=xmax-xmin;dy=ymax-ymin;
	gcb[0]=rc.left+50;gcb[3]=rc.top+50;
	gcb[2]=xmin;gcb[5]=ymax;
	if (fabs(dx)<1e-5 || fabs(dy)<1e-5)
	{
		gcb[1]=100.0f;gcb[4]=-100.0f;
	}
	else
	{
		dp1=fabs(dxi/dx);dp2=fabs(dyi/dy);
		if (dp1<dp2) {gcb[1]=dp1;gcb[4]=-dp1;}
		else {gcb[1]=dp2;gcb[4]=-dp2;}
	}
	m_size.cx=rc.right;m_size.cy=rc.bottom;
}

void CBemInpDoc::OpenNods()
{
	char szFile[256];
	UINT  i, cbString;
	char  chReplace;    /* string separator for szFilter */
	char  szFilter[256],msg[80],*pom,*pf;
	float x,y;
	CString menf;
	CFileDialog fd(TRUE);    
	CStdioFile hf;

    strcpy(szFilter,"Text files (*.TXT)|*.txt|Mapview files(*.BNA)|*.bna|All files (*.*)|*.*|");
    cbString=strlen(szFilter);
	chReplace = szFilter[cbString - 1]; /* retrieve wildcard */
    szFile[0]=0;
    
    for (i = 0; szFilter[i] != '\0'; i++) 
	{
    	if (szFilter[i] == chReplace) szFilter[i] = '\0';
	}

/* Set all structure members to zero. */

	fd.m_ofn.lpstrFilter = szFilter;
	fd.m_ofn.nFilterIndex = 1;
	fd.m_ofn.lpstrFile= szFile;
	fd.m_ofn.nMaxFile = sizeof(szFile);
	fd.m_ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_ENABLEHOOK | OFN_EXPLORER;


	if (fd.DoModal()==IDOK) 
	{
		menf=fd.GetPathName();
		hf.Open(menf,CFile::typeText | CFile::modeRead);
		if (fd.m_ofn.nFilterIndex==1)
		{
     		for (;;)
     		{
     			pf=hf.ReadString (msg,80);
     			if (pf==NULL) break;
     			pom=strtok(msg,", ");x=atof(pom);
     			pom=strtok (NULL,", ");y=atof(pom);
				AddNode (x,y);
			}
		}
		if (fd.m_ofn.nFilterIndex==2)
		{
     		for (;;)
     		{
     			pf=hf.ReadString (msg,80);
     			if (pf==NULL) break;
     			if (msg[0]=='"') continue;
     			pom=strtok(msg,", ");x=atof(pom);
     			pom=strtok (NULL,", ");y=atof(pom);
				AddNode (x,y);
			}
		}	
		hf.Close();
	}
}

int CBemInpDoc::FindNode(CPoint p)
{
	int i,ij;
	ij=-1;
	for (i=0;i<Nodes.GetSize();i++)
	{
		CNode* nd=GetNode(i);
		if (nd->IsEqu(p,gcb)) {ij=i;break;}
	}
	return ij;
}

int CBemInpDoc::FindIntPt(CPoint p)
{
	int i,ij;
	ij=-1;
	for (i=0;i<IntNods.GetSize();i++)
	{
		CIntPoint* nd=GetIntPt(i);
		if (nd->IsEqu(p,gcb)) {ij=i;break;}
	}
	return ij;
}

void CBemInpDoc::DelElem(int in)
{
	CElement* el=GetElement(in);
	delete el;
	Elems.RemoveAt(in);
	SetModifiedFlag();
}

void CBemInpDoc::DelNode(int in)
{
	CNode* nd=GetNode(in);
	Nodes.RemoveAt(in);
	delete nd;
	SetModifiedFlag();
}

void CBemInpDoc::DelWell(int in)
{
	CWell* nd=GetWell(in);
	Wells.RemoveAt(in);
	delete nd;
	SetModifiedFlag();
}

void CBemInpDoc::DelIntPt(int in)
{
	CIntPoint* nd=GetIntPt(in);
	IntNods.RemoveAt(in);
	delete nd;
	SetModifiedFlag();
}

void CBemInpDoc::PlotElem(CDC * pDC, BOOL IsNum)
{
	int i;
	CPoint p[2];
	CPen pred(PS_SOLID,2,RGB(255,0,0)),pbl(PS_SOLID,2,RGB(0,0,255)),pgr(PS_SOLID,2,RGB(0,255,0));
	CPen *ppr;
	CFont hf,*hfp;
	hf.CreateFontIndirect(&lfnt);
	hfp=pDC->SelectObject(&hf);
	ppr=(CPen*)pDC->SelectStockObject(BLACK_PEN);
	for (i=0;i<Elems.GetSize();i++)
	{
		CElement* el=GetElement(i);
		CNode* n1=GetNode(el->nod1);CNode* n2=GetNode(el->nod2);
		n1->TranCoord(gcb);n2->TranCoord(gcb);
		p[0]=n1->Coord();p[1]=n2->Coord();
		switch (el->type)
		{
		case 1:
			pDC->SelectObject(&pred);
			break;
		case 2:
			pDC->SelectObject(&pbl);
			break;
		case 3:
			pDC->SelectObject(&pgr);
			break;
		}
		pDC->Polygon(p,2);
		if (IsNum)
		{
			int ix,iy;
			ix=(p[0].x+p[1].x)/2;iy=(p[0].y+p[1].y)/2;
			CString msg;
			msg.Format("E%d",i+1);
			UINT tap=pDC->SetTextAlign(TA_CENTER | TA_BOTTOM);
			pDC->TextOut (ix,iy,msg);
			pDC->SetTextAlign(tap);
		}
		pDC->SelectStockObject(BLACK_PEN);
	}
	pDC->SelectObject(hfp);
	pDC->SelectObject(ppr);
	hf.DeleteObject();
	pred.DeleteObject();pbl.DeleteObject();pgr.DeleteObject();
}

void CBemInpDoc::OnGeomCoef() 
{
	CCoefDial cdl;
	cdl.m_kx=kx;cdl.m_ky=ky;cdl.m_thick=thick;
	if (cdl.DoModal()==IDOK)
	{
		kx=cdl.m_kx;ky=cdl.m_ky;thick=cdl.m_thick;
	}
}

void CBemInpDoc::OpenPoints()
{
	char szFile[256];
	UINT  i, cbString;
	char  chReplace;    /* string separator for szFilter */
	char  szFilter[256],msg[80],*pom,*pf;
	float x,y;
	CString menf;
	CFileDialog fd(TRUE);    
	CStdioFile hf;

    strcpy(szFilter,"Text files (*.TXT)|*.txt|Mapview files(*.BNA)|*.bna|All files (*.*)|*.*|");
    cbString=strlen(szFilter);
	chReplace = szFilter[cbString - 1]; /* retrieve wildcard */
    szFile[0]=0;
    
    for (i = 0; szFilter[i] != '\0'; i++) 
	{
    	if (szFilter[i] == chReplace) szFilter[i] = '\0';
	}

/* Set all structure members to zero. */

	fd.m_ofn.lpstrFilter = szFilter;
	fd.m_ofn.nFilterIndex = 1;
	fd.m_ofn.lpstrFile= szFile;
	fd.m_ofn.nMaxFile = sizeof(szFile);
	fd.m_ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_ENABLEHOOK | OFN_EXPLORER;


	if (fd.DoModal()==IDOK) 
	{
		menf=fd.GetPathName();
		hf.Open(menf,CFile::typeText | CFile::modeRead);
		if (fd.m_ofn.nFilterIndex==1)
		{
     		for (;;)
     		{
     			pf=hf.ReadString (msg,80);
     			if (pf==NULL) break;
     			pom=strtok(msg,", ");x=atof(pom);
     			pom=strtok (NULL,", ");y=atof(pom);
				AddIntPt (x,y);
			}
		}
		if (fd.m_ofn.nFilterIndex==2)
		{
     		for (;;)
     		{
     			pf=hf.ReadString (msg,80);
     			if (pf==NULL) break;
     			if (msg[0]=='"') continue;
     			pom=strtok(msg,", ");x=atof(pom);
     			pom=strtok (NULL,", ");y=atof(pom);
				AddIntPt (x,y);
			}
		}	
		hf.Close();
	}
}

void CBemInpDoc::OnGeomNodeSave() 
{
	char szFile[256];
	UINT  cbString;
	int i;
	char  chReplace;    /* string separator for szFilter */
	char  szFilter[256];
	CString menf,msx;
	CFileDialog fd(FALSE);    
	CStdioFile hf;

    strcpy(szFilter,"Nodes (*.TXT)|*.txt|Mapview (*.BNA)|*.bna|všetky (*.*)|*.*|");
    cbString=strlen(szFilter);
	chReplace = szFilter[cbString - 1]; /* retrieve wildcard */
    szFile[0]=0;
    
    for (i = 0; szFilter[i] != '\0'; i++) 
	{
    	if (szFilter[i] == chReplace) szFilter[i] = '\0';
	}

/* Set all structure members to zero. */

	fd.m_ofn.lpstrFilter = szFilter;
	fd.m_ofn.nFilterIndex = 1;
	fd.m_ofn.lpstrFile= szFile;
	fd.m_ofn.nMaxFile = sizeof(szFile);
	fd.m_ofn.Flags = OFN_SHOWHELP | OFN_OVERWRITEPROMPT | OFN_ENABLEHOOK | OFN_EXPLORER;


	if (fd.DoModal()==IDOK) 
	{
		menf=fd.GetPathName();
		msx=fd.GetFileExt();
		if (fd.m_ofn.nFilterIndex==1)
		{
			if (msx.IsEmpty()) menf+=".txt";
			hf.Open(menf,CFile::typeText | CFile::modeWrite | CFile::modeCreate);
			for (i=0;i<Nodes.GetSize();i++)
			{
				CNode* bd=GetNode(i);
				CString str;
				str.Format ("%8.3f %8.3f\n",bd->xb,bd->yb);
				hf.WriteString(str);
			}
		}
		if (fd.m_ofn.nFilterIndex==2)
		{
			if (msx.IsEmpty()) menf+=".bna";
			hf.Open(menf,CFile::typeText | CFile::modeWrite | CFile::modeCreate);
			for (i=0;i<Nodes.GetSize();i++)
			{
				CNode* bd=GetNode(i);
				CString str;
				str.Format ("\"%d\"\n",i+1);
				hf.WriteString(str);
				str.Format ("%8.3f %8.3f\n",bd->xb,bd->yb);
				hf.WriteString(str);
			}
		}
		hf.Close();
	}
}

void CBemInpDoc::OnUpdateGeomNodeSave(CCmdUI* pCmdUI) 
{
	if (Nodes.GetSize()>0) pCmdUI->Enable(1);
	else pCmdUI->Enable(0);
}

void CBemInpDoc::OnInterSave() 
{
	char szFile[256];
	UINT  cbString;
	int i;
	char  chReplace;    /* string separator for szFilter */
	char  szFilter[256];
	CString menf,msx;
	CFileDialog fd(FALSE);    
	CStdioFile hf;

    strcpy(szFilter,"Nodes (*.TXT)|*.txt|Mapview (*.BNA)|*.bna|všetky (*.*)|*.*|");
    cbString=strlen(szFilter);
	chReplace = szFilter[cbString - 1]; /* retrieve wildcard */
    szFile[0]=0;
    
    for (i = 0; szFilter[i] != '\0'; i++) 
	{
    	if (szFilter[i] == chReplace) szFilter[i] = '\0';
	}

/* Set all structure members to zero. */

	fd.m_ofn.lpstrFilter = szFilter;
	fd.m_ofn.nFilterIndex = 1;
	fd.m_ofn.lpstrFile= szFile;
	fd.m_ofn.nMaxFile = sizeof(szFile);
	fd.m_ofn.Flags = OFN_SHOWHELP | OFN_OVERWRITEPROMPT | OFN_ENABLEHOOK | OFN_EXPLORER;


	if (fd.DoModal()==IDOK) 
	{
		menf=fd.GetPathName();
		msx=fd.GetFileExt();
		if (fd.m_ofn.nFilterIndex==1)
		{
			if (msx.IsEmpty()) menf+=".txt";
			hf.Open(menf,CFile::typeText | CFile::modeWrite | CFile::modeCreate);
			for (i=0;i<IntNods.GetSize();i++)
			{
				CIntPoint* bd=GetIntPt(i);
				CString str;
				str.Format ("%8.3f %8.3f\n",bd->xz,bd->yz);
				hf.WriteString(str);
			}
		}
		if (fd.m_ofn.nFilterIndex==2)
		{
			if (msx.IsEmpty()) menf+=".bna";
			hf.Open(menf,CFile::typeText | CFile::modeWrite | CFile::modeCreate);
			for (i=0;i<IntNods.GetSize();i++)
			{
				CIntPoint* bd=GetIntPt(i);
				CString str;
				str.Format ("\"%d\"\n",i+1);
				hf.WriteString(str);
				str.Format ("%8.3f %8.3f\n",bd->xz,bd->yz);
				hf.WriteString(str);
			}
		}
		hf.Close();
	}
}

void CBemInpDoc::OnUpdateInterSave(CCmdUI* pCmdUI) 
{
	if (IntNods.GetSize()>0) pCmdUI->Enable(1);
	else pCmdUI->Enable(0);
}

int CBemInpDoc::FindElem(CPoint pt)
{
  	int i,ip,j1,j2,i1,i2,k1,k2,cux,cuy;
  	long kk;
  	int ix,iy;
  	CPoint bd1,bd2;
  	float xo1,xo2,yo1,yo2,xp,yp,p,q,dx,px[2],py[2],
  		xu1,yu1,xu2,yu2;
  	cux=cuy=3;	
  	ix=pt.x;iy=pt.y;
  	i=ix-cux;xo1=gcb[2]+(i-gcb[0])/gcb[1];
  	i=ix+cux;xo2=gcb[2]+(i-gcb[0])/gcb[1];
  	i=iy-cuy;yo1=gcb[5]+(i-gcb[3])/gcb[4];
  	i=iy+cuy;yo2=gcb[5]+(i-gcb[3])/gcb[4];
  	for (i=0;i<Elems.GetSize();i++)
  	{   
  		CElement* prv=GetElement(i);
    	ip=0;
    	i1=prv->nod1;i2=prv->nod2;
    	CNode* uzl1=GetNode(i1);CNode* uzl2=GetNode(i2);
    	bd1=uzl1->Coord();bd2=uzl2->Coord();
    	uzl1->Coord (xu1,yu1);uzl2->Coord (xu2,yu2);
    	j1=ix-bd1.x;j2=ix-bd2.x;
    	if (j1==j2)                 // vertical elem.
    	{
     		if (xu1<xo1||xu1>xo2) continue;
     		k1=iy-bd1.y;k2=iy-bd2.y;
     		kk=(long)k1*(long)k2;
     		if (kk<0) return i;
     		else continue;
    	}
    	j1=iy-bd1.y;j2=iy-bd2.y;
    	if (j1==j2)                 // horizontal elem.
    	{
     		if (yu1>yo1||yu1<yo2) continue;
     		k1=ix-bd1.x;k2=ix-bd2.x;
     		kk=(long)k1*(long)k2;
     		if (kk<0) return i;
     		else continue;
    	}
    	p=(yu2-yu1)/(xu2-xu1);
    	q=yu1-p*xu1;
    	yp=p*xo1+q;
    	if (yp<=yo1&&yp>=yo2)
    	{
     		py[ip]=yp;px[ip]=xo1;ip++;
     		if (ip==2) goto pries;
    	}
    	yp=p*xo2+q;
    	if (yp<=yo1&&yp>=yo2)
    	{
     		py[ip]=yp;px[ip]=xo2;ip++;
     		if (ip==2) goto pries;
    	}
    	xp=(yo1-q)/p;
    	if (xp>=xo1&&xp<=xo2)
    	{
     		py[ip]=yo1;px[ip]=xp;ip++;
     		if (ip==2) goto pries;
    	}
    	xp=(yo2-q)/p;
    	if (xp>=xo1&&xp<=xo2)
    	{
     		py[ip]=yo2;px[ip]=xp;ip++;
     		if (ip==2) goto pries;
    	}
    	continue;
  pries:;
    	dx=(px[0]-xu1)*(px[0]-xu2);
    	if (dx>0) continue;
    	dx=(px[1]-xu1)*(px[1]-xu2);
    	if (dx>0) continue;
    	dx=(py[1]-yu1)*(py[1]-yu2);
    	if (dx>0) continue;
    	dx=(py[0]-yu1)*(py[0]-yu2);
    	if (dx>0) continue;
    	return i;
   	}
   	return -1;
}

void CBemInpDoc::PlotWells(CDC *pDC, int sz)
{
	int i;
	CFont hf,*hfp;
	hf.CreateFontIndirect(&lfnt);
	hfp=pDC->SelectObject(&hf);
	for (i=0;i<Wells.GetSize();i++)
	{
		CWell* nd=GetWell(i);
		nd->Plot (pDC,gcb,sz);
	}
	pDC->SelectObject(hfp);
	hf.DeleteObject();
}

int CBemInpDoc::FindWell(CPoint pt)
{
	int i,ij;
	ij=-1;
	for (i=0;i<Wells.GetSize();i++)
	{
		CWell* nd=GetWell(i);
		if (nd->IsEqu(pt,gcb)) {ij=i;break;}
	}
	return ij;
}

void CBemInpDoc::NewPoint()
{
	CAddPoint nd;
	if (nd.DoModal()==IDOK)
		AddIntPt (nd.m_x,nd.m_y);
}
