Finished ellipse app
This commit is contained in:
parent
571094bc01
commit
55c22881de
@ -6,8 +6,13 @@
|
|||||||
// z pliku XML "mywindow.ui"
|
// z pliku XML "mywindow.ui"
|
||||||
#include "ui_mywindow.h"
|
#include "ui_mywindow.h"
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <QColor>
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QColorDialog>
|
||||||
|
#include <iostream>
|
||||||
|
#include <float.h> // for float,double macros
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#define PI 3.1415
|
#define PI 3.1415
|
||||||
|
|
||||||
@ -32,6 +37,10 @@ MyWindow::MyWindow(QWidget *parent) :
|
|||||||
poczX = ui->rysujFrame->x();
|
poczX = ui->rysujFrame->x();
|
||||||
poczY = ui->rysujFrame->y();
|
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
|
// Tworzymy obiekt klasy QImage, o odpowiedniej szerokosci
|
||||||
// i wysokosci. Ustawiamy format bitmapy na 32 bitowe RGB
|
// i wysokosci. Ustawiamy format bitmapy na 32 bitowe RGB
|
||||||
// (0xffRRGGBB).
|
// (0xffRRGGBB).
|
||||||
@ -60,6 +69,14 @@ void MyWindow::on_exitButton_clicked()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Funkcja "odmalowujaca" komponent
|
// Funkcja "odmalowujaca" komponent
|
||||||
void MyWindow::paintEvent(QPaintEvent*)
|
void MyWindow::paintEvent(QPaintEvent*)
|
||||||
{
|
{
|
||||||
@ -135,6 +152,8 @@ void MyWindow::mousePressEvent(QMouseEvent *event)
|
|||||||
|
|
||||||
draw_finished = false;
|
draw_finished = false;
|
||||||
|
|
||||||
|
mode = event->button() == Qt::LeftButton ? Circle : event->button() == Qt::RightButton ? Ellipse : Other;
|
||||||
|
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,7 +169,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;
|
||||||
@ -162,7 +181,11 @@ void MyWindow::mouseMoveEvent(QMouseEvent *event)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
DrawEllipse(startX, startY, x, y);
|
if(mode == Circle){
|
||||||
|
DrawCircle(startX, startY, x, y);
|
||||||
|
}else if(mode == Ellipse){
|
||||||
|
DrawEllipse(startX, startY, x, y);
|
||||||
|
}
|
||||||
|
|
||||||
// Odswiezamy komponent
|
// Odswiezamy komponent
|
||||||
update();
|
update();
|
||||||
@ -186,56 +209,54 @@ void MyWindow::ApplyTempImage(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MyWindow::UpdateTempImage(){
|
void MyWindow::UpdateTempImage(){
|
||||||
memcpy(img_tmp->bits(), img->bits(), szer * wys * 4);
|
memcpy(img_tmp->bits(), img->bits(), szer * wys * 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Rysuj(unsigned char* ptr, int szer, int wys, int x, int y){
|
void DrawPixel(unsigned char* ptr, int szer, int wys, QColor color, int x, int y){
|
||||||
if(x >= szer || y >= wys || x < 0 || y < 0)
|
if(x >= szer || y >= wys || x < 0 || y < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
int kolor = 255;
|
ptr[szer*4*y + 4*x] = color.blue();
|
||||||
ptr[szer*4*y + 4*x] = kolor;
|
ptr[szer*4*y + 4*x + 1] = color.green();
|
||||||
ptr[szer*4*y + 4*x + 1] = kolor;
|
ptr[szer*4*y + 4*x + 2] = color.red();
|
||||||
ptr[szer*4*y + 4*x + 2] = kolor;
|
ptr[szer*4*y + 4*x + 3] = color.alpha();
|
||||||
ptr[szer*4*y + 4*x + 3] = 255;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MyWindow::DrawLine(int x1, int y1, int x2, int y2, bool update_temp){
|
void MyWindow::DrawLine(int x1, int y1, int x2, int y2, bool update_temp){
|
||||||
|
|
||||||
if(x2 >= szer || y2 >= wys || x2 < 0 || y2 < 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
|
|
||||||
if(update_temp)
|
if(update_temp)
|
||||||
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;
|
|
||||||
x2 = tmpX;
|
|
||||||
y2 = tmpY;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int kolor = 255;
|
|
||||||
unsigned char *ptr;
|
|
||||||
int y = y2;
|
|
||||||
ptr = img_tmp->bits();
|
|
||||||
float diff = x2 - x1;
|
float diff = x2 - x1;
|
||||||
float a = (y2 - y1) / diff;
|
float a = diff != 0 ? (y2 - y1) / diff : FLT_MAX;
|
||||||
for(int i = x1; i <= x2; i++){
|
|
||||||
int x = i;
|
|
||||||
y = a * (i - x1) + y1;
|
|
||||||
|
|
||||||
|
QColor color(255, 255, 255, 255);
|
||||||
|
|
||||||
Rysuj(ptr, szer, wys, x, y);
|
if(abs(a) < 0.5f){
|
||||||
// Ustawiamy kolor kliknietego piksela na bialy lub czarny
|
for(int x = x1; x <= x2; x++){
|
||||||
// ptr[szer*4*y + 4*x] = kolor;
|
int x_form = x - x1;
|
||||||
// ptr[szer*4*y + 4*x + 1] = kolor;
|
int y = a * x_form + y1;
|
||||||
// ptr[szer*4*y + 4*x + 2] = kolor;
|
|
||||||
// ptr[szer*4*y + 4*x + 3] = 255;
|
DrawPixel(ptr, szer, wys, color, 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;
|
||||||
|
|
||||||
|
DrawPixel(ptr, szer, wys, color, x, y);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,13 +273,15 @@ void MyWindow::DrawCircle(int x1, int y1, int x2, int y2){
|
|||||||
float R = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
|
float R = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
|
||||||
int q_last_x = R / sqrt(2) + 1;
|
int q_last_x = R / sqrt(2) + 1;
|
||||||
|
|
||||||
|
QColor color(255, 255, 255, 255);
|
||||||
|
|
||||||
for(int x = 0; x <= q_last_x; x++){
|
for(int x = 0; x <= q_last_x; x++){
|
||||||
float y = sqrt(pow(R, 2) - pow(x, 2));
|
float y = sqrt(pow(R, 2) - pow(x, 2));
|
||||||
|
|
||||||
for(int k = 0; k < 2; k++){
|
for(int k = 0; k < 2; k++){
|
||||||
for(int i = 0; i < 2; i++){
|
for(int i = 0; i < 2; i++){
|
||||||
for(int j = 0; j < 2; j++){
|
for(int j = 0; j < 2; j++){
|
||||||
Rysuj(ptr, szer, wys, x1 + x, y1 - y);
|
DrawPixel(ptr, szer, wys, color, x1 + x, y1 - y);
|
||||||
y *= -1;
|
y *= -1;
|
||||||
}
|
}
|
||||||
x *= -1;
|
x *= -1;
|
||||||
@ -275,20 +298,14 @@ void MyWindow::DrawEllipse(int x1, int y1, int x2, int y2){
|
|||||||
|
|
||||||
UpdateTempImage();
|
UpdateTempImage();
|
||||||
|
|
||||||
int point_count = 60;
|
|
||||||
|
|
||||||
unsigned char *ptr;
|
|
||||||
ptr = img_tmp->bits();
|
|
||||||
|
|
||||||
|
|
||||||
float last_x = 0;
|
float last_x = 0;
|
||||||
float last_y = 0;
|
float last_y = 0;
|
||||||
|
|
||||||
float Rx = abs(x2 - x1);
|
float Rx = abs(x2 - x1);
|
||||||
float Ry = abs(y2 - y1);
|
float Ry = abs(y2 - y1);
|
||||||
|
|
||||||
for(int i = 0; i < point_count; i++){
|
for(int i = 0; i <= segment_count; i++){
|
||||||
double angle = 2.0 * PI * (float(i) / point_count);
|
double angle = 2.0 * PI * (float(i) / segment_count);
|
||||||
|
|
||||||
float x = sin(angle) * Rx;
|
float x = sin(angle) * Rx;
|
||||||
float y = cos(angle) * Ry;
|
float y = cos(angle) * Ry;
|
||||||
|
@ -28,6 +28,8 @@
|
|||||||
// klikniecia, ruch myszka itp.
|
// klikniecia, ruch myszka itp.
|
||||||
#include <QMouseEvent>
|
#include <QMouseEvent>
|
||||||
|
|
||||||
|
enum Mode {Ellipse, Circle, Other};
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
class MyWindow;
|
class MyWindow;
|
||||||
}
|
}
|
||||||
@ -59,6 +61,8 @@ private:
|
|||||||
// Do poszczegolnych elementow GUI odwolujemy sie za pomoca zmiennej "ui"
|
// Do poszczegolnych elementow GUI odwolujemy sie za pomoca zmiennej "ui"
|
||||||
Ui::MyWindow *ui;
|
Ui::MyWindow *ui;
|
||||||
|
|
||||||
|
Mode mode;
|
||||||
|
|
||||||
// Pole przechowujace obrazek
|
// Pole przechowujace obrazek
|
||||||
QImage *img;
|
QImage *img;
|
||||||
// Pole przechowujace obrazek
|
// Pole przechowujace obrazek
|
||||||
@ -74,6 +78,7 @@ private:
|
|||||||
int poczY;
|
int poczY;
|
||||||
int startX, startY;
|
int startX, startY;
|
||||||
bool draw_finished;
|
bool draw_finished;
|
||||||
|
int segment_count;
|
||||||
|
|
||||||
|
|
||||||
// Deklaracje funkcji
|
// Deklaracje funkcji
|
||||||
@ -93,6 +98,7 @@ private:
|
|||||||
private slots:
|
private slots:
|
||||||
void on_cleanButton_clicked();
|
void on_cleanButton_clicked();
|
||||||
void on_exitButton_clicked();
|
void on_exitButton_clicked();
|
||||||
|
void on_segmentSlider_valueChanged(int val);
|
||||||
void mousePressEvent(QMouseEvent *event);
|
void mousePressEvent(QMouseEvent *event);
|
||||||
void mouseMoveEvent(QMouseEvent *event);
|
void mouseMoveEvent(QMouseEvent *event);
|
||||||
void mouseReleaseEvent(QMouseEvent *event);
|
void mouseReleaseEvent(QMouseEvent *event);
|
||||||
|
@ -42,11 +42,11 @@
|
|||||||
<x>630</x>
|
<x>630</x>
|
||||||
<y>10</y>
|
<y>10</y>
|
||||||
<width>241</width>
|
<width>241</width>
|
||||||
<height>191</height>
|
<height>271</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<property name="title">
|
<property name="title">
|
||||||
<string>Wypełnianie</string>
|
<string>Opcje</string>
|
||||||
</property>
|
</property>
|
||||||
<widget class="QPushButton" name="cleanButton">
|
<widget class="QPushButton" name="cleanButton">
|
||||||
<property name="geometry">
|
<property name="geometry">
|
||||||
@ -74,6 +74,70 @@
|
|||||||
<string>Wyjście</string>
|
<string>Wyjście</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
|
<widget class="QSlider" name="segmentSlider">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>10</x>
|
||||||
|
<y>150</y>
|
||||||
|
<width>221</width>
|
||||||
|
<height>20</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="minimum">
|
||||||
|
<number>4</number>
|
||||||
|
</property>
|
||||||
|
<property name="maximum">
|
||||||
|
<number>100</number>
|
||||||
|
</property>
|
||||||
|
<property name="value">
|
||||||
|
<number>10</number>
|
||||||
|
</property>
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
<property name="tickPosition">
|
||||||
|
<enum>QSlider::TicksAbove</enum>
|
||||||
|
</property>
|
||||||
|
<property name="tickInterval">
|
||||||
|
<number>10</number>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<widget class="QLabel" name="segmentLabel">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>10</x>
|
||||||
|
<y>120</y>
|
||||||
|
<width>131</width>
|
||||||
|
<height>20</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>TextLabel</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<widget class="QLabel" name="label">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>10</x>
|
||||||
|
<y>186</y>
|
||||||
|
<width>221</width>
|
||||||
|
<height>71</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>Przytrzymaj lewy przycisk myszy by utworzyć okrąg.
|
||||||
|
Przytrzymaj prawy przycisk myszy by utworzyć elipse.</string>
|
||||||
|
</property>
|
||||||
|
<property name="textFormat">
|
||||||
|
<enum>Qt::PlainText</enum>
|
||||||
|
</property>
|
||||||
|
<property name="scaledContents">
|
||||||
|
<bool>false</bool>
|
||||||
|
</property>
|
||||||
|
<property name="wordWrap">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
</widget>
|
</widget>
|
||||||
</widget>
|
</widget>
|
||||||
<widget class="QMenuBar" name="menuBar">
|
<widget class="QMenuBar" name="menuBar">
|
||||||
|
Loading…
Reference in New Issue
Block a user