diff --git a/QT/scan-line/mywindow.cpp b/QT/scan-line/mywindow.cpp index 8265a96..7a62965 100644 --- a/QT/scan-line/mywindow.cpp +++ b/QT/scan-line/mywindow.cpp @@ -130,6 +130,7 @@ void MyWindow::on_cleanButton_clicked() void MyWindow::czysc() { shapes.clear(); + shapes.push_back(Shape()); DrawShapes(); } @@ -303,9 +304,42 @@ void MyWindow::DrawShapes(){ Point p2 = shapes[i].points[0]; DrawLine(p1.x, p1.y, p2.x, p2.y, img); } + std::vector lines; + 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]; + lines.push_back(Line(p1, p2)); + } + for(int y = 0; y < wys; y++){ + std::vector xs = GetIntersectionsWithY(y, lines); + for(int l = 1; l < xs.size(); l+=2){ + DrawLine(xs[l-1], y, xs[l], y, img); + } + } } } +std::vector MyWindow::GetIntersectionsWithY(int y, std::vector lines){ + std::vector res; + for(int i = 0; i < lines.size(); i++){ + Line l = lines[i]; + if(l.A.x < l.B.x) + std::swap(l.A, l.B); +// if(l.A.x < l.B.x) +// std::swap(l.A, l.B); + if((l.A.y < y && l.B.y > y) || (l.A.y > y && l.B.y < y)){ + float d1 = (y - l.A.y); + float d2 = (l.B.y - l.A.y); + float dx = (l.B.x - l.A.x); + float ratio = d2/d1; + int x = l.A.x + dx / ratio; + res.push_back(x); + } + } + std::sort(res.begin(), res.end(), std::greater()); + return res; +} + void MyWindow::ClearImage(QImage *img){ unsigned char* empty_val = (unsigned char*)malloc(4); empty_val[0] = 0; diff --git a/QT/scan-line/mywindow.h b/QT/scan-line/mywindow.h index 1358198..2634fc7 100644 --- a/QT/scan-line/mywindow.h +++ b/QT/scan-line/mywindow.h @@ -147,6 +147,7 @@ private: void ClearImage(QImage *img); void DrawPixel(QImage *img, int x, int y, QColor color = QColor(255, 255, 255)); bool FindPoint(int x, int y, int& pt_id, int& shape_id, float radius = FLT_MAX); + std::vector GetIntersectionsWithY(int y, std::vector lines); // Deklaracje slotow, czyli funkcji wywolywanych // po wystapieniu zdarzen zwiazanych z GUI