Added shape generation/rendering to scan-line

This commit is contained in:
Dawid Pietrykowski 2022-12-17 16:42:09 +01:00
parent f82b43044c
commit 3554862f20
2 changed files with 93 additions and 68 deletions

View File

@ -16,6 +16,25 @@
#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
// konstruktor klasy nadrzednej, nastepnie tworzymy
// 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());
ui->segmentLabel->setText(str);
shapes.push_back(Shape());
// Tworzymy obiekt klasy QImage, o odpowiedniej szerokosci
// i wysokosci. Ustawiamy format bitmapy na 32 bitowe RGB
// (0xffRRGGBB).
@ -74,7 +95,7 @@ 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);
DrawSpline();
DrawShapes();
update();
}
@ -108,8 +129,8 @@ void MyWindow::on_cleanButton_clicked()
// obszaru rysowania
void MyWindow::czysc()
{
bezier_points.clear();
DrawSpline();
shapes.clear();
DrawShapes();
}
@ -131,31 +152,39 @@ void MyWindow::mousePressEvent(QMouseEvent *event)
}
int id = -1;
int pt_id, sh_id;
bool res;
switch(event->button()){
case Qt::LeftButton:
id = FindPoint(x, y, 20);
if(id == -1){
res = FindPoint(x, y, pt_id, sh_id, 20);
if(!res){
mode = Add;
bezier_points.push_back(Point(x, y));
shapes[shapes.size()-1].points.push_back(Point(x, y));
}else{
mode = Move;
tmp_point_id = id;
bezier_points[id].x = x;
bezier_points[id].y = y;
if(pt_id == 0 && shapes[shapes.size()-1].points.size() >= 3){
mode = Add;
shapes.push_back(Shape());
}else{
mode = Move;
tmp_point_id = pt_id;
tmp_shape_id = sh_id;
shapes[sh_id].points[pt_id].x = x;
shapes[sh_id].points[pt_id].y = y;
}
}
break;
case Qt::RightButton:
mode = Remove;
id = FindPoint(x, y, 20);
if(id != -1){
bezier_points.erase(bezier_points.begin() + id);
res = FindPoint(x, y, pt_id, sh_id, 20);
if(res){
// bezier_points.erase(bezier_points.begin() + id);
}
break;
default:
break;
}
DrawSpline();
DrawShapes();
update();
}
@ -178,30 +207,30 @@ void MyWindow::mouseMoveEvent(QMouseEvent *event)
switch(mode){
case Move:
bezier_points[tmp_point_id].x = x;
bezier_points[tmp_point_id].y = y;
// bezier_points[tmp_point_id].x = x;
// 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;
default:
break;
}
DrawSpline();
DrawShapes();
// Odswiezamy komponent
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 < (int)bezier_points.size(); i++){
float dist = (src_point - (Point)(bezier_points[i])).length();
if(dist < min_dist && dist < radius){
min_id = i;
min_dist = dist;
bool MyWindow::FindPoint(int x, int y, int& pt_id, int& shape_id, float radius){
for(int i = 0; i < (int)shapes.size(); i++){
int id;
if((id = shapes[i].FindPoint(x, y, radius)) != -1){
shape_id = i;
pt_id = id;
return true;
}
}
return min_id;
return false;
}
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);
for(int i = 0; i < bezier_points.size(); i+=1){
DrawSquare(bezier_points[i].x, bezier_points[i].y, 15, QColor(255, 0, 0));
}
int count = bezier_points.size();
for(int i = 1; i < bezier_points.size() + 2; i++){
Point p0 = bezier_points[std::min(std::max(0, i - 3), count - 1)];
Point p1 = bezier_points[std::min(std::max(0, i - 2), count - 1)];
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;
for(int i = 0; i < (int)shapes.size(); i++){
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);
}
if(i != (int)shapes.size() - 1 && (int)shapes[i].points.size() >= 2){
Point p1 = shapes[i].points[(int)shapes[i].points.size()-1];
Point p2 = shapes[i].points[0];
DrawLine(p1.x, p1.y, p2.x, p2.y, img);
}
}
}

View File

@ -40,6 +40,11 @@ namespace Ui {
struct Point{
int x, y;
Point(){
this->x=0;
this->y=0;
}
Point(int x, int y){
this->x=x;
this->y=y;
@ -52,10 +57,6 @@ struct Point{
Point operator-(const Point& other){
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){
@ -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.
class MyWindow : public QMainWindow
{
@ -113,9 +131,10 @@ private:
bool draw_finished;
int segment_count;
std::vector<Point> bezier_points;
std::vector<Shape> shapes;
int tmp_point_id = 0;
int tmp_shape_id = 0;
// Deklaracje funkcji
void czysc();
@ -124,10 +143,10 @@ private:
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 DrawSpline();
void DrawShapes();
void ClearImage(QImage *img);
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
// po wystapieniu zdarzen zwiazanych z GUI