WIP Scan line

This commit is contained in:
Dawid Pietrykowski 2022-12-18 19:10:44 +01:00
parent 3554862f20
commit 9dc3611bc5
2 changed files with 35 additions and 0 deletions

View File

@ -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<Line> 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<int> xs = GetIntersectionsWithY(y, lines);
for(int l = 1; l < xs.size(); l+=2){
DrawLine(xs[l-1], y, xs[l], y, img);
}
}
}
}
std::vector<int> MyWindow::GetIntersectionsWithY(int y, std::vector<Line> lines){
std::vector<int> 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<int>());
return res;
}
void MyWindow::ClearImage(QImage *img){
unsigned char* empty_val = (unsigned char*)malloc(4);
empty_val[0] = 0;

View File

@ -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<int> GetIntersectionsWithY(int y, std::vector<Line> lines);
// Deklaracje slotow, czyli funkcji wywolywanych
// po wystapieniu zdarzen zwiazanych z GUI