Added shape generation/rendering to scan-line
This commit is contained in:
parent
f82b43044c
commit
3554862f20
@ -16,6 +16,25 @@
|
|||||||
|
|
||||||
#define PI 3.1415
|
#define PI 3.1415
|
||||||
|
|
||||||
|
bool Intersects(Line a, Line b, Point& p){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Shape::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 < (int)points.size(); i++){
|
||||||
|
float dist = (src_point - (Point)(points[i])).length();
|
||||||
|
if(dist < min_dist && dist < radius){
|
||||||
|
min_id = i;
|
||||||
|
min_dist = dist;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return min_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// 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
|
||||||
@ -41,6 +60,8 @@ MyWindow::MyWindow(QWidget *parent) :
|
|||||||
QString str(("Ilosc segmentow: " + std::to_string(segment_count)).c_str());
|
QString str(("Ilosc segmentow: " + std::to_string(segment_count)).c_str());
|
||||||
ui->segmentLabel->setText(str);
|
ui->segmentLabel->setText(str);
|
||||||
|
|
||||||
|
shapes.push_back(Shape());
|
||||||
|
|
||||||
// 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).
|
||||||
@ -74,7 +95,7 @@ void MyWindow::on_segmentSlider_valueChanged(int val)
|
|||||||
segment_count = val;
|
segment_count = val;
|
||||||
QString str(("Ilosc segmentow: " + std::to_string(segment_count)).c_str());
|
QString str(("Ilosc segmentow: " + std::to_string(segment_count)).c_str());
|
||||||
ui->segmentLabel->setText(str);
|
ui->segmentLabel->setText(str);
|
||||||
DrawSpline();
|
DrawShapes();
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,8 +129,8 @@ void MyWindow::on_cleanButton_clicked()
|
|||||||
// obszaru rysowania
|
// obszaru rysowania
|
||||||
void MyWindow::czysc()
|
void MyWindow::czysc()
|
||||||
{
|
{
|
||||||
bezier_points.clear();
|
shapes.clear();
|
||||||
DrawSpline();
|
DrawShapes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -131,31 +152,39 @@ void MyWindow::mousePressEvent(QMouseEvent *event)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int id = -1;
|
int id = -1;
|
||||||
|
int pt_id, sh_id;
|
||||||
|
bool res;
|
||||||
switch(event->button()){
|
switch(event->button()){
|
||||||
case Qt::LeftButton:
|
case Qt::LeftButton:
|
||||||
id = FindPoint(x, y, 20);
|
res = FindPoint(x, y, pt_id, sh_id, 20);
|
||||||
if(id == -1){
|
if(!res){
|
||||||
mode = Add;
|
mode = Add;
|
||||||
bezier_points.push_back(Point(x, y));
|
shapes[shapes.size()-1].points.push_back(Point(x, y));
|
||||||
|
}else{
|
||||||
|
if(pt_id == 0 && shapes[shapes.size()-1].points.size() >= 3){
|
||||||
|
mode = Add;
|
||||||
|
shapes.push_back(Shape());
|
||||||
}else{
|
}else{
|
||||||
mode = Move;
|
mode = Move;
|
||||||
tmp_point_id = id;
|
tmp_point_id = pt_id;
|
||||||
bezier_points[id].x = x;
|
tmp_shape_id = sh_id;
|
||||||
bezier_points[id].y = y;
|
shapes[sh_id].points[pt_id].x = x;
|
||||||
|
shapes[sh_id].points[pt_id].y = y;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Qt::RightButton:
|
case Qt::RightButton:
|
||||||
mode = Remove;
|
mode = Remove;
|
||||||
id = FindPoint(x, y, 20);
|
res = FindPoint(x, y, pt_id, sh_id, 20);
|
||||||
if(id != -1){
|
if(res){
|
||||||
bezier_points.erase(bezier_points.begin() + id);
|
// bezier_points.erase(bezier_points.begin() + id);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
DrawSpline();
|
DrawShapes();
|
||||||
|
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
@ -178,30 +207,30 @@ void MyWindow::mouseMoveEvent(QMouseEvent *event)
|
|||||||
|
|
||||||
switch(mode){
|
switch(mode){
|
||||||
case Move:
|
case Move:
|
||||||
bezier_points[tmp_point_id].x = x;
|
// bezier_points[tmp_point_id].x = x;
|
||||||
bezier_points[tmp_point_id].y = y;
|
// bezier_points[tmp_point_id].y = y;
|
||||||
|
shapes[tmp_shape_id].points[tmp_point_id].x = x;
|
||||||
|
shapes[tmp_shape_id].points[tmp_point_id].y = y;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
DrawSpline();
|
DrawShapes();
|
||||||
|
|
||||||
// Odswiezamy komponent
|
// Odswiezamy komponent
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
int MyWindow::FindPoint(int x, int y, float radius){
|
bool MyWindow::FindPoint(int x, int y, int& pt_id, int& shape_id, float radius){
|
||||||
Point src_point(x, y);
|
for(int i = 0; i < (int)shapes.size(); i++){
|
||||||
float min_dist = FLT_MAX;
|
int id;
|
||||||
int min_id = -1;
|
if((id = shapes[i].FindPoint(x, y, radius)) != -1){
|
||||||
for(int i = 0; i < (int)bezier_points.size(); i++){
|
shape_id = i;
|
||||||
float dist = (src_point - (Point)(bezier_points[i])).length();
|
pt_id = id;
|
||||||
if(dist < min_dist && dist < radius){
|
return true;
|
||||||
min_id = i;
|
|
||||||
min_dist = dist;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return min_id;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MyWindow::DrawPixel(QImage* img, int x, int y, QColor color){
|
void MyWindow::DrawPixel(QImage* img, int x, int y, QColor color){
|
||||||
@ -261,41 +290,18 @@ void MyWindow::DrawSquare(int x, int y, int size, QColor color){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MyWindow::DrawSpline(){
|
void MyWindow::DrawShapes(){
|
||||||
ClearImage(img);
|
ClearImage(img);
|
||||||
for(int i = 0; i < bezier_points.size(); i+=1){
|
for(int i = 0; i < (int)shapes.size(); i++){
|
||||||
DrawSquare(bezier_points[i].x, bezier_points[i].y, 15, QColor(255, 0, 0));
|
for(int j = 0; j < (int)shapes[i].points.size() - 1; j++){
|
||||||
|
Point p1 = shapes[i].points[j];
|
||||||
|
Point p2 = shapes[i].points[j+1];
|
||||||
|
DrawLine(p1.x, p1.y, p2.x, p2.y, img);
|
||||||
}
|
}
|
||||||
int count = bezier_points.size();
|
if(i != (int)shapes.size() - 1 && (int)shapes[i].points.size() >= 2){
|
||||||
for(int i = 1; i < bezier_points.size() + 2; i++){
|
Point p1 = shapes[i].points[(int)shapes[i].points.size()-1];
|
||||||
Point p0 = bezier_points[std::min(std::max(0, i - 3), count - 1)];
|
Point p2 = shapes[i].points[0];
|
||||||
Point p1 = bezier_points[std::min(std::max(0, i - 2), count - 1)];
|
DrawLine(p1.x, p1.y, p2.x, p2.y, img);
|
||||||
Point p2 = bezier_points[std::min(std::max(0, i - 1), count - 1)];
|
|
||||||
Point p3 = bezier_points[std::min(std::max(0, i), count - 1)];
|
|
||||||
|
|
||||||
|
|
||||||
Point last_point = p0;
|
|
||||||
for(int i = 0; i <= segment_count; i++){
|
|
||||||
float t = (float(i) / segment_count);
|
|
||||||
|
|
||||||
float t2 = (float)pow(t, 2);
|
|
||||||
float t3 = (float)pow(t, 3);
|
|
||||||
|
|
||||||
float w1 = (-1.0f * t3 + 3.0f * t2 - 3.0f * t + 1.0f) / 6.0f;
|
|
||||||
float w2 = (3.0f * t3 - 6.0f * t2 + 4.0f) / 6.0f;
|
|
||||||
float w3 = (-3.0f * t3 + 3.0f * t2 + 3.0f * t + 1.0f) / 6.0f;
|
|
||||||
float w4 = t3 / 6.0f;
|
|
||||||
|
|
||||||
Point P =
|
|
||||||
p0 * w1
|
|
||||||
+ p1 * w2
|
|
||||||
+ p2 * w3
|
|
||||||
+ p3 * w4;
|
|
||||||
|
|
||||||
if(i > 0)
|
|
||||||
DrawLine(last_point.x, last_point.y, P.x, P.y, img);
|
|
||||||
|
|
||||||
last_point = P;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,11 @@ namespace Ui {
|
|||||||
struct Point{
|
struct Point{
|
||||||
int x, y;
|
int x, y;
|
||||||
|
|
||||||
|
Point(){
|
||||||
|
this->x=0;
|
||||||
|
this->y=0;
|
||||||
|
}
|
||||||
|
|
||||||
Point(int x, int y){
|
Point(int x, int y){
|
||||||
this->x=x;
|
this->x=x;
|
||||||
this->y=y;
|
this->y=y;
|
||||||
@ -52,10 +57,6 @@ struct Point{
|
|||||||
|
|
||||||
Point operator-(const Point& other){
|
Point operator-(const Point& other){
|
||||||
return Point(this->x - other.x, this->y - other.y);
|
return Point(this->x - other.x, this->y - other.y);
|
||||||
// float x = other.x - this->x;
|
|
||||||
// float y = other.y - this->y;
|
|
||||||
// float mag = std::sqrt(std::pow(x, 2) + std::pow(y, 2));
|
|
||||||
// return mag;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Point operator*(const float mul){
|
Point operator*(const float mul){
|
||||||
@ -67,6 +68,23 @@ struct Point{
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct Shape{
|
||||||
|
std::vector<Point> points;
|
||||||
|
|
||||||
|
int FindPoint(int x, int y, float radius = FLT_MAX);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Line{
|
||||||
|
Point A,B;
|
||||||
|
|
||||||
|
Line(Point a, Point b){
|
||||||
|
A = a;
|
||||||
|
B = b;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
bool Intersects(Line a, Line b, Point& p);
|
||||||
|
|
||||||
// MyWindow jest podklasa klasy QMainWindow.
|
// MyWindow jest podklasa klasy QMainWindow.
|
||||||
class MyWindow : public QMainWindow
|
class MyWindow : public QMainWindow
|
||||||
{
|
{
|
||||||
@ -113,9 +131,10 @@ private:
|
|||||||
bool draw_finished;
|
bool draw_finished;
|
||||||
int segment_count;
|
int segment_count;
|
||||||
|
|
||||||
std::vector<Point> bezier_points;
|
std::vector<Shape> shapes;
|
||||||
|
|
||||||
int tmp_point_id = 0;
|
int tmp_point_id = 0;
|
||||||
|
int tmp_shape_id = 0;
|
||||||
|
|
||||||
// Deklaracje funkcji
|
// Deklaracje funkcji
|
||||||
void czysc();
|
void czysc();
|
||||||
@ -124,10 +143,10 @@ private:
|
|||||||
|
|
||||||
void DrawLine(int x1, int y1, int x2, int y2, QImage *img);
|
void DrawLine(int x1, int y1, int x2, int y2, QImage *img);
|
||||||
void DrawSquare(int x, int y, int size, QColor color = QColor(255,255,255));
|
void DrawSquare(int x, int y, int size, QColor color = QColor(255,255,255));
|
||||||
void DrawSpline();
|
void DrawShapes();
|
||||||
void ClearImage(QImage *img);
|
void ClearImage(QImage *img);
|
||||||
void DrawPixel(QImage *img, int x, int y, QColor color = QColor(255, 255, 255));
|
void DrawPixel(QImage *img, int x, int y, QColor color = QColor(255, 255, 255));
|
||||||
int FindPoint(int x, int y, float radius = FLT_MAX);
|
bool FindPoint(int x, int y, int& pt_id, int& shape_id, float radius = FLT_MAX);
|
||||||
|
|
||||||
// Deklaracje slotow, czyli funkcji wywolywanych
|
// Deklaracje slotow, czyli funkcji wywolywanych
|
||||||
// po wystapieniu zdarzen zwiazanych z GUI
|
// po wystapieniu zdarzen zwiazanych z GUI
|
||||||
|
Loading…
Reference in New Issue
Block a user