2022-12-03 13:41:36 +01:00
|
|
|
// Dolaczamy plik naglowkowy naszej klasy MyWindow
|
|
|
|
#include "mywindow.h"
|
|
|
|
|
|
|
|
// Dolaczamy plik naglowkowy zawierajacy definicje GUI
|
|
|
|
// Plik ten jest generowany automatycznie
|
|
|
|
// z pliku XML "mywindow.ui"
|
|
|
|
#include "ui_mywindow.h"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <QColor>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QColorDialog>
|
|
|
|
#include <iostream>
|
|
|
|
#include <float.h> // for float,double macros
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define PI 3.1415
|
|
|
|
|
|
|
|
// Definicja konstruktora, wywolujemy najpierw
|
|
|
|
// konstruktor klasy nadrzednej, nastepnie tworzymy
|
|
|
|
// obiekt klasy Ui_MyWindow reprezentujacy GUI
|
|
|
|
MyWindow::MyWindow(QWidget *parent) :
|
|
|
|
QMainWindow(parent),
|
|
|
|
ui(new Ui::MyWindow)
|
|
|
|
{
|
|
|
|
// Wywolujemy funkcje tworzaca elementy GUI
|
|
|
|
// Jej definicja znajduje sie w pliku "ui_mywindow.h"
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
// Pobieramy wymiary i wspolrzedne lewego gornego naroznika ramki
|
|
|
|
// i ustawiamy wartosci odpowiednich pol
|
|
|
|
// Uwaga: ramke "rysujFrame" wykorzystujemy tylko do
|
|
|
|
// wygodnego ustaiwenia tych wymiarow. Rysunek bedziemy wyswietlac
|
|
|
|
// bezposrednio w glownym oknie aplikacji.
|
|
|
|
szer = ui->rysujFrame->width();
|
|
|
|
wys = ui->rysujFrame->height();
|
|
|
|
poczX = ui->rysujFrame->x();
|
|
|
|
poczY = ui->rysujFrame->y();
|
|
|
|
|
|
|
|
segment_count = ui->segmentSlider->value();
|
|
|
|
QString str(("Ilosc segmentow: " + std::to_string(segment_count)).c_str());
|
|
|
|
ui->segmentLabel->setText(str);
|
|
|
|
|
|
|
|
// Tworzymy obiekt klasy QImage, o odpowiedniej szerokosci
|
|
|
|
// i wysokosci. Ustawiamy format bitmapy na 32 bitowe RGB
|
|
|
|
// (0xffRRGGBB).
|
|
|
|
img = new QImage(szer,wys,QImage::Format_RGB32);
|
|
|
|
img_tmp = new QImage(szer,wys,QImage::Format_RGB32);
|
|
|
|
active_img = img;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Definicja destruktora
|
|
|
|
MyWindow::~MyWindow()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Funkcja (slot) wywolywana po nacisnieciu przycisku "Wyjscie" (exitButton)
|
|
|
|
// Uwaga: polaczenie tej funkcji z sygnalem "clicked"
|
|
|
|
// emitowanym przez przycisk jest realizowane
|
|
|
|
// za pomoca funkcji QMetaObject::connectSlotsByName(MyWindow)
|
|
|
|
// znajdujacej sie w automatycznie generowanym pliku "ui_mywindow.h"
|
|
|
|
// Nie musimy wiec sami wywolywac funkcji "connect"
|
|
|
|
void MyWindow::on_exitButton_clicked()
|
|
|
|
{
|
|
|
|
// qApp to globalny wskaznik do obiektu reprezentujacego aplikacje
|
|
|
|
// quit() to funkcja (slot) powodujaca zakonczenie aplikacji z kodem 0 (brak bledu)
|
|
|
|
qApp->quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MyWindow::on_segmentSlider_valueChanged(int val)
|
|
|
|
{
|
|
|
|
segment_count = val;
|
|
|
|
QString str(("Ilosc segmentow: " + std::to_string(segment_count)).c_str());
|
|
|
|
ui->segmentLabel->setText(str);
|
2022-12-04 12:44:32 +01:00
|
|
|
DrawBezier();
|
|
|
|
update();
|
2022-12-03 13:41:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Funkcja "odmalowujaca" komponent
|
|
|
|
void MyWindow::paintEvent(QPaintEvent*)
|
|
|
|
{
|
|
|
|
// Obiekt klasy QPainter pozwala nam rysowac na komponentach
|
|
|
|
QPainter p(this);
|
|
|
|
|
|
|
|
// Rysuje obrazek "img" w punkcie (poczX,poczY)
|
|
|
|
// (tu bedzie lewy gorny naroznik)
|
|
|
|
p.drawImage(poczX,poczY,*active_img);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Funkcja (slot) wywolywana po nacisnieciu przycisku "Czysc" (cleanButton)
|
|
|
|
void MyWindow::on_cleanButton_clicked()
|
|
|
|
{
|
|
|
|
// Funkcja czysci (zamalowuje na bialo) obszar rysowania
|
|
|
|
// definicja znajduje sie ponizej
|
|
|
|
czysc();
|
|
|
|
|
|
|
|
// Funkcja "update()" powoduje ponowne "namalowanie" calego komponentu
|
|
|
|
// Wywoluje funkcje "paintEvent"
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Funkcja powoduje wyczyszczenie (zamalowanie na bialo)
|
|
|
|
// obszaru rysowania
|
|
|
|
void MyWindow::czysc()
|
|
|
|
{
|
2022-12-03 14:28:15 +01:00
|
|
|
bezier_points.clear();
|
|
|
|
DrawBezier();
|
2022-12-03 13:41:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Funkcja (slot) wywolywana po nacisnieciu przycisku myszy (w glownym oknie)
|
|
|
|
void MyWindow::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
UpdateTempImage();
|
|
|
|
|
|
|
|
// Pobieramy wspolrzedne punktu klikniecia
|
|
|
|
int x = event->x();
|
|
|
|
int y = event->y();
|
|
|
|
|
|
|
|
// Sa to wspolrzedne wzgledem glownego okna,
|
|
|
|
// Musimy odjac od nich wpolrzedne lewego gornego naroznika rysunku
|
|
|
|
|
|
|
|
x -= poczX;
|
|
|
|
y -= poczY;
|
|
|
|
|
2022-12-03 14:32:26 +01:00
|
|
|
if(x >= szer || y >= wys || x < 0 || y < 0){
|
|
|
|
return;
|
|
|
|
}
|
2022-12-03 13:41:36 +01:00
|
|
|
|
|
|
|
int id = -1;
|
|
|
|
switch(event->button()){
|
|
|
|
case Qt::LeftButton:
|
|
|
|
id = FindPoint(x, y, 20);
|
|
|
|
if(id == -1){
|
|
|
|
mode = Add;
|
|
|
|
bezier_points.push_back(Point(x, y));
|
|
|
|
}else{
|
|
|
|
mode = Move;
|
|
|
|
tmp_point_id = id;
|
|
|
|
bezier_points[id].x = x;
|
|
|
|
bezier_points[id].y = y;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Qt::RightButton:
|
|
|
|
mode = Remove;
|
|
|
|
id = FindPoint(x, y, 20);
|
|
|
|
if(id != -1){
|
|
|
|
bezier_points.erase(bezier_points.begin() + id);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawBezier();
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyWindow::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
// Pobieramy wspolrzedne punktu klikniecia
|
|
|
|
int x = event->x();
|
|
|
|
int y = event->y();
|
|
|
|
|
|
|
|
// Sa to wspolrzedne wzgledem glownego okna,
|
|
|
|
// Musimy odjac od nich wpolrzedne lewego gornego naroznika rysunku
|
|
|
|
|
|
|
|
x -= poczX;
|
|
|
|
y -= poczY;
|
|
|
|
|
|
|
|
if(x >= szer || y >= wys || x < 0 || y < 0){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(mode){
|
|
|
|
case Move:
|
|
|
|
bezier_points[tmp_point_id].x = x;
|
|
|
|
bezier_points[tmp_point_id].y = y;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
DrawBezier();
|
|
|
|
|
|
|
|
// Odswiezamy komponent
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyWindow::mouseReleaseEvent(QMouseEvent *event){
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
int MyWindow::FindPoint(int x, int y, float radius){
|
|
|
|
Point src_point(x, y);
|
|
|
|
float min_dist = FLT_MAX;
|
|
|
|
int min_id = -1;
|
|
|
|
for(int i = 0; i < bezier_points.size(); i++){
|
2022-12-03 14:28:15 +01:00
|
|
|
float dist = (src_point - (Point)(bezier_points[i])).length();
|
2022-12-03 13:41:36 +01:00
|
|
|
if(dist < min_dist && dist < radius){
|
|
|
|
min_id = i;
|
|
|
|
min_dist = dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return min_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyWindow::ApplyTempImage(){
|
|
|
|
memcpy(img->bits(), img_tmp->bits(), szer * wys * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyWindow::UpdateTempImage(){
|
|
|
|
memcpy(img_tmp->bits(), img->bits(), szer * wys * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyWindow::DrawPixel(QImage* img, int x, int y, QColor color){
|
|
|
|
if(x >= szer || y >= wys || x < 0 || y < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned char* ptr = img->bits();
|
|
|
|
|
|
|
|
ptr[szer*4*y + 4*x] = color.blue();
|
|
|
|
ptr[szer*4*y + 4*x + 1] = color.green();
|
|
|
|
ptr[szer*4*y + 4*x + 2] = color.red();
|
|
|
|
ptr[szer*4*y + 4*x + 3] = color.alpha();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyWindow::DrawLine(int x1, int y1, int x2, int y2, QImage *img){
|
|
|
|
if(x1 > x2){
|
|
|
|
std::swap(x1, x2);
|
|
|
|
std::swap(y1, y2);
|
|
|
|
}
|
|
|
|
float diff = x2 - x1;
|
|
|
|
float a = diff != 0 ? (y2 - y1) / diff : FLT_MAX;
|
|
|
|
|
|
|
|
QColor color(255, 255, 255, 255);
|
|
|
|
|
2023-04-14 22:15:14 +02:00
|
|
|
if(abs(a) < 1.0f){
|
2022-12-03 13:41:36 +01:00
|
|
|
for(int x = x1; x <= x2; x++){
|
|
|
|
int x_form = x - x1;
|
|
|
|
int y = a * x_form + y1;
|
|
|
|
|
|
|
|
DrawPixel(img, x, y, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
if(y1 > y2){
|
|
|
|
std::swap(x1, x2);
|
|
|
|
std::swap(y1, y2);
|
|
|
|
}
|
|
|
|
float diff = x2 - x1;
|
|
|
|
float a = diff != 0 ? (y2 - y1) / diff : FLT_MAX;
|
|
|
|
for(int y = y1; y <= y2; y++){
|
|
|
|
int y_form = y - y1;
|
|
|
|
int x = ((float)(y_form) / a) + x1;
|
|
|
|
|
|
|
|
DrawPixel(img, x, y, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MyWindow::DrawCircle(int x1, int y1, int x2, int y2){
|
|
|
|
if(x2 >= szer || y2 >= wys || x2 < 0 || y2 < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
UpdateTempImage();
|
|
|
|
|
|
|
|
float R = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
|
|
|
|
int q_last_x = R / sqrt(2) + 1;
|
|
|
|
|
|
|
|
QColor color(255, 255, 255, 255);
|
|
|
|
|
|
|
|
for(int x = 0; x <= q_last_x; x++){
|
|
|
|
float y = sqrt(pow(R, 2) - pow(x, 2));
|
|
|
|
|
|
|
|
for(int k = 0; k < 2; k++){
|
|
|
|
for(int i = 0; i < 2; i++){
|
|
|
|
for(int j = 0; j < 2; j++){
|
|
|
|
DrawPixel(img, x1 + x, y1 - y, color);
|
|
|
|
y *= -1;
|
|
|
|
}
|
|
|
|
x *= -1;
|
|
|
|
}
|
|
|
|
int tmp = x;
|
|
|
|
x = y;
|
|
|
|
y = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 12:44:32 +01:00
|
|
|
void MyWindow::DrawSquare(int x, int y, int size, QColor color){
|
|
|
|
size /= 2;
|
|
|
|
for(int x1 = x - size; x1 < x + size; x1++){
|
|
|
|
for(int y1 = y - size; y1 < y + size; y1++){
|
|
|
|
if(x1 >= szer || y1 >= wys || x1 < 0 || y1 < 0)
|
|
|
|
continue;
|
|
|
|
DrawPixel(img, x1, y1, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 13:41:36 +01:00
|
|
|
void MyWindow::DrawEllipse(int x1, int y1, int x2, int y2){
|
|
|
|
if(x2 >= szer || y2 >= wys || x2 < 0 || y2 < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
UpdateTempImage();
|
|
|
|
|
|
|
|
float last_x = 0;
|
|
|
|
float last_y = 0;
|
|
|
|
|
|
|
|
float Rx = abs(x2 - x1);
|
|
|
|
float Ry = abs(y2 - y1);
|
|
|
|
|
|
|
|
for(int i = 0; i <= segment_count; i++){
|
|
|
|
double angle = 2.0 * PI * (float(i) / segment_count);
|
|
|
|
|
|
|
|
float x = sin(angle) * Rx;
|
|
|
|
float y = cos(angle) * Ry;
|
|
|
|
|
|
|
|
// if(i > 0)
|
|
|
|
// DrawLine(last_x + x1, last_y + y1, x + x1, y + y1, false);
|
|
|
|
|
|
|
|
last_x = x;
|
|
|
|
last_y = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyWindow::DrawBezier(){
|
|
|
|
ClearImage(img);
|
|
|
|
for(int i = 0; i < bezier_points.size(); i+=1){
|
2022-12-04 12:44:32 +01:00
|
|
|
DrawSquare(bezier_points[i].x, bezier_points[i].y, 15, QColor(255, 0, 0));
|
2022-12-03 13:41:36 +01:00
|
|
|
}
|
|
|
|
for(int i = 1; i < bezier_points.size(); i+=3){
|
2022-12-03 14:28:15 +01:00
|
|
|
if(i + 2 >= bezier_points.size())
|
|
|
|
break;
|
|
|
|
Point p0 = bezier_points[i - 1];
|
|
|
|
Point p1 = bezier_points[i];
|
|
|
|
Point p2 = bezier_points[i + 1];
|
|
|
|
Point p3 = bezier_points[i + 2];
|
2022-12-03 13:41:36 +01:00
|
|
|
|
2022-12-03 14:28:15 +01:00
|
|
|
|
|
|
|
Point last_point = p0;
|
|
|
|
for(int i = 1; i <= segment_count; i++){
|
|
|
|
float t = (float(i) / segment_count);
|
|
|
|
|
|
|
|
Point P =
|
|
|
|
p0 * (float)pow(1.0f - t, 3)
|
|
|
|
+ p1 * 3.0f * (float)pow(1.0f - t, 2) * t
|
|
|
|
+ p2 * 3.0f * (1.0f - t) * (float)pow(t, 2)
|
|
|
|
+ p3 * (float)pow(t, 3);
|
|
|
|
|
|
|
|
DrawLine(last_point.x, last_point.y, P.x, P.y, img);
|
|
|
|
|
|
|
|
last_point = P;
|
2022-12-03 13:41:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyWindow::ClearImage(QImage *img){
|
2022-12-03 14:28:15 +01:00
|
|
|
unsigned char* empty_val = (unsigned char*)malloc(4);
|
|
|
|
empty_val[0] = 0;
|
|
|
|
empty_val[1] = 0;
|
|
|
|
empty_val[2] = 0;
|
|
|
|
empty_val[3] = 255;
|
|
|
|
unsigned char* ptr = img->bits();
|
2022-12-03 13:41:36 +01:00
|
|
|
for(int i = 0; i < img->width(); i++){
|
|
|
|
for(int j = 0; j < img->height(); j++){
|
2022-12-03 14:28:15 +01:00
|
|
|
memcpy(ptr + 4 * (i + j * img->width()), empty_val, 4);
|
2022-12-03 13:41:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|