[kandinsky] Fix bugs in fill_polygon and removed the limit of 32 points

This commit is contained in:
Laury
2022-01-13 17:20:53 +01:00
parent a3e2c9732f
commit 77707cd930
3 changed files with 10 additions and 13 deletions

View File

@@ -42,7 +42,6 @@ public:
virtual void pullRect(KDRect rect, KDColor * pixels) = 0;
//Polygon
static const int k_polygonMaxNumberOfPoints = 32;
void fillPolygon(KDCoordinate pointsX[], KDCoordinate pointsY[], int numberOfPoints, KDColor color);
protected:
KDContext(KDPoint origin, KDRect clippingRect);

View File

@@ -11,6 +11,7 @@ void KDContext::fillPolygon(KDCoordinate pointsX[], KDCoordinate pointsY[], int
KDCoordinate right = m_clippingRect.width();
KDCoordinate left = 0;
// We find top and bottom of the polygon
for (int i = 0; i < numberOfPoints; i++) {
if (pointsY[i] < top) {
top = pointsY[i];
@@ -20,6 +21,7 @@ void KDContext::fillPolygon(KDCoordinate pointsX[], KDCoordinate pointsY[], int
}
}
// We update them if needed
if (top < 0) {
top = 0;
}
@@ -29,13 +31,13 @@ void KDContext::fillPolygon(KDCoordinate pointsX[], KDCoordinate pointsY[], int
for (int y=top; y<=bottom; y++) {
int switches=0;
KDCoordinate switchesX[KDContext::k_polygonMaxNumberOfPoints];
KDCoordinate switchesX[numberOfPoints];
int lastPointIndex = numberOfPoints-1;
for (int i=0; i<numberOfPoints; i++) {
if (((pointsY[i]<=y && pointsY[lastPointIndex]>=y) || (pointsY[lastPointIndex]<= y && pointsY[i]>=y)) && pointsY[i] != pointsY[lastPointIndex]) {
if (((pointsY[i]<y && pointsY[lastPointIndex]>=y) || (pointsY[lastPointIndex]<y && pointsY[i]>=y)) &&
pointsY[i] != pointsY[lastPointIndex]) {
switchesX[switches++] = (int) round(pointsX[i]+1.0*(y-pointsY[i])/(pointsY[lastPointIndex]-pointsY[i])*(pointsX[lastPointIndex]-pointsX[i]));
}
lastPointIndex=i;
@@ -60,7 +62,7 @@ void KDContext::fillPolygon(KDCoordinate pointsX[], KDCoordinate pointsY[], int
break;
}
if (switchesX[i+1]>left) {
fillRect( KDRect( switchesX[ i ] , y, switchesX[ i+1 ] - switchesX[ i ], 1 ), color ) ;
fillRect( KDRect(switchesX[ i ], y, switchesX[ i+1 ] - switchesX[ i ], 1 ), color ) ;
}
}
}

View File

@@ -132,23 +132,19 @@ mp_obj_t modkandinsky_fill_circle(size_t n_args, const mp_obj_t * args) {
}
mp_obj_t modkandinsky_fill_polygon(size_t n_args, const mp_obj_t * args) {
KDCoordinate pointsX[KDContext::k_polygonMaxNumberOfPoints];
KDCoordinate pointsY[KDContext::k_polygonMaxNumberOfPoints];
size_t itemLength;
mp_obj_t * items;
mp_obj_get_array(args[0], &itemLength, &items);
KDCoordinate pointsX[itemLength];
KDCoordinate pointsY[itemLength];
if (itemLength < 3) {
mp_raise_ValueError("polygon must have at least 3 points");
}
else if (itemLength > KDContext::k_polygonMaxNumberOfPoints) {
mp_raise_ValueError("polygon is defined by too many points");
}
for(unsigned int i=0; i<itemLength; i++)
{
for(int i=0; i<itemLength; i++) {
mp_obj_t * coordinates;
mp_obj_get_array_fixed_n(items[i], 2, &coordinates);