Лаба 4

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Лабораторна робота №4

Алгоритми побудови кола

Виконала: студентка
гупи СЗ-181 ФККПІ
Сопун Ірина Миколаївна
Хід роботи
1. Напишіть програму реалізації алгоритму малювання кола
Брезенхема. Координати пишемо із консолі.
#include <windows.h>
#include <iostream>
#include <cmath>
int main()
{
HWND hwnd = GetConsoleWindow();
HDC hdc = GetDC(hwnd);
int h = 200, k = 300, x, y = 100, r, d;
r = y;
d = 3 - 2 * r;
x = 0;
while (x < y)
{
if (d < 0)
{
d = d + 4 * x + 6;
x = x + 1; }
else
{ d = d + 4 * (x - y) + 10;
y = y - 1;
x = x + 1;
}
SetPixel(hdc, x + h, y + k, RGB(255, 0, 0));
SetPixel(hdc, y + h, x + k, RGB(255, 128, 0));
SetPixel(hdc, -y + h, x + k, RGB(255, 247, 0));
SetPixel(hdc, -x + h, y + k, RGB(94, 255, 0));
SetPixel(hdc, -x + h, -y + k, RGB(0, 247, 255));
SetPixel(hdc, -y + h, -x + k, RGB(13, 0, 255));
SetPixel(hdc, y + h, -x + k, RGB(111, 0, 255));
SetPixel(hdc, x + h, -y + k, RGB(255, 0, 200));
}
ReleaseDC(hwnd, hdc);
std::cin.ignore();
return 0;}
2. Напишіть програму для реалізації алгоритму малювання кола з
середньої точки. Координати пишемо із консолі.

#include <windows.h>
#include <iostream>
#include <conio.h>
void drawCircle(HDC hdc, int xc, int yc, int x, int y)
{
COLORREF color = RGB(255, 100, 0);
SetPixel(hdc, xc + x, yc + y, color);
SetPixel(hdc, xc - x, yc + y, color);
SetPixel(hdc, xc + x, yc - y, color);
SetPixel(hdc, xc - x, yc - y, color);
SetPixel(hdc, xc + y, yc + x, color);
SetPixel(hdc, xc - y, yc + x, color);
SetPixel(hdc, xc + y, yc - x, color);
SetPixel(hdc, xc - y, yc - x, color);
}
void circleBres(HDC hdc, int xc, int yc, int r)
{
int x = 0, y = r;
int d = 3 - 2 * r;
drawCircle(hdc, xc, yc, x, y);
while (y >= x)
{
x++;
if (d > 0)
{
y--;
d = d + 4 * (x - y) + 10;
}
else
d = d + 4 * x + 6;
drawCircle(hdc, xc, yc, x, y);
}
}
int main()
{
HWND hwnd = GetConsoleWindow();
HDC hdc = GetDC(hwnd);
int x = 150, y = 150, r = 30;

std::cout << "Drawing circle:\n";


std::cout << "enter x of center ";
std::cin >> x;
std::cout << "enter y of center ";
std::cin >> y;
std::cout << "enter radius ";
std::cin >> r;
system("cls");
circleBres(hdc, x, y, r);
ReleaseDC(hwnd, hdc);
std::cin >> r;
return 0;
}

3. Напишіть програму для створення зображення з ліній,


прямокутників та кіл.

#include <windows.h>
#include <iostream>
#include <cmath>

void DrawCircleElements(HDC hdc, int x, int y, int h, int k) {


SetPixel(hdc, x + h, y + k, RGB(255, 0, 0));
SetPixel(hdc, y + h, x + k, RGB(255, 128, 0));
SetPixel(hdc, -y + h, x + k, RGB(111, 0, 255));
SetPixel(hdc, -x + h, y + k, RGB(255, 0, 200));
SetPixel(hdc, -x + h, -y + k, RGB(0, 247, 255));
SetPixel(hdc, -y + h, -x + k, RGB(13, 0, 255));
SetPixel(hdc, y + h, -x + k, RGB(255, 247, 0));
SetPixel(hdc, x + h, -y + k, RGB(94, 255, 0));
}
void DrawCircle(HDC hdc, int y, int h, int k) {
int r, d;
r = y;
d = 3 - 2 * r;
int x = 0;
while (x < y)
{
if (d < 0)
{
d = d + 4 * x + 6;
x = x + 1;
}
else
{
d = d + 4 * (x - y) + 10;
y = y - 1;
x = x + 1;
}

DrawCircleElements(hdc, x, y, h, k);
}
}
void DrawLine(HDC hdc, int x1, int y1, int x2, int y2) {
int steps, k;
float xincr, yincr, x, y, dx, dy;
dx = x2 - x1;
dy = y2 - y1;
if (abs(dx) > abs(dy))
steps = abs(dx);
else
steps = abs(dy);
xincr = dx / steps;
yincr = dy / steps;
x = x1;
y = y1;
for (k = 1; k <= steps; k++)
{
x += xincr;
y += yincr;
SetPixel(hdc, round(x), round(y), RGB(186, 10, 40));
}
}
void DrawRect(HDC hdc, int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
DrawLine(hdc, x1, y1, x1 + dx, y1);
DrawLine(hdc, x1 + dx, y1, x2, y2);
DrawLine(hdc, x2, y2, x1, y1 + dy);
DrawLine(hdc, x1, y1 + dy, x1, y1);
}
int main()
{
HWND hwnd = GetConsoleWindow();
HDC hdc = GetDC(hwnd);

int h = 200, k = 250, x, y = 50;


DrawCircle(hdc, y, h, k);
DrawCircle(hdc, y, h + 400, k);
DrawLine(hdc, 100, 198, 700, 198);

DrawRect(hdc, 200, 100, 400, 198);


DrawRect(hdc, 401, 50, 601, 198);
DrawRect(hdc, 300, 5, 400, 100);

ReleaseDC(hwnd, hdc);
std::cin.ignore();
return 0;
}

You might also like