Смекни!
smekni.com

Разработка программы на языке С для построения сложных геометрических фигур на плоскости (стр. 3 из 3)

this->RedrawWindow();

}

void CMyDlg::OnSize(UINT nType, int cx, int cy)

{

CDialog::OnSize(nType, cx, cy);

this->RedrawWindow();

}

// функция OnSize вызывается при всяком изменении размера окна

void CMyDlg::OnBtnUpdate()

{

if(k_filePath.GetLength() <= 0)return;

if(!k_Draw.LoadParamFromFile(k_filePath.GetString()))

this->MessageBox(_T("Can't load params"));

this->RedrawWindow();

}

Рисование

#include "StdAfx.h"

#include "Draw.h"

#include <Math.h>

inline int sqrt(int arg){return (int)sqrt((double) (arg)); }

CDraw::CDraw(void):

isCorrectState(false)

{

}

CDraw::~CDraw(void)

{

}

bool CDraw::LoadParamFromFile(const TCHAR* path)

{

CStdioFile file;

if(!file.Open(path, CStdioFile::modeRead | CStdioFile::typeText))return false;

CString strs[9];

for(int i = 0; i < 9; i++)

{

if(!file.ReadString(strs[i]))

{

file.Close();

return false;

}

}

int i = 0;

X1 = _tstoi(strs[i++].GetString());

Y1 = _tstoi(strs[i++].GetString());

R1 = _tstoi(strs[i++].GetString());

x2 = _tstoi(strs[i++].GetString());

y2 = _tstoi(strs[i++].GetString());

r2 = _tstoi(strs[i++].GetString());

x3 = _tstoi(strs[i++].GetString());

y3 = _tstoi(strs[i++].GetString());

r3 = _tstoi(strs[i++].GetString());

//test

isCorrectState =

(R1 > 0) &&

(R1 > 2*r2) && (R1 > 2*r3) &&

((x2 - r2) > x3) &&

sqrt((x2 - X1)*(x2 - X1) + (y2 - Y1)*(y2 - Y1)) + r2 < R1 &&

sqrt((x3 - X1)*(x3 - X1) + (y3 - Y1)*(y3 - Y1)) + r3 < R1 ;

return isCorrectState;

}

bool CDraw::Draw(CPaintDC* pDC)

{

if(!isCorrectState)return false;

RECT cr;

pDC->GetWindow()->GetClientRect(&cr);

int x0 = (cr.right - cr.left) /2;

int y0 = (cr.bottom - cr.top) / 2;

pDC->Rectangle(&cr);

pDC->MoveTo(x0, cr.top);

pDC->LineTo(x0, cr.bottom);

pDC->MoveTo(cr.left,y0);

pDC->LineTo(cr.right,y0);

CRect r;

r.SetRect(X1 - R1, -(Y1 - R1), X1 + R1, -(Y1 + R1));

r.OffsetRect(x0, y0);

pDC->Arc(&r, CPoint(r.left, r.top), CPoint(r.left, r.top));

pDC->MoveTo(x2 + x0, r.top + 10);

pDC->LineTo(x2 + x0, r.bottom - 10);

pDC->MoveTo(x3 + x0, r.top + 10);

pDC->LineTo(x3 + x0, r.bottom - 10);

r.SetRect(x2 - r2, -(y2 - r2), x2 + r2, -(y2 + r2));

r.OffsetRect(x0, y0);

pDC->Arc(&r, CPoint(r.left, r.top), CPoint(r.left, r.top));

r.SetRect(x3 - r3, -(y3 - r3), x3 + r3, -(y3 + r3));

r.OffsetRect(x0, y0);

pDC->Arc(&r, CPoint(r.left, r.top), CPoint(r.left, r.top));

CPen pen(PS_SOLID, 7, (COLORREF)(0));

CPen* penLast = pDC->SelectObject(&pen);

pDC->MoveTo(X1 + x0, -Y1 + y0);

pDC->LineTo(X1 + x0, -Y1 + y0);

pDC->MoveTo(x2 + x0, -y2 + y0);

pDC->LineTo(x2 + x0, -y2 + y0);

pDC->MoveTo(x3 + x0, -y3 + y0);

pDC->LineTo(x3 + x0, -y3 + y0);

r.SetRect(x2 - r2, -(y2 - r2), x2 + r2, -(y2 + r2));

r.OffsetRect(x0, y0);

pDC->Arc(&r, CPoint(r.CenterPoint().x, r.bottom), CPoint(r.CenterPoint().x, r.top));

r.SetRect(x3 - r3, -(y3 - r3), x3 + r3, -(y3 + r3));

r.OffsetRect(x0, y0);

pDC->Arc(&r, CPoint(r.CenterPoint().x, r.bottom), CPoint(r.CenterPoint().x, r.top));

//rb

int lu = Y1 + sqrt(R1*R1 - (X1 - x3)*(X1 - x3));

int ru = Y1 + sqrt(R1*R1 - (x2 - X1)*(x2 - X1));

int rd = Y1 -(ru - Y1);

int ld = Y1 -(lu - Y1);

pDC->MoveTo(x2 + x0, -(y2 - r2) + y0);

pDC->LineTo(x2 + x0, -rd + y0);

pDC->MoveTo(x2 + x0, -(y2 + r2) + y0);

pDC->LineTo(x2 + x0, -ru + y0);

pDC->MoveTo(x3 + x0, -(y3 - r3) + y0);

pDC->LineTo(x3 + x0, -ld + y0);

pDC->MoveTo(x3 + x0, -(y3 + r3) + y0);

pDC->LineTo(x3 + x0, -lu + y0);

r.SetRect(X1 - R1, -(Y1 - R1), X1 + R1, -(Y1 + R1));

r.OffsetRect(x0, y0);

pDC->Arc(&r, CPoint(x3 + x0, -ld + y0), CPoint(x2 + x0, -rd + y0));

pDC->Arc(&r, CPoint(x2 + x0, -ru + y0), CPoint(x3 + x0, -lu + y0));

pDC->SelectObject(penLast);

//CRect rto = cr;

//rto.right -= 10;

//rto.bottom -= 10;

//pDC->SetTextColor(0);

//pDC->SetBkColor(RGB(255,255,255));

//pDC->DrawText(_T("4ertenie 4ever!"),-1, &rto, DT_RIGHT | DT_BOTTOM | DT_SINGLELINE );

return true;

}