- •До розрахунково-графічної роботи
- •Завдання
- •Побудувати графік функції
- •Завдання
- •2.1 Розрахунок функціональних залежностей для побудови графіка.
- •2.2 Розрахунок матриці перетворень для рухомого зображення.
- •3. Список ідентифікаторів програми.
- •4. Блок-схеми основної програми побудови графіка та анімації.
- •5. Текст програми.
- •If(first && rotate)
- •6. Результат виконання програми
If(first && rotate)
-
+
if(phi2
> ToRadian(233))
18
rotate
= false;
if(first
&& !rotate)
if(pos.X
> 400 - r)
21
first
= false; rotate
= true; phi2
= ToRadian(130);
if(!first
&& !rotate)
if(pos.X
> 400 + r + k + t/2)
first
= false; rotate
= true; phi2
= ToRadian(45);
5. Текст програми.
// ChildView.cpp : реализация класса CChildView
//
#include "stdafx.h"
#include "ChildView.h"
#include "Math.h"
#include <conio.h>
#include <vector>
#include<iostream>
#include <fstream>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CChildView
CChildView::CChildView()
{
}
CChildView::~CChildView()
{
}
BEGIN_MESSAGE_MAP(CChildView, CWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
// обработчики сообщений CChildView
BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs)
{
if (!CWnd::PreCreateWindow(cs))
return FALSE;
cs.dwExStyle |= WS_EX_CLIENTEDGE;
cs.style &= ~WS_BORDER;
cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS,
::LoadCursor(NULL, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1), NULL);
return TRUE;
}
double ToRadian(double);
int d = 28;
class Vector2
{
public:
double X;
double Y;
Vector2(double x, double y)
{
X = x;
Y = y;
}
Vector2(int a)
{
X = a;
Y = a;
}
Vector2()
{
X = 0;
Y = 0;
}
friend Vector2& operator+(const double&, const Vector2&);
friend Vector2& operator+(const Vector2&, const Vector2&);
};
Vector2& operator+(const double& value, const Vector2& vec)
{
return Vector2(vec.X + value, vec.Y + value);
}
Vector2& operator+(const Vector2& vec1, const Vector2& vec2)
{
return Vector2(vec1.X + vec2.X, vec1.Y + vec2.Y);
}
class Matrix
{
public:
static Vector2 CreateRotation(Vector2 position, double angle, bool rotateLeft)
{
position.X /= sqrt(2.0);
position.Y /= sqrt(2.0);
double matrix[4];
matrix[0] = cos(angle);
matrix[1] = rotateLeft ? -sin(angle) : sin(angle);
matrix[2] = rotateLeft ? sin(angle) : -sin(angle);
matrix[3] = cos(angle);
return Vector2(position.X * matrix[0] + position.Y * matrix[1], position.X * matrix[2] + position.Y * matrix[3]);
}
};
int a = 20, A = 120;
class Square
{
public:
Vector2 Center;
Vector2 vertex[6];
Square()
{
Center = Vector2(0);
}
Square(Vector2 center)
{
Center = center;
}
Vector2* CreateRotation(double angle) //обертання шестикутника
{
vertex[0] = Vector2(Center.X + Matrix::CreateRotation(a / 2, angle + ToRadian(60), false).X, Center.Y + Matrix::CreateRotation(a / 2, angle + ToRadian(60), false).Y);
vertex[1] = Vector2(Center.X + Matrix::CreateRotation(a / 2, angle + ToRadian(120), false).X, Center.Y + Matrix::CreateRotation(a / 2, angle + ToRadian(120), false).Y);
vertex[2] = Vector2(Center.X + Matrix::CreateRotation(a / 2, angle + ToRadian(180), false).X, Center.Y + Matrix::CreateRotation(a / 2, angle + ToRadian(180), false).Y);
vertex[3] = Vector2(Center.X + Matrix::CreateRotation(a / 2, angle + ToRadian(240), false).X, Center.Y + Matrix::CreateRotation(a / 2, angle + ToRadian(240), false).Y);
vertex[4] = Vector2(Center.X + Matrix::CreateRotation(a / 2, angle + ToRadian(300), false).X, Center.Y + Matrix::CreateRotation(a / 2, angle + ToRadian(300), false).Y);
vertex[5] = Vector2(Center.X + Matrix::CreateRotation(a / 2, angle + ToRadian(360), false).X, Center.Y + Matrix::CreateRotation(a / 2, angle + ToRadian(360), false).Y);
return vertex;
}
};
const double PI = 3.1415;
const double kx = 50, ky = 0.005;
const int Cx = 350, Cy = 350;
double phi = 0;
int r = 60, k = 40;
double ToRadian(double x)
{
return (x*PI) / 180;
}
double f(double x)
{
return -1.0 / sin(x);
return sin(x);
}
double F(double x){ return 9 * pow(x, 3) - 1; } //функція
void CChildView::OnPaint()
{
CPaintDC dc(this);
int Xmashn, Ymashn, Xo, Yo, VXL, VXR, VYB, VYT;
double y, Kx, Ky, dXmash, dXreal, dYmash, dYreal, a, b, h, Ymax, Ymin;
VXL = VXR = VYB = VYT = 70;
a = -5;
b = 3;
h = 0.3;
Ymax = F(a);
Ymin = F(a);
for (double x = a; x<b; x = x + h)
{
y = F(x);
if (Ymax<y)
Ymax = y; else;
if (Ymin>y)
Ymin = y; else;
}
RECT rect;
GetClientRect(&rect);
dXreal = b - a; dXmash = rect.right+50 - rect.left;
dYreal = Ymax - Ymin; dYmash = rect.bottom-150 - rect.top;
Ky = (dYmash - VXL - VXR) / dYreal; Kx = (dXmash - VYB - VYT) / dXreal;
Xo = VXL - Kx*a;
Yo = VYT + Ky*Ymax*(1/3);
int Xo1 = Xo, Yo1 = Yo;
if (Xo1<VXL)Xo1 = VXL; if (Xo1>rect.right - VXR)Xo1 = rect.right - VXR;
if (Yo1<VYT)Yo1 = VYT; if (Yo1>rect.bottom - VYB)Yo1 = rect.bottom - VYB;
//-----Рамка----//
//з більшою товщиною
CPen contur_pen; contur_pen.CreatePen(PS_SOLID, 2, RGB(0, 0, 0));
dc.SelectObject(&contur_pen);
dc.MoveTo(Xo1-400, Yo1+55);
dc.LineTo(Xo1 - 100, Yo1 + 55);
dc.LineTo(Xo1 - 100, Yo1 + 210);
dc.LineTo(Xo1 - 400, Yo1 + 210);
dc.LineTo(Xo1 - 400, Yo1 + 55);
//Знову переходимо до стандартного пера:
CPen black; black.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
dc.SelectObject(&black);
//----осі координат start----//
dc.MoveTo(Xo1, Yo1);
dc.LineTo(Xo1, VYT - 50);
dc.MoveTo(Xo1 , VYT-50);
dc.LineTo(Xo1-5, VYT - 20);
dc.MoveTo(Xo1 , VYT-50);
dc.LineTo(Xo1+5, VYT - 20);
const CString Y("Y");
dc.TextOutW(Xo1 - 15, VYT - 50, Y);
dc.MoveTo(Xo1, Yo1);
dc.LineTo(Xo1, rect.bottom - VYB);
dc.MoveTo(Xo1, Yo1);
dc.LineTo(VXL, Yo1);
dc.MoveTo(Xo1, Yo1);
dc.LineTo(rect.right - VXR + 25, Yo1);
dc.MoveTo(rect.right - VXR, Yo1 - 5);
dc.LineTo(rect.right - VXR + 25, Yo1);
dc.MoveTo(rect.right - VXR, Yo1 + 5);
dc.LineTo(rect.right - VXR + 25, Yo1);
const CString X("X");
dc.TextOutW(rect.right - VXR + 25, Yo1 - 20, X);
const CString O("O");
if (Xo1 <= VXL || Xo1 >= rect.right - VXR|| Yo1 <= VYT || Yo1 >= rect.bottom - VYB);
else dc.TextOutW(Xo1 - 15, Yo1 + 5, O);
//----------a i b start--------//
double d = b;
char str[20];
sprintf_s(str, 20, "%5.4lf", d);
CString ss(str);
dc.TextOutW(Xo + d*Kx - 50, Yo1 + 5, ss);//3.000
dc.MoveTo(Xo + b*Kx, Yo1 - 2);
dc.LineTo(Xo + b*Kx, Yo1 + 2);
double H = (b - a) / 8;
d = a;
int w = b / 5;
for (int i = 0; i<8; i++)
{
sprintf_s(str, 20, "%3.4lf", d);
CString A(str);
dc.TextOut(Xo + d*Kx + 2, Yo1 + 5, A);
dc.MoveTo(Xo + d*Kx, Yo1 - 2);
dc.LineTo(Xo + d*Kx, Yo1 + 2);
d += H;
}
//----------a i b end--------//
//----------Ymax i Ymin start--------//
H = (Ymax - Ymin) / 5;
d = Ymin;
for (int i = 0; i<6; i++)
{
dc.MoveTo(Xo1 - 2, Yo - d*Ky);
dc.LineTo(Xo1 + 2, Yo - d*Ky);
d += H;
}
//----------Ymax i Ymin end--------//
//----------Осі координат end--------//
CRect rc;
GetClientRect(rc);
int width = 4; // товщина лінії функції
LOGBRUSH logBrush;
logBrush.lbStyle = BS_SOLID;
logBrush.lbColor = RGB(0, 213, 255); // голубий колір лінії
CPen pen1(PS_DASHDOT | PS_GEOMETRIC | PS_ENDCAP_ROUND, width, &logBrush); // штрих-пунктир лінія //перо геометричне //кінець заокруглений
CPen *pPrevPen = dc.SelectObject(&pen1);
dc.MoveTo(Xo + a*Kx, Yo - Ky*F(a));
dc.LineTo(Xo + a*Kx, Yo - Ky*F(a));
for (double x = a; x<b; x = x + h)
{
Xmashn = Xo + x*Kx;
y = F(x);
Ymashn = Yo - y*Ky;
dc.LineTo(Xmashn, Ymashn);
}
CPen pen(PS_SOLID, 1, RGB(0, 0, 0));
dc.SelectObject(&pen);
//------------------Aнімація-----------//
phi = ToRadian(180);
int r = 60;
int t = 20;
Square square = Square();
Vector2 pos = Vector2(400 + r + k + t / 2, 250);
bool first = true;
bool rotate = true;
double phi2 = ToRadian(45);
CBrush brush;
brush.CreateSolidBrush(RGB(0, 0, 0)); // колір шестикутника
CBrush brush2;
brush2.CreateSolidBrush(RGB(160, 160, 160)); //колір арки
while (true)
{
for (int i = 0; i<2; i++)
{
//---------kolo_1---------
if (i == 0)
{
dc.SelectStockObject(BLACK_BRUSH);
dc.SelectStockObject(BLACK_PEN);
}
else
{
dc.SelectStockObject(WHITE_BRUSH);
dc.SelectStockObject(WHITE_PEN);
}
double teta = PI / 20.0;
if (rotate)
pos = Vector2(400, 250) + Matrix::CreateRotation(Vector2(first ? r + k + t / 2 : r - t / 2), phi2, first ? false : true);
square.Center = pos;
square.CreateRotation(phi);
dc.MoveTo(square.vertex[0].X, square.vertex[0].Y);
for (int j = 0; j<6; j++)
dc.LineTo(square.vertex[j].X, square.vertex[j].Y);
dc.LineTo(square.vertex[0].X, square.vertex[0].Y);
dc.MoveTo(400 + r, 250);
dc.AngleArc(400, 250, r, 0, 180);
dc.MoveTo(400 + r, 250);
dc.LineTo(400 + r + k, 250);
dc.AngleArc(400, 250, r + k, 0, 180);
dc.LineTo(400 - r, 250);
if (i == 1)
{
dc.FloodFill(square.Center.X, square.Center.Y, RGB(255, 255, 255));
dc.FloodFill(400 - r - t / 2, 240, RGB(255, 255, 255));
phi += 2 * teta;
if (rotate)
phi2 += teta / 2;
if (first && rotate)
if (phi2 > ToRadian(233))
{
rotate = false;
}
if (first && !rotate)
{
pos.X += 3;
if (pos.X > 400 - r)
{
first = false;
rotate = true;
phi2 = ToRadian(130);
}
}
if (!first && rotate)
if (phi2 > ToRadian(330))
{
rotate = false;
}
if (!first && !rotate)
{
pos.X += 3;
if (pos.X > 400 + r + k + t / 2)
{
first = true;
rotate = true;
phi2 = ToRadian(45);
}
}
}
if (i == 0)
{
dc.SelectObject(brush);
dc.FloodFill(square.Center.X, square.Center.Y, RGB(0, 0, 0));
dc.SelectObject(brush2);
dc.FloodFill(400 - r - t / 2, 240, RGB(0, 0, 0));
Sleep(180);
}
}
}
}
