Fixed line project

This commit is contained in:
Dawid Pietrykowski 2022-11-23 23:37:50 +01:00
parent 2da49f4b90
commit baaf46a56f
3 changed files with 57 additions and 43 deletions

View File

@ -8,7 +8,7 @@ QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = piksele TARGET = linia
TEMPLATE = app TEMPLATE = app
OUTPUT += Console OUTPUT += Console

View File

@ -5,9 +5,10 @@
// Plik ten jest generowany automatycznie // Plik ten jest generowany automatycznie
// z pliku XML "mywindow.ui" // z pliku XML "mywindow.ui"
#include "ui_mywindow.h" #include "ui_mywindow.h"
#include <QColor>
#include <QDebug> #include <QDebug>
#include <iostream> #include <iostream>
#include<float.h> // for float,double macros
// Definicja konstruktora, wywolujemy najpierw // Definicja konstruktora, wywolujemy najpierw
// konstruktor klasy nadrzednej, nastepnie tworzymy // konstruktor klasy nadrzednej, nastepnie tworzymy
// obiekt klasy Ui_MyWindow reprezentujacy GUI // obiekt klasy Ui_MyWindow reprezentujacy GUI
@ -147,7 +148,7 @@ void MyWindow::mouseMoveEvent(QMouseEvent *event)
x -= poczX; x -= poczX;
y -= poczY; y -= poczY;
if(x >= szer || y >= wys || x < 0 || y < 0){ if(x >= szer || y >= wys || x < 0 || y < 0 || draw_finished){
ApplyTempImage(); ApplyTempImage();
active_img = img; active_img = img;
@ -193,31 +194,43 @@ void MyWindow::DrawLine(int x1, int y1, int x2, int y2){
UpdateTempImage(); UpdateTempImage();
unsigned char *ptr = img_tmp->bits();
if(x1 > x2){ if(x1 > x2){
int tmpX = x1; std::swap(x1, x2);
int tmpY = y1; std::swap(y1, y2);
x1 = x2; }
y1 = y2; float diff = x2 - x1;
x2 = tmpX; float a = diff != 0 ? (y2 - y1) / diff : FLT_MAX;
y2 = tmpY;
if(abs(a) < 0.5f){
for(int x = x1; x <= x2; x++){
int x_form = x - x1;
int y = a * x_form + y1;
SetColor(ptr, QColor(255, 255, 255), x, y);
}
}
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;
SetColor(ptr, QColor(255, 255, 255), x, y);
}
}
} }
void MyWindow::SetColor(unsigned char *ptr, QColor color, int x, int y){
int kolor = 255; ptr[szer*4*y + 4*x] = color.red();
unsigned char *ptr; ptr[szer*4*y + 4*x + 1] = color.green();
int y = y2; ptr[szer*4*y + 4*x + 2] = color.blue();
ptr = img_tmp->bits();
float diff = x2 - x1;
float a = (y2 - y1) / diff;
for(int i = x1; i <= x2; i++){
int x = i;
y = a * (i - x1) + y1;
// Ustawiamy kolor kliknietego piksela na bialy lub czarny
ptr[szer*4*y + 4*x] = kolor;
ptr[szer*4*y + 4*x + 1] = kolor;
ptr[szer*4*y + 4*x + 2] = kolor;
ptr[szer*4*y + 4*x + 3] = 255; ptr[szer*4*y + 4*x + 3] = 255;
} }
}

View File

@ -84,6 +84,7 @@ private:
void DrawLine(int x1, int y1, int x2, int y2); void DrawLine(int x1, int y1, int x2, int y2);
void UpdateTempImage(); void UpdateTempImage();
void ApplyTempImage(); void ApplyTempImage();
void SetColor(unsigned char *ptr, QColor color, int x, int y);
// Deklaracje slotow, czyli funkcji wywolywanych // Deklaracje slotow, czyli funkcji wywolywanych
// po wystapieniu zdarzen zwiazanych z GUI // po wystapieniu zdarzen zwiazanych z GUI