{"record":{"id":"d98444df0aaedcf1","repo":"Yalantis/uCrop","slug":"draw-polygon-invalid-specified-point-set-u-u","errorCode":null,"errorMessage":"draw_polygon(): Invalid specified point set (%u,%u,%u,%u).","messagePattern":"draw_polygon\\(\\): Invalid specified point set \\(%u,%u,%u,%u\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":52302,"sourceCode":"        draw_line(nx0,ny1 - 1,nx0,ny0 + 1,color,opacity,pattern,false);\n    }\n\n    //! Draw a filled 2D polygon.\n    /**\n       \\param points Set of polygon vertices.\n       \\param color Pointer to \\c spectrum() consecutive values of type \\c T, defining the drawing color.\n       \\param opacity Drawing opacity.\n     **/\n    template<typename tp, typename tc>\n    CImg<T>& draw_polygon(const CImg<tp>& points,\n                          const tc *const color, const float opacity=1) {\n      if (is_empty() || !points) return *this;\n      if (!color)\n        throw CImgArgumentException(_cimg_instance\n                                    \"draw_polygon(): Specified color is (null).\",\n                                    cimg_instance);\n      if (points.height()!=2)\n        throw CImgArgumentException(_cimg_instance\n                                    \"draw_polygon(): Invalid specified point set (%u,%u,%u,%u).\",\n                                    cimg_instance,\n                                    points._width,points._height,points._depth,points._spectrum);\n      CImg<intT> ipoints;\n      if (cimg::type<tp>::is_float()) ipoints = points.get_round();\n      else ipoints.assign(points,cimg::type<tp>::string()==cimg::type<int>::string());\n\n      if (ipoints._width==1) return draw_point(ipoints(0,0),ipoints(0,1),color,opacity);\n      if (ipoints._width==2) return draw_line(ipoints(0,0),ipoints(0,1),ipoints(1,0),ipoints(1,1),color,opacity);\n      if (ipoints._width==3) return draw_triangle(ipoints(0,0),ipoints(0,1),ipoints(1,0),ipoints(1,1),\n                                                  ipoints(2,0),ipoints(2,1),color,opacity);\n      cimg_init_scanline(opacity);\n      int\n        xmin = 0, ymin = 0,\n        xmax = ipoints.get_shared_row(0).max_min(xmin),\n        ymax = ipoints.get_shared_row(1).max_min(ymin);\n      if (xmax<0 || xmin>=width() || ymax<0 || ymin>=height()) return *this;\n      if (ymin==ymax) return draw_line(xmin,ymin,xmax,ymax,color,opacity);","sourceCodeStart":52284,"sourceCodeEnd":52320,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L52284-L52320","documentation":"CImg's draw_polygon() expects the points image to be a 2xN matrix: height must be exactly 2 (row 0 = x coordinates, row 1 = y coordinates). This error means points.height()!=2, so the point set has an invalid layout and CImg throws CImgArgumentException.","triggerScenarios":"Calling img.draw_polygon(points, color, opacity) where points is a CImg with height() other than 2 — e.g. an Nx2 matrix stored transposed, a 3xN (x,y,z) set, or a scalar/list image.","commonSituations":"Storing polygon vertices as one-point-per-row (Nx2) instead of CImg's 2xN convention; passing 3D coordinates including z; reusing a point cloud image; transposing errors from get_transpose().","solutions":["Reshape points to 2xN: build a CImg(2,N) with x in row 0 and y in row 1 (e.g. points.get_transpose() if you have Nx2).","If points are 3D, drop the z row: keep only x,y rows.","Validate points.height()==2 && points.width()>=3 before calling."],"exampleFix":"// before\nCImg<int> pts(2,4); // built with one vertex per column-slot but wrong axes; height=4\nimg.draw_polygon(pts, color, 1.0f); // throws: height != 2\n// after\nCImg<int> pts_ok(2,4); // row 0 = x coords, row 1 = y coords\npts_ok(0,0)=x0; pts_ok(1,0)=y0; /* ... */\nimg.draw_polygon(pts_ok, color, 1.0f);","handlingStrategy":"validation","validationCode":"if (points.height() != 2)\n  throw std::invalid_argument(\"draw_polygon expects a 2xN point image (row 0 = x, row 1 = y)\");\nimg.draw_polygon(points, color, opacity);","typeGuard":"bool valid_point_set = (points.height() == 2) && (points.width() >= 3);","tryCatchPattern":"try {\n  img.draw_polygon(points, color, opacity);\n} catch (CImgArgumentException& e) {\n  std::cerr << \"Invalid point set: \" << e.what() << std::endl;\n}","preventionTips":["Always build point sets as CImg(2,N): x in row 0, y in row 1","Transpose Nx2 matrices before passing to draw_polygon","Drop z coordinates from 3D point sets before polygon calls"],"tags":["cimg","image-processing","polygon","shape-mismatch"],"backgroundTag":"shape-mismatch","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}