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

@ -1,20 +1,20 @@
#-------------------------------------------------
#
# Project created by QtCreator 2015-03-03T00:14:51
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = piksele
TEMPLATE = app
OUTPUT += Console
SOURCES += main.cpp\
mywindow.cpp
HEADERS += mywindow.h
FORMS += mywindow.ui
#-------------------------------------------------
#
# Project created by QtCreator 2015-03-03T00:14:51
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = linia
TEMPLATE = app
OUTPUT += Console
SOURCES += main.cpp\
mywindow.cpp
HEADERS += mywindow.h
FORMS += mywindow.ui

View File

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